this和super

this

1.获取当前对象的引用 --- 内存地址

public class Student {
   public Student B(){
       return this;
  }


   public static void main(String[] args) {
       Student s = new Student();
       System.out.println(s.B());
  }
}
com.bw.aa.thread.iterator1@4d405ef7

2.成员方法中调用

当形参和成员变量重名时,我们优先使用形参,所以我们要加this来区分

public class Student {
    
    Integer num;
    
    public int a(Integer age){
        return  num;
    }
}
​
​
public class Test1 extends Student{
    public  Integer age;
    public  String name;
    public test1(Integer age,String name){
          this.age = age;
          this.name = name;
    }
   /**
    形参和成员变量不同名时,我们可以不用加this区分
    public Test1(Integer a,String n){
          age = a;
          name = n;
    }  
    a = 1  name = haha
    
   形参和成员变量同名时,不加this,就表示形参赋值给形参,没有任何意义,返回为null
   public Test1(Integer age,String name){
          this.age = age;
          this.name = name;
    }  
    a = null  name = null
   */
    
   //重写的Student方法
   @Override
   public  int a(Integer a){
       //访问本类中的属性,如果本类中没有,就从父类中继续查询
        this.num = a;
        System.out.println(a);
        return a;
    }
​
    public static void main(String[] args) {
        Test1 test1 = new Test1(1,"haha");
        test1.a(2);
        System.out.println(test1);
    }
}
​
2
a = 1  name = haha

3.方法内部调用 -- 不必加this

public class Student {
    String name;
    Integer a;
    
    public int a(Integer A){
        B();
        return  A;
    }
​
    public Student B(){
        return this;
    }
 }

4.构造器中调用 ---- 只能调用一个,且必须在首行

public class Student {
    String name;
    Integer age;
   
    Student(){
        System.out.println("1111111");
    }
  
    Student(String a){
        this(1,"b");
        System.out.println("2");
    }
  
    Student(Integer b,String a){
        this();
        //必须在第一位 ,所以只能调用一个
        //this("a");
        System.out.println("3");
    }
​
    public static void main(String[] args) {
        Student S = new Student("2");
        System.out.println(S);
    }
}
1111111
3
2
com.bw.aa.thread.iterator1@4d405ef7

5.static修饰的方法无法调用this关键字

public class Student {
    String name;
    Integer school;
    //this关键字指代当前对象,而静态方法是通过类调用的方法,不需要new来实例化对象,都不用实例化,自然就
    //没有当前对象了。因此,this关键字无法在static方法中使用就解释得通了。
  /**  
        public static  student B(){
            return this;
        }
        public static void setSchool(String school) {
            this.school = school;
        }
    */
}

super

当我重写一个方法时,我可以在完全覆盖父类的逻辑,但我只想在父类的基础上扩展一点自己的逻辑,我们就可以用super来调用父类的方法,从而复用父类的逻辑

1.访问成员变量

public class Student {
    String name;
    Integer a = 11111;
}
public class Test1 extends Student{
    public  Integer age;
    public  String name;
​
    public test1(Integer age,String name){
          this.age = super.a;
          this.name = name;
    }
    
    public static void main(String[] args) {
        Test1 test1 = new Test1(1,"sds");
        System.out.println(test1);
    }
}
a = 11111  name = sds

2.访问构造器

子类在构造时必须先得完成父类的构造,也就是说在所有的构造方法中第一行的逻辑必须是调用父类的构造方法,平常没有通过super调用构造器,是因为编译器会在默认第一行加上父类的无参构造方法。

public class Student {
    String name;
    Integer a;
    student(Integer age,String name){ }
    
}
public class Test1 extends Student{
    
     //父类只有有参构造器没有无参构造器,我们就必须用super来手动调用父类构造器。
    public test1(){
        //编译失败,因为父类没有无参构造方法
    }
    public test1(String name){
        //编译成功,因为this(age,name)指定了父类构造器
          this(18,"aaaaa");
    }
    public test1(Integer age,String name){
        //编译成功,因为指定了父类构造器
          super(age,name);
    }
}

3.访问成员方法

public class Student {
    public int setA(Integer a) {
        System.out.println(a);
        return  a;
    }
}
public class Test1 extends Student{
    public  Integer age;
    public  String name;
​
    public test1(){
          super.setA(11);
          
    }
    public static void main(String[] args) {
        Test1 test1 = new Test1();
        System.out.println(test1);
    }
}

this和super的区别

区别点thissuper
访问属性 访问本类中的属性,如果本类中没有,就从父类中继续查询 访问父级中的属性
调用方法 本类中 直接访问父类
调用构造器 调用本类构造器,必须放在构造器的首行 调用父类构造器,必须放在子类构造器的首行
特殊 表示当前对象 引用父类对象

在子类中,this或者super调用构造器只能使用一个。因为都在首行。

posted @ 2023-02-12 23:13  早日月薪五万  阅读(11)  评论(0编辑  收藏  举报