this与super怎么在Java中使用

发布时间:2021-02-04 16:05:11 作者:Leah
来源:亿速云 阅读:149

这篇文章给大家介绍this与super怎么在Java中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、this

概念:this代表着当前对象的引用,也是当前函数所属对象的引用。直白的说,哪个对象调用了当前函数,this就代表哪个对象。

常见的用法(理论不理解就先参考下面案例)

this使用案例

class Student extends Person{

 public Student(String name, int age, String nation) {
  super(name);
 }
}

public class Person{
 private String name;
 private int age;
 private static String nation = "chinese";

 public Person(String name) {
  this.name = name;
 }

 public Person(String name, int age) {
  //初始化方法必须放到第一行
  this(name);
  this.age = age;
 }

 //局部变量name和age屏蔽了name和age属性,用this进行标识
 public Person(String name, int age,String nation) {
  this.name = name;
  this.age = age;
  //this可以调用到静态属性
  this.nation = nation;
 }


 public void test(){
  System.out.println("测试");
 }

 public static void test2(){
  //静态方法内不能出现this或super
  System.out.println("测试");
 }

 public void print(){
  //调用类内部的方法(toString已重写)加不加this都行
  this.toString();
 }

 @Override
 public String toString() {
  return "Person{" +
    "name='" + name + '\'' +
    ", age=" + age +
    '}';
 }

 public static void main(String[] args) {
  //这个this代表的student对象
  Student student = new Student("wzh",20,"chinese2");
  System.out.println(student.toString());
  //这个this代表的是person对象
  Person person = new Person("wzh3",20,"chinese3");
  System.out.println(student.toString());
  //扩展,全局变量nation的值最后是chinese3
  System.out.println(Person.nation);
 }
}

结果:

this与super怎么在Java中使用

二、super

概念:
super可以理解为“父类的”,super可以在子类中调用父类的属性,方法,构造器,super关键字和this一样能省就省;

注意点:
1、this和super很像,this指向的是当前对象的调用,super指向的是当前调用对象的父类
2、类加载完毕,创建对象,父类的构造方法会被调用

父类如果重写了无参构造器或者父类中没有有参构造器,那么子类的构造方法第一行就是super(),可以省略

class Student extends Person{
 //这是默认的构造器内容,写出来是为了帮大家理解
 public Student(){
  super();
 }
}

public class Person{
 private String name;
 private int age;
}

如果父类中定义了有参构造器但没有显示写出无参构造器,那么必须通过super调用父类的有参构造函数,如果父类中定义了多个有参构造区,那么用super调用其中一个有参构造器即可

class Student extends Person{

 public Student(String name, int age) {
  //任选一个父类有参构造
  //super(name, age);
  super(name);
 }
}

public class Person{
 private String name;
 private int age;

 public Person(String name, int age) {
  this.name = name;
  this.age = age;
 }

 public Person(String name) {
  this.name = name;
 }
}

3、子类相应构造创建了一个子类对象,该子类对象还包含了一个父类对象。该父类对象在子类对象内部(正如子类的构造器无论如何都要通过super调用父类构造器一样,子类的对象被成功构造,那么它的内部就会包含父类的对象),证明如下

子类重写父类的方法后可以通过super调用到父类的方法

class Student extends Person {
 private String name = "wzh3";

 @Override
 public String getName() {
  return "子类" + name;
 }

 public String getParentName(){
  //调用父类的方法
  return super.getName();
 }

 public static void main(String[] args) {
  Student student = new Student();
  System.out.println(student.getName());
  System.out.println(student.getParentName());
 }
}

public class Person{
 //protected意味着子类和同一包中可以访问
 protected String name = "wzh"; 
 protected int age = 20;

 public String getName() {
  return "父类" +name;
 }
}

输出结果

this与super怎么在Java中使用

子类获取到父类的变量

class Student extends Person{

 public void parentDisplay(){
  System.out.println(super.age + super.name);
 }

 public static void main(String[] args) {
  new Student().parentDisplay(); //输出结果:20wzh
 }
}

public class Person{
 //protected意味着子类和同一包中可以访问
 protected String name = "wzh";
 protected int age = 20;
}

调用父类的构造器
不再举例

4、this super只能在有对象的前提下使用,不能在静态上下文使用
5、如果一个类没有基础任何父类,super还有用吗?肯定有用,可以调用Object中的方法

public class Person{
 private String name;
 private int age;

 public void display(){
  //通过this或super调用到了Object的toString();
  System.out.println(super.toString());
 }

 public static void main(String[] args) {
  new Person().display(); //输出为Person@452b3a41
 }

}

关于this与super怎么在Java中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. this与super怎么在java项目中使用
  2. super和this怎么在java中使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java this super

上一篇:IDEA2018卡死不动如何解决

下一篇:怎么在IDEA中设置Tab选项卡

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》