super关键字

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 move
      cat.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);//因为子类的有参构造将参数传给了父类,所以子类这里是null
        System.out.println(super.name);
    }

}
public class text {
    public static void main(String[] args) {
         Cat cat=new Cat("狸花猫");
         cat.some();
    }
}
//输出结果:
//null
//狸花猫

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值