黑马程序员技术交流社区

标题: 关于this与super [打印本页]

作者: 爱学习爱java..    时间: 2016-8-18 09:34
标题: 关于this与super
class Grandfather
{        
        public void print()
        {        
                System.out.println("Grandfather") ;
        }
};


class Father extends Grandfather
{               
        public void print()
        {
                System.out.println("father") ;
        }
        public void funFather()
        {        
                this.print() ;        
                super.print() ;        
        }
};


class Child extends Father{        // 定义继承关系
        public void print()
        {        
                System.out.println("child") ;
        }
        public void funChild()//避免覆写
        {
                this.print() ;        
                super.print() ;        
        }
        
};


public class test3{
        public static void main(String args[])
        {
                Child s = new Child() ;
                s.funFather() ;//此处通过继承调用父类函数中的     this.print与super.print
                s.funChild();//此处直接调用子类类                this.print与super.print
        }
};
输出为
child
Grandfather
child
father
为什么第一个输出是child呢???而不是father??
作者: FunnyKing    时间: 2016-8-18 09:49
帮你顶顶
作者: talons    时间: 2016-8-18 09:55
this表示调用当前对象。funFather() 的调用者是Child s ,所以这个方法里面的this.print()是child的print
作者: 爱学习爱java..    时间: 2016-8-18 11:50
talons 发表于 2016-8-18 09:55
this表示调用当前对象。funFather() 的调用者是Child s ,所以这个方法里面的this.print()是child的print ...

谢谢。貌似理解一点了     
作者: 龙旋    时间: 2016-8-18 23:18
this表示对当前对象进行引用,super表示对父类的对象进行引用
作者: cc478508847    时间: 2016-8-18 23:21
一个是当对象,一个是当前父类对象,慢慢捋一遍就行




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2