vs this: 1、代表的对象不同:this 本身调用者的对象 super 代表父类对象的引用 2、前提:this 没有继承也可以使用 super只能在继承条件下使用 3、构造方法:this(); 本类的构造 super(); 父类的构造
com.oop.Demo05;//學生 人 父类publicclassPerson{publicPerson(){System.out.println("person无参执行了");}privateint money=10000000;protectedString name ="张三";publicvoidprint(){System.out.println("Person");}//private 私有的东西无法被继承publicvoidsay(){System.out.println("说了一句话");}}
packagecom.oop.Demo05;//學生 is 人 派生类 子类publicclassStudentextendsPerson{publicStudent(){//隐藏代码 调用了父类无参构造super();//调用父类构造器,必须要在子类构造器的第一行System.out.println("Student无参执行了");}privateString name ="李四";publicvoidprint(){System.out.println("Student");}publicvoidtest1(){print();//studentthis.print();//studentsuper.print();//person}publicvoidtest(String name){System.out.println(name);System.out.println(this.name);System.out.println(super.name);}}