Java中super 关键字是什么?用来干什么?

super 关键字

含义

super : 超级的
含义: 在Java中代表当前对象的 直接父类对象的引用

用法分类

super.
super(实际参数)

1、super(实际参数)

super():代表调⽤⽗类⽆参的构造
super(参数):调⽤⽗类有参的构造

作用: 在⼦类构造⽅法中调⽤⽗类的构造⽅法

特点:
1.如果⼦类构造⽅法中的第⼀⾏没有this()或者this(参数),也不是super(参数) 默认是
super(),构造⽅法的第⼀⾏不是this(...)就是super(...)
2.super()和super(参数)只能在构造⽅法的第⼀⾏

注意: super()和super(参数)只能⽤在⼦类的构造⽅法中

案例: 
package com.ujiuye.day09;

public class Demo10 {
    public static void main(String[] args) {
        //Sub sub = new Sub();
       Sub sub = new Sub(10,20);

       // 在定义一个类的时候 如果已经定义了有参构造 在开发中一般要将无参数的构造方法补出来
    }
}



// 定义一个父类
class Super{
    int a;
    int b;

    //将构造
    public Super(){
        System.out.println("我是父类无参数的构造方法");
    }
    public Super(int a, int b) {
        System.out.println("我是父类有参数的构造方法");
        this.a = a;
        this.b = b;
    }


}

//定义一个子类
class Sub extends  Super{
    int c;
    int d;
    //构造方法
    public Sub(){
        super(11,22);
        System.out.println("我是子类的无参数的构造方法");
    }

    public Sub(int c, int d) {
        super(11,22);
        System.out.println("我是子类的有参数的构造方法");
        this.c = c;
        this.d = d;
    }

}


案例2: 
package com.ujiuye.day09;

public class Demo10 {
    public static void main(String[] args) {
        //Sub sub = new Sub();
       Sub sub = new Sub(10,20);

       // 在定义一个类的时候 如果已经定义了有参构造 在开发中一般要将无参数的构造方法补出来
    }
}



// 定义一个父类
class Super{
    int a;
    int b;

    //将构造
    public Super(){
        System.out.println("我是父类无参数的构造方法");
    }
    public Super(int a, int b) {
        System.out.println("我是父类有参数的构造方法");
        this.a = a;
        this.b = b;
    }


}

//定义一个子类
class Sub extends  Super{
    int c;
    int d;
    //构造方法
    public Sub(){
        super();
        System.out.println("我是子类的无参数的构造方法");
    }

    public Sub(int c, int d) {
        this();
        System.out.println("我是子类的有参数的构造方法");
        this.c = c;
        this.d = d;
    }

}

案例3: 
package com.ujiuye.day09;

public class Demo10 {
    public static void main(String[] args) {
        //Sub sub = new Sub();
       Sub sub = new Sub(10,20);

       // 在定义一个类的时候 如果已经定义了有参构造 在开发中一般要将无参数的构造方法补出来
    }
}

class YeYe{
    public YeYe(){
        System.out.println("我是yeye的构造方法");
    }
}

// 定义一个父类
class Super extends YeYe{
    int a;
    int b;

    //将构造
    public Super(){
        super();
        System.out.println("我是父类无参数的构造方法");
    }
    public Super(int a, int b) {
        System.out.println("我是父类有参数的构造方法");
        this.a = a;
        this.b = b;
    }


}

//定义一个子类
class Sub extends  Super{
    int c;
    int d;
    //构造方法
    public Sub(){
        super();
        System.out.println("我是子类的无参数的构造方法");
    }

    public Sub(int c, int d) {
        this();
        System.out.println("我是子类的有参数的构造方法");
        this.c = c;
        this.d = d;
    }

}

2、super .

作用: 区分 直接父类中和子类中重名的成员(成员变量和成员方法)

案例: 
package com.ujiuye.day09;

public class Demo10 {
    public static void main(String[] args) {
        Sub sub = new Sub();
        sub.m();
    }
}

class YeYe{
    int b = 1;
}

// 定义一个父类
class Super extends YeYe{
    int b = 10;

    public int m1(){
       return  super.b;
    }
}

//定义一个子类
class Sub extends  Super{// sub中有3个 b 成员变量
   int b = 20;

   public void m(){
       int b = 30;
       //System.out.println(b);
       //System.out.println(this.b);
       System.out.println(super.b);//super 指代 当前子类的直接父类对象的引用
       System.out.println(super.b);
       int i = m1();
       System.out.println(i);
   }
}

案例2: 
package com.ujiuye.day09;

public class Demo10 {
    public static void main(String[] args) {
        Sub sub = new Sub();
        sub.m2();
    }
}


// 定义一个父类
class Super{
    public void m1(){
        System.out.println("我是父类中的m1方法");
    }
}

//定义一个子类
class Sub extends  Super{// sub中有3个 b 成员变量

    @Override
    public void m1(){
        System.out.println("我是子类重写父类的m1方法");
    }

    public void m2(){
       //this.m1();//this.m1()
        super.m1();
    }

}

总结

super: 当前类的直接父类对象的引用

	1、super(实际参数):在子类的构造方法中 调用父类的构造方法
		注意: 
			1. 只能用在构造方法中
			2. 只能是构造方法的第一条有效语句
			3. 如果一个构造方法的第一行不是 this() / this(实参) / super(实参),默认是super()
	2、super. : 当子类中存在和父类同名的成员变量,需要使用super 指定父类的重名的成员变量.
posted @ 2021-04-11 23:32  泰初  阅读(2288)  评论(0编辑  收藏  举报