深圳做网站建设公司,青岛网景互联网站建设公司,金华做网站的公司,网站免费正能量直播Super详解#xff08;重点#xff09;
super注意点; 1. super调用父类的方法#xff0c;必须在构造方法的第一个2. super必须只能出现在子类的方法或者构造方法中#xff01;3. super和this 不能同时调用构造方法#xff01;Vs this:
代表的对象不同#xff1a;
…Super详解重点
super注意点; 1. super调用父类的方法必须在构造方法的第一个2. super必须只能出现在子类的方法或者构造方法中3. super和this 不能同时调用构造方法Vs this:
代表的对象不同
this:本身调用者这个对象
super代表父类对象的应用
前提
this没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法
this本类的构造
super(); 父类的构造
package com.oop;import com.oop.demo05.Student;//一个项目应该这存在一个main方法
public class Application {public static void main(String[] args) {Student student new Student();// student.test(我不会);//student.test1();}
}package com.oop.demo05;//java中,所有的类都默认直接或者继承object
//Person 人父类或基类
public class Person {public Person() {System.out.println(Person无参执行);}protected String name你好;//私有的东西无法被继承public void print(){System.out.println(Person);}}package com.oop.demo05;//Student 学生 is 人派生类或子类
//子类继承了父类会拥有父类的全部方法
public class Student extends Person{public Student() {//隐藏代码调用了父类的无参构造super();//调用父类的构造器必须要在子类构造器的第一行。System.out.println(Student无参执行);}private String name不知道;public void print() {System.out.println(Student);}public void test1(){print();//Studentthis.print();//Studentsuper.print();//Person}public void test(String name){System.out.println(name);//我不会System.out.println(this.name);//不知道System.out.println(super.name);//你好}}