father f=new son()的问题!!

linj 2001-11-16 10:03:30
class Father {
int var1=10;
void amethod() {
System.out.println("father method ");
}
}
public class Son extends Father {
int var1=20;
void amethod() {
System.out.println("father is over ridden ");
}
public void bmethod() {
System.out.println("son's method ");
}

public static void main(String as[]) {
Father f=new Son();
System.out.println(f.var1);

f.amethod();

}
}

...全文
556 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
linj 2001-11-19
  • 打赏
  • 举报
回复
呵呵,再次谢谢你!为我们陕西人自豪!!
向你学习!!
tiger_lei 2001-11-19
  • 打赏
  • 举报
回复
其实我也不懂,看了问题后再去看规范,应该是你帮我才对,你知道,直接去看规范很不爽。
fen doesn't matter,CSDN出了问题,我的分还是几个月之间的记录。
linj 2001-11-19
  • 打赏
  • 举报
回复
to tiger_lei(雷老虎):
谢谢!
水平很高并且肯乐于助人,佩服佩服!!!
linj 2001-11-19
  • 打赏
  • 举报
回复
不能给分?????
linj 2001-11-19
  • 打赏
  • 举报
回复
呵呵,明白!
谢谢各位了!!!
java_alias 2001-11-17
  • 打赏
  • 举报
回复
to tiger_lei(雷老虎):高手!!果然是高手!!
tiger_lei 2001-11-17
  • 打赏
  • 举报
回复
airfei(airfei)说得对,但是也有一点不准确。
这里应该是hide的关系。
If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.
......
A hidden field can be accessed by using a qualified name (if it is static) or by using a field access expression (§15.11) that contains the keyword super or a cast to a superclass type. See §15.11.2 for discussion and an example.
“如果一个类声明了一个成员变量,则此声明会隐藏其父类和父接口中所有同名的、可以访问的成员变量。
...
被隐藏的成员变量如果是static的,则可以使用全名来访问它(这里全名就是package.class.field的形式),如果是非static的,可以用super.field 或强制类型转换为父类后进行访问。“
这是java语言规范中的一句话,如果我翻的不行请参照原文。
我的理解是: 顾名思义,hide是把父类的成员变量隐藏起来,你不会一眼就看见它(不能用正常的、简单的方式访问到它),而要揭开一层盖子才能发现(用super或cast到父类)。而override相信是比较清楚的,覆盖后原来的方法已经不存在了,你是无论如何也看不见它的。

回到father和son,sunjiujiu(芳芳)说的也有道理,“其实Father f = new Son() 相当于:
Father f = new Father();
Son s = new Son();
f = s;”
但我觉得 Father f= (Father)(new Son())更准确一点。因此在 f.var1是相当于有一个cast的动作,因此就访问到了被hide的Father中的var1,而f.amethod虽然也有cast,但是amethod已经被override了,也就只可能调用Son中的定义了。

至于shadow,通常是址在一个类(或一个代码块)中的重复定义。
class Test {
static int x = 1;
public static void main(String[] args) {
int x = 0;
System.out.print("x=" + x);
System.out.println(", Test.x=" + Test.x);
}
}

此时在main中定义的x 就shadow了外部的x,当然只是在main(x的scope)中才会shadow,出了main后,x指的就是外部的x。
airfei 2001-11-16
  • 打赏
  • 举报
回复
方法是overriden,
但是variable是shadow,即变量是不overriden的。
tanghuan 2001-11-16
  • 打赏
  • 举报
回复
这是上溯造型
运行一下就明白了
public static void main(String as[]) {
Son s,s1;
Father f=s=new Son();
s1=(Son)f;
System.out.println(f.var1);
System.out.println(s.var1);
System.out.println(s1.var1);

f.amethod();

}
linj 2001-11-16
  • 打赏
  • 举报
回复
呵呵,没人会还是都不屑与回答???
Schuman 2001-11-16
  • 打赏
  • 举报
回复
有道理,初始化过程中,先初始化所有的成员变量,在调用构造;而在构造中调用父类的初始化,所以var是10。函数的调用是多态的特征。不知道我说的有没有错误,请执教
linj 2001-11-16
  • 打赏
  • 举报
回复
to: sunjiujiu(芳芳) 
不对,初始化是先父类再子类的。
你搞反了
sunjiujiu 2001-11-16
  • 打赏
  • 举报
回复
我认为是这样的,其实Father f = new Son() 相当于:
Father f = new Father();
Son s = new Son();
f = s;
这样f和s就指向了同一块存储空间,而由于他们的继承关系,是先初始化s,后初始化f,所以var就是10了。而对于函数,两个函数分别有自己的存储空间的,不会产生覆盖的问题。
linj 2001-11-16
  • 打赏
  • 举报
回复
?????????????????????
hexiaofeng 2001-11-16
  • 打赏
  • 举报
回复
关注
linj 2001-11-16
  • 打赏
  • 举报
回复
不好意思,忘了写问题
上面的程序输出为:
10
father is over ridden
请问为什么这样?为什么f.var1调用父类,而f.amethod()却调用字类的????

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧