40-Super







Super

  • main 方法

    public class Application {
        public static void main(String[] args) {
            Student_03 student = new Student_03();
            student.test("叮当当");
        }
    }
  • 父类 Person

    package Oop.Demo_02;
    public class Person_03 {
        protected String name = "dingdang";
    }
  • 子类 Student

    package Oop.Demo_02;
    public class Person_03 {
        protected String name = "dingdang";
    }

调用父类的方法


  • main 方法

    public class Application {
        public static void main(String[] args) {
            Student_03 student = new Student_03();
            student.test1();
        }
    }
  • 父类 Person

    package Oop.Demo_02;
    public class Person_03 {
        protected String name = "dingdang";
        public void print () {
            System.out.println("这里Person");
        }
    }
  • 子类 Student

    package Oop.Demo_02;
    public class Student_03 extends Person_03{
        public void print(){
            System.out.println("这里是Student");
        }
        public void test1 () {
            print();
            this.print();
            super.print();
        }
    }

无参构造


  • main 方法

    public class Application {
        public static void main(String[] args) {
            Student_03 student = new Student_03();
        }
    }
  • 父类 Person

    package Oop.Demo_02;
    public class Person_03 {
        public Person_03() {
            System.out.println("Person无参构造启动了");
        }
    }
  • 子类 Student

    package Oop.Demo_02;
    public class Student_03 extends Person_03{
        public Student_03() {
            System.out.println("Student无参构造启动了");
        }
    }

    执行 main 之后获得输出

    Person无参构造启动了
    Student无参构造启动了

    说明默认调用父类 Person 的无参构造

注意


  • super 调用父类的构造方法,必须在构造方法的第一个

  • super 必须只能出现在子类的方法或者构造方法中

  • superthis 不能同时调用构造方法

对比

  1. 代表的对象不同

    this: 本身调用着这个对象

    super: 代表父类对象的应用

  2. 前提

    this: 没有继承也可以使用

    super: 只能在继承条件才能使用

  3. 构造方法

    this(): 本类的构造

    super: 父类的构造


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇