this与super的使用与区别

一、this应用

this: 当前对象,也就是谁调用this所在方法,this就代表谁,打印this时,打印的是当前对象的地址

this的用法: this调属性,this调方法 , this调构造方法

this调属性

//案例:手机打电话
//分析:类-Phone  对象-new对象  方法-call  属性-brand
class Phone{
	String brand;
	
	//带参构造的规范:参数名和属性名要一致--值为null;因为局部优先
	public Phone(String brand) { 
		this.brand = brand; //this调属性-谁调用this所在的方法this就代表谁
	}
	public void call() {
		System.out.println(brand+"手机正在打电话~");
	}
}
public class Test2 {
	public static void main(String[] args) {
		Phone phone2 = new Phone("OPPO");
		//phone2.brand = "OPPO";  //等价this.brand=brand
		phone2.call();
	}
}

this调方法

//this调方法案例:张三在写代码,也在打游戏
//类-Man  对象-张三    属性-姓名   方法:write,play
class Man{
	String name;
	public void write() {
		System.out.println(name+"正在写代码");
		this.play(); //this代表main方法中的对象   this.方法
	}
	
	public void play() {
		System.out.println(name+"正在玩游戏");
	}
}
public class Test2 {
	public static void main(String[] args) {
		Man man = new Man();
		man.name = "张三";
		man.write();
		//man.play();
	}
}

this调构造方法

this(): 必须放在构造方法的首句-不常用 this调构造只能写一句,因为都要在首句

//this调构造方法案例:this()-调无参构造  this(参数)-调带参构造
//案例:女朋友帮忙洗衣服
class Girl{
	String name;  
	int    age;
	public Girl() {
		System.out.println("无参...");
	}
	public Girl(String name) {
		this.name = name;
	}
	public Girl(String name,int age) {

		this(name); //this(带参)-不常用
		//this.name = name;
		this.age  = age;
	}
	
	public void wash() {
		System.out.println(age+"岁的女朋友"+name+"正在帮忙洗衣服");
	}
}
public class Test3 {
	public static void main(String[] args) {
		Girl fj = new Girl("凤姐", 18);
		fj.wash();
	}
}

二、super用法

super表示父类对象;和this有点类似,只是this表示当前对象

在应用上也和this类似;super可以调用属性,方法,构造方法

super VS this

  1. this:
  • this调属性:调当前对象的属性;如果当前类没有,则根据继承性,调父类属性
  • this调方法:调当前对象的方法;如果当前类没有,则根据继承性,调父类方法
  • this调构造方法:调当前类的构造方法
  1. super:
  • super调属性:调父类的属性
  • super调方法:调父类的方法
  • super调构造方法:调父类的构造方法

super调属性

super调属性:调父类的属性

class Person{
	String name="刘亦菲";
}
class Student extends Person{
    String name="凤姐";
	public void show() {
		System.out.println(name);       //凤姐-先调自身,自身没有才调父类属性
		System.out.println(this.name);  //凤姐-先调自身,自身没有才调父类属性
		System.out.println(super.name); //刘亦菲-调父类属性
	}
}
public class Test1 {
	public static void main(String[] args) {
		new Student().show();
	}
}

super调方法

super调方法:调的是父类的方法

class Animal{
	public void show() {
		System.out.println("父类的show方法");
	}
}
class Dog extends Animal{
	@Override
	public void show() {
		super.show();  //先调父类的方法
		
		System.out.println("子类的show方法");
	}
}
public class Test2 {
	public static void main(String[] args) {
		new Dog().show();
	}
}

super调构造方法

在构造方法中,默认首句会出现super(); 先调用父类无参构造。

为什么这么设计?

原因是,实例化对象的过程是父类资源+子类资源;否则就不会有继承的特点(调父类属性和方法)

注意: this和super在构造方法中不能共存

class A{
	public A() {
		System.out.println("父类A的无参构造");
	}
	public A(int a) {
		System.out.println("父类A的带参构造");
	}
}
class B extends A{
	public B() {
		//super();
		System.out.println("子类B的无参构造");
	}
	public B(int a) {
		//super(a);  //调父类带参   this和super在构造方法中不能共存
		this();      //A无参   B无参   B带参
		System.out.println("子类B的带参构造");
	}
}
public class Test3 {
	public static void main(String[] args) {
		//new B();   //父类A无参; 子类B无参
		new B(66);   //父类A无参; 子类B带参
	}
}

扩展知识

  1. 解析为什么this和super在构造方法中不能共存

    答:

    一个构造方法的首句代码只能调用一个this()或super(),不能在构造方法中第一行使用this(),第二行中写super(),反过来也不行,因为已经规定了this()或super()只能放在构造方法的第一行。

  2. 为什么this.toString()super.toString()this.getClass()super.getClass()打印出的结果为一致?

答:

在Java中, superthis 代表不同的含义和用法。
super 关键字用于在子类中引用父类的成员变量、方法和构造函数。它可以用来调用父类的构造函数,访问父类的成员变量或方法。当使用 super 关键字时,它引用的是父类的实例。
this 关键字用于引用当前对象的成员变量、方法和构造函数。它可以用来访问当前对象的成员变量或方法,以及调用当前对象的构造函数。当使用 this 关键字时,它引用的是当前对象的实例。
在Java中,无论是 super 还是 this 关键字,它们都引用的是对象的实例,因此它们的地址值是相同的。当调用 getClass() 方法时,它返回的是对象的运行时类型,因此也会相同。
需要注意的是, superthis 关键字的使用场景和含义是不同的,虽然它们的地址值和 getClass() 值可能相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值