super

2020-04-23  本文已影响0人  雪上霜
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");
  }
}
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("大了");
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读