super

作者: 雪上霜 | 来源:发表于2020-04-23 17:52 被阅读0次
    • super:关键字

    • 与this对比学习

      • this:出现在实例方法中和构造方法中,不能出现在静态方法中。
      • this(),只能出现在构造方法第一行。通过当前构造方法去调用本类中其他构造方法,目的是:代码复用。
      • 语法:this. this()
      • super:出现在实例方法中和构造方法中,不能出现在静态方法中。
      • 语法:super. super()
      • super(),只能出现在构造方法第一行。通过当前构造方法去调用父类中构造方法,目的是:创建子类对象时,先初始化父类型特征。
      • super(),通过子类中构造方法调用父类构造方法,模拟现实世界中这种场景,要想有儿子,先要有父亲。
      public class Super{
        public static void main(String[] args){
            new B();    //A先执行,在执行B构造方法
        }
      }
      
      class A{
        //建议将无参构造写出。
        /*
        public A(){
            System.out.prinlnt("A");
        }
        */
        //一个类没有手动提供构造方法,系统默认提供一个无参构造方法。
        public A(int i){
            
        }
      }
      
      class B extends A{
        public B(){
            super();        //默认的,自动加的。,A中没有了默认无参。
            //super(123);   //如果有此行,上行则没有了,会调用A的有参构造方法。
            System.out.prinlnt("B");
        }
        
        public B(String name){
            System.out.prinlnt("B String");
        }
      }
      
    • 当一个构造方法第一行,既没有this()又没有super(),默认有一个super(),表示通过当前子类的构造方法调用父类的无参数构造方法。所以必须保证父类的无参构造方法是存在的。

    • this()和super()都只能存在第一行。

    • 无论怎样,父类的构造方法一定会执行。

    • //判断程序的输出结果。
      public class SuperTest{
        public static void main(String[] args){
            new C();
        }
      }
      
      class A{
        public A(){
            System.out.println("A");
        }
      }
      
      class B extends A{
        public B(){
            System.out.println("B");
        }
        public B(String name){
            System.out.println("B String");
        }
      }
      
      class C extends B{
        public C{
            this("hziong");
            System.out.println("C");
        }
        
        public C(String name){
            this(name,20);
            System.out.println("C String");
        }
        
        public C(String name,int age){
            super(name);
            System.out.println("C String int");
        }
      }
      
    • 在Java中,无论调用什么对象,Object类的无参构造一定会调用。且最先执行。

    • 无参构造最好写出来,否则可能会影响子类的创建。

    • 在恰当的时间使用super(实参列表)。

    • public class SuperTest{
        public static void main(String[] args){
            CreditAccount ca = new CreditAccount();
            System.out.prinln();
            CreditAccount ca2 = new CreditAccount("1111",10000.0,0.999);
        }
      }
      
      class Acount{
            private String actno;
            private double balance;
            
            public Account{
            
            }
            
            public Account(String actno,double balance){
                this.actno = actno;
                thia.balance = balance;
            }
            
            
        }
        
        class CreditAccount extends Account{
            private double credit;
            
            public CreditAccount{
                
            }
            
            public CreditAccount(String actno,double balance,double credit){
                //this.actno = actno;
            //this.balance = balance;//actno ,balance 是父类中私有属性。
            super(actno,balance);//以上两行代码可以使用本行代码。
                this.credit = credit;
            }
        }
      
    • 注意:虽然调用构造方法,在构造方法执行过程中,一连串调用了父类的构造方法,父类的构造方法又继续向下调用它的父类构造方法,但是实际上对象只创建了一个。

    • super(实参)到底是干啥的??

      • 作用:初始化当前对象的父类型特征。并不是创建新对象。实际上对象只创建了1个。
    • super代表什么??

      • super关键字代表当前对象的那部分父类型特征。
    image.png
    image.png
    public class SuperTest{
        public static void main(String[] args){
            Vip v = new Vip("zhangsan");
            v.shopping();
        }
    }
    
    class Customer{
        String name;
        
        public Customer(){}
        public Customer(String name){
            this.name = name;
        }
    }
    
    class Vip extends Customer{
        public Vip(){}
        public Vip(String name){
            super(name);
        }
        //super和this都不能出现在静态方法中。
        public void shopping(){
            //this表示当前对象
            System.out.println(this.name + "shopping");
            //super表示当前对象的父类型特征(super是this指向的那个对象总的一块空间)
            System.out.println(super.name + "shopping");
            System.out.println(name + "shopping");
        }
    }
    
    image.png
    public class SuperTest{
        public static void main(String[] args){
            Vip v = new Vip("zhangsan");
            v.shopping();
        }
    }
    
    class Customer{
        String name;
        
        public Customer(){}
        public Customer(String name){
            this.name = name;
        }
    }
    
    class Vip extends Customer{
        String name;//与父类有一个同名属性。
    
        public Vip(){}
        public Vip(String name){
            super(name);
            //this.name = null;
        }
        public void shopping(){
            System.out.println(this.name + "shopping");
            System.out.println(super.name + "shopping");
            System.out.println(name + "shopping");
        }
    }
    
    image.png
    public class SuperTest{
      public static void main(String[] args){
          Vip v = new Vip("zhangsan");
          v.shopping();
                  v.doSome();
      }
    }
    
    class Customer{
      String name;
      
      public Customer(){}
      public Customer(String name){
          this.name = name;
      }
      
      public void doSome(){
          System.out.println(this.name+"do some");
          System.out.println(name+"do some");
          System.out.println(super.name+"do some");//父类中没有name。
      }
    }
    
    class Vip extends Customer{
      String name;//与父类有一个同名属性。java中允许子类出现和父类一样的同名属性。
    
      public Vip(){}
      public Vip(String name){
          super(name);
          //this.name = null;
      }
      public void shopping(){
          //Java区分父类与子类的同名属性,this :当前对象的同名属性,super:当前对象中父类特征的同名属性。
          System.out.println(this.name + "shopping");
          System.out.println(super.name + "shopping");
          System.out.println(name + "shopping");
      }
    }
    
    • super.什么时候不能省略:

      • 如果父类和子类中有同名属性,如果想在子类中访问父类中的属性,不能省。
    • super不是引用,也不保存内存地址,也不指向任何对象。

    • super 只是代表当前对象内部的那块父类型特征。

    • this和super不能使用在静态方法中。

      public class SuperTest{
        public void doSome(){
            System.out.println(this);   //可以
            System.out.println(super);  //错误
        }
        
        public static void main(String[] args){
            SuperTest v = new SuperTest();
            v.doSome();
        }
      }
      
      public class SuperTest{
        public static void main(String[] args){
            Cat c = new Cat();
            c.yiDong();
        }
      }
      
      class Animal{
        public void move(){
            System.out.println("Animal move!");
        }
      }
      
      class Cat extends Animal{
        public void move(){
            System.out.println("Cat move");
        }
        
        public void yiDong(){
            this.move();
            move();
            super.move();
        }
      }
      
      • 如果父和子有相同的属性和方法,需要用super.区分。
      • super.属性名:访问父类的属性
      • super.方法名(实参):访问父类的方法
      • super(实参):调用父类的构造方法
    public class Test{
        public static void main(String[] args){
            A a = new A(100);
            B b = new B(a);
            
            java.util.Scanner  s = new java.util.Scanner(System.in);
            while(true){
                System.out.println("请输入猜测数字");
                int caiCeNum = s.nextInt();
                b.cai(caiCeNum);
            }
        }
    }
    
    class A{
        private int v;
        
        public A(){
            
        }
        public A(int v){
            this.v = v;
        }
        public int getV(){
            return v;
        }
    }
    
    class B{
        
        private A a;
        
        public B(){}
    
        public B(A a){
            this.a = a;
        }
        
        public void setA(A a){
            this.a = a;
        }
        
        public A getA(){
            return A;
        }
        public void cai(int caiCeNum){
            //int shijizhi = this.getA().getV();
            int shijizhi = a.getV();
            if(caiCeNum == shiJiZhi){
                System.out.println("success");
                System.exit(0); //退出JVM
            }else if(shiJiZhi > caiCeNum){
                System.out.println("小了");
            }else {
                System.out.println("大了");
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:super

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