美文网首页
super||继承

super||继承

作者: 哈迪斯Java | 来源:发表于2021-09-23 11:00 被阅读0次
    image.png

    ===========

    super注意点:


    image.png

    ===
    首选需要注意一下几个点:

    ~1、super调用父类的构造方法时。必须在构造方法的第一个

    ~2、super必须只能出现在子类的方法或者说构造方法中

    ~3、super和this不能同时调用构造方法

    于此同时,与this的区别:

      this本身调用着这个对象
    super代表父类对象的应用
    
    ~~~~前提不同:
    this没有继承也可以使用
    但是super只能在继承条件下才可以使用
    
    
    ~~~~构造方法不同:
    this();本类的构造
    super();父类的构造
    
    =====================
    person:
    package oop.Demon5;
    
    //Person 人   父亲
    public  class Person  {
    
    //    public
        //protected
        //default
    //      private
    
    
        public Person() {
            System.out.println("Person无参执行了");
        }
    
        protected String name = "tang";
    
        //私有的无法被继承
        public void print(){
            System.out.println("Person");
        }
    
    
        }
        //在java中,所有的类都直接或者间接默认继承object
    
    
    
    student:
    package oop.Demon5;
    
    
    //学生
    public class Student extends Person {
    
        public Student() {
            //隐形代码:调用了父类的无参构造
            super();//调用父类的构造器,必须放在子类构造器的第一行
           // this();和super();一样的要求
            System.out.println("Student无参执行了");
        }
    
        public Student(String name) {
            this.name = name;
        }
    
        private String name ="yang";
    
        public void print(){
            System.out.println("Student");
        }
    
        public void test1(){
           print();//Student
           this.print();//Student
           super.print();//Person
    
    
        }
        public void test(String name){
    
        }
    
        //crl+h快捷键
    
    }
    
    
    
    
    Application:
    import oop.Demon5.Student;
    
    public class Application {
        public static void main(String[] args) {
            Student student = new Student();
    
            //student.test("小明");
            //student.test1();
    
    
        }
    }
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:super||继承

          本文链接:https://www.haomeiwen.com/subject/tyingltx.html