先贴源码public class ThisTest{ public static void main(String[] augs) { Son son1 = new Son("son1"); Son son2 = new Son("son2"); System.out.println("son1.name: "+son1.name); System.out.println("son1.getName(): "+son1.getName()); System.out.println(son1.equals(son2)); }} class Father{ public String getName() { System.out.println("This in getName: "+this.getClass()); return "Pring name field in "+this.getClass()+": "+this.name; } public boolean equals(Object otherObject) { System.out.println("This in equals: "+this.getClass()); if (this == otherObject) return true; if (otherObject == null) return false; if (this.getClass() != otherObject.getClass()) return false; Father other = (Father)otherObject; return this.name.equals(other.name); } public String name="father";} class Son extends Father{ public Son(String s) { name=s; } public String name;}
{
public static void main(String[] augs)
{
Son son1 = new Son("son1");
Son son2 = new Son("son2");
System.out.println("son1.name: "+son1.name);
System.out.println("son1.getName(): "+son1.getName());
System.out.println(son1.equals(son2));
}
}
class Father
{
public String getName()
{
System.out.println("This in getName: "+this.getClass());
return "Pring name field in "+this.getClass()+": "+this.name;
}
public boolean equals(Object otherObject)
{
System.out.println("This in equals: "+this.getClass());
if (this == otherObject) return true; if (otherObject == null) return false; if (this.getClass() != otherObject.getClass()) return false;
Father other = (Father)otherObject;
return this.name.equals(other.name);
} public String name="father";
}class Son extends Father
{
public Son(String s)
{
name=s;
}
public String name;
}之行结果:
son1.name: son1
This in getName: class Son
son1.getName(): Pring name field in class Son: father
This in equals: class Son
true
请注意第二行和第三行输出结果。我的问题是为什么this是son1的引用,而this.name的致却是father??哪位老大能给指点一下啊?
~~~~ ~~~~
不好意思,应该是覆盖