软件编程
位置:首页>> 软件编程>> java编程>> Java中超详细this与super的概念和用法

Java中超详细this与super的概念和用法

作者:挖坑埋你的王子晗  发布时间:2021-05-25 19:45:50 

标签:java,this,super,用法

前言:理论代码必须结合起来才能真正的掌握

一、this

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

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

  • 最常见的情况是是对象的一个属性或被构造器的参数屏蔽时,如果需要调用屏蔽的属性,this就代表哪个对象

  • this只能在方法内使用,表示正在调用方法的那个对象,但是,如果在方法内调用同一个类的另一个方法,就不必使用this,直接调用即可,this关键字是能省则省

  • this和static的关系:

  • static方法是类方法,依附于类而不依赖与任何对象,static属性是指该属性是类中所有对象所共享的,static方法是类方法,先于任何实例(对象)存在,static在类加载时就已经存在了,但对象是在创建时才生成;方法中使用this关键字它的值是当前对象的引用,只能用它调用属于当前对象的属性和方法和。但是,this可以调用static类型的属性,举个例子:一个父亲是不可能向他还未出生的孩子借钱的,但孩子出生后完全可以找他父亲去借钱;

  • 对于一个类中的this,不一定单指这个的对象,也可能是这个类的子类的对象(抽象类里面的this只能是实际调用中它的派生类的实例化对象);

  • 总之:如果new 父类对象的话,父类方法的this指向的是父类,如果new 子类,那么父类方法的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("wzh2",20,"chinese3");
 System.out.println(student.toString());
 //扩展,全局变量nation的值最后是chinese3
 System.out.println(Person.nation);
}
}

结果:

Java中超详细this与super的概念和用法

二、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 = "wzh2";

@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;
}
}

输出结果

Java中超详细this与super的概念和用法

子类获取到父类的变量


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
}

}

来源:https://blog.csdn.net/wwwwwww31311/article/details/113281877

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com