先贴源码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;}

解决方案 »

  1.   

    不好意思,贴错了。下面是源码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;
    }之行结果:
    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??哪位老大能给指点一下啊?
      

  2.   

    son类虽然继承了father类,但是没有重写getName方法,所以在做son1.getName()调用的时候会找不到相应的方法,这时会将son类向上转型,父类father有getName方法,所以就调用父类的getName方法,当然打印出的就是father了。
      

  3.   

    兄弟,当调用父类father有getName方法时,隐含参数this引用的是对象son1,我输出this.name,理应当是son1的成员变量name,其值应该是son1才对啊。我的问题其实是:this是son1的引用,而this.name的值却是father??
      

  4.   

    java中只有函数能够多态,成员变量不具备多态性。
      

  5.   

    在java中方法可以重载,可是属性不能重载,你在子类中又定义了一个String 类型的属性name,这时候类Son实际上有两个名字是name的String属性. 当你调用getName方法的时候,因为你的子类没有覆盖父类的getName方法,因此对父类中的这个方法来说,它要调用的属性name是属于父类的,因为它根本不知道子类还有一个属性也叫name.
      

  6.   

    在java中方法可以重载,可是属性不能重载
                    ~~~~             ~~~~
    不好意思,应该是覆盖