1、super
(1)super是一个关键字。
(2)super和this很类似,我们对比着学习。
2、先复习一下this关键字的使用。
(1)this能出现在实例方法和构造方法中;
(2)this的语法是“this.”和“this()”;
(3)this不能出现在静态方法中;
(4)this大部分情况下是可以省略的;
(5)this.什么时候不能省略呢?
Public void setName(String name){this.name = name;
}
// 在区分局部变量和实例变量时this不能省略
(6)this()只能出现在构造方法的第一行,通过当前的构造方法去调用“本类”中其他构造方法,目的是:代码复用。
3、super关键字:
(1)super能出现在实例方法和构造方法中。
(2)super的语法是“super.”和“super()”。
(3) super不能出现在静态方法中。
(4) super大部分情况下是可以省略的。
(5)super.什么时候不能省略呢?
当在子类对象中,子类想访问父类的东西,可以使用“super.”的方式访问。例如:方法覆盖后,子类内部虽然重写了父类的方法,但子类也想使用一下父类的被覆盖的方法,此时可以使用“super.”的方式。当子类中出现和父类一样的属性或者方法,此时,你要想去调用父类的那个属性或者方法,此时“super.”不能省略。
(5)super()只能出现在构造方法的第一行,通过当前的构造方法去调用“父类”中的对应的构造方法,目的是:创建子类对象时,先初始化父类型特征。
4.当子类的构造方法内第一行没有super()与this()时,系统会默认给它加上无参数的super()去调用父类的无参数构造,所以建议无参构造手动写出来
public class Super {public static void main(String[] args) {
// 创建子类对象new B();
// 这是A类的无参构造
// 这是B类的无参构造}
}
class A{public A(){System.out.println("这是A类的无参构造");}}class B extends A{public B(){//省略super()与this()默认有super()System.out.println("这是B类的无参构造");}}
5.this与super不能共存(因为他们都只能出现在第一行)
class A{public A(){System.out.println("这是A类的无参构造");}}class B extends A{public B(){this("wy");super();//Error:(18, 14) java: 对super的调用必须是构造器中的第一个语句System.out.println("这是B类的无参构造");}public B(String name){System.out.println("这是B类的有参构造");}}
6.子类创建对象时父类的构造方法一定会执行且先执行,模拟的现实生活中有子必先有父
public class Super {public static void main(String[] args) {//创建子类对象new B();//输出结果:// 这是A类的无参构造// 这是B类的有参构造// 这是B类的无参构造}
}
class A{public A(){System.out.println("这是A类的无参构造");}}class B extends A{public B(){this("wy");//这里调用本类的有参构造System.out.println("这是B类的无参构造");}public B(String name){//默认调用父类的无参构造System.out.println("这是B类的有参构造");}}
7.super()的使用
子类对象调用父类的属性可以用get与set方法进行访问与赋值,但是如果需要在创建对象时候就要赋值就要用到我们的有参构造
class A{private String name;private int age;//构造方法public A(){}public A(String name, int age) {this.name = name;this.age = age;}//set、get方法public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}class B extends A{private String sex;public B(){}public B(String sex,String name,int age){super(name,age);//调用父类的有参构造this.sex=sex;}//get、set方法public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}
public class text {public static void main(String[] args) {B b=new B("男","张三丰",20);//重点看B类的有参构造}
}
8.super.的使用
使用super.访问父类被覆盖的方法:
因为方法被覆盖,子类对象无法直接访问父类覆盖的方法,但是可以通过子类独有的方法访问父类被覆盖的方法
class Animal{public void move(){System.out.println("Animal move");}
}class Cat extends Animal{public void move(){System.out.println("Cat move");}//访问父类被覆盖的方法:因为方法被覆盖,子类对象不能直接访问父类覆盖的方法,但是可以通过子类独有的方法访问父类被覆盖的方法public void some(){super.move();}
}
public class text {public static void main(String[] args) {Cat cat=new Cat();cat.move();//Cat movecat.some();///Animal move}
}
使用super.访问与父类重名的属性:
注意:父类重名的属性不可以封装
class Animal{String name;public Animal(){}public Animal(String name){this.name=name;}
}class Cat extends Animal{private String name;public Cat(){}public Cat(String name){super(name);//this.name=name;}public void some(){System.out.println(this.name);//因为子类的有参构造将参数传给了父类,所以子类这里是nullSystem.out.println(super.name);}}
public class text {public static void main(String[] args) {Cat cat=new Cat("狸花猫");cat.some();}
}
//输出结果:
//null
//狸花猫