java基础 this和super使用

this

在方法中,可以使用一个关键词this来表示这个对象本身。在普通方法中,this表示调用这个方法的对象;在构造方法中,this表示新创建的对象。

super

super是指父类,super在类继承中有重要作用

this的使用

 1,使用this来访问字段及方法

可以使用this来访问字段和方法

String name="milo";
   public void hello(){
		System.out.println("Hello My name is "+name);
		System.out.println("Hello My name is "+this.name);
	}

控制台结果:Hello My name is milo  Hello My name is milo 含义是相同的

2,使用this来解决局部变量与字段同名的问题

public class Main
{
   String name="milo";
    public void hello(String name){
		System.out.println("Hello My name is "+name);
		//这里的name 是参数变 输出zom
		System.out.println("Hrllo My name is "+this.name);
		//这里的this.name是字段变量 输出milo
	}
	public static void main(String[] args)
	{
		Main ji=new Main();
		ji.hello("zom");
	}
}
 

3,构造方法中,用this调用另一构造方法

public class Main
{
    public void hello(){
		this.hello("nji",8);
		/*调用另一个 hello 方法,
		 使用时必须放在第一句
		*/
	}
	public  void hello(String name,int k){
		System.out.println("Hello My name is "+ name);
	}
	public static void main(String[] args)
	{
		
		Main ji=new Main();
		ji.hello();
	}
}
 

注:使用this时,要注意this指的是“对象”实例本身

super的使用

注:正是由于继承,使用this可以访问父类的字段和方法。但有时为了明确的指明父类的字段和方法,就要使用关键字super。在这个意义上,this和super并不是两个对象,而是一个对象。

class Data{
	String sayhello="my";
	public void data(){
	}
}
class Person extends Data{
	public  void ji(int o){
		System.out.println(sayhello);
		System.out.println(this.sayhello);
		System.out.println(super.sayhello);
		/*
		 在子类 person 中用sayhello,this.sayhello,
		 super.sayhello来访问sayhello是完全一样的。
		*/
	}
}
public class Main
{
	public static void main(String[] args)
	{
		Person ui=new Person();
		ui.ji(18);
	}
}
 

使用super不能访问在子类中添加的字段和方法

有时使用super来区别同名的字段和方法。如,使用super可以访问被子类所隐藏的同名变量。当覆盖父类的同名方法的同时,又要调用父类的方法,就必须使用super

class Data{
	String sayhello="my";
	public void data(){
	}
}
class Person extends Data{
	public  void ji(int o){
		String sayhello="ok";
		System.out.println(sayhello);
		System.out.println(this.sayhello);
		System.out.println(super.sayhello);
		/*
		 结果分别为
		 ok my my
		*/
	}
}
public class Main
{
	public static void main(String[] args)
	{
		Person ui=new Person();
		ui.ji(18);
	}
}
class Data{
	String sayhello="my";
	public String data(){
		return"Data 中的 data方法";
	}
}
class Person extends Data{
	public String data(){
		return "Person 中的 data方法";
	}
	public  void ji(int o){
		System.out.println(data());
		System.out.println(this.data());
		System.out.println(super.data());
		/*
		 结果分别为
		 Person 中的 data方法
		 Person 中的 data方法

		 Data 中的 data方法
		*/
	}
}
public class Main
{
	public static void main(String[] args)
	{
		Person ui=new Person();
		ui.ji(18);
	}
}
 

从这里可以看出,即使同名,也仍然可以使用父类的方法,这也使得在覆盖父类的方法的同时,又利用已定义好的父类方法。这也是继承带来的一个好处。

使用父类的构造方法

 子类在构造方法中,可以使用super来调用父类的构造方法

使用时,super()必须放在第一句。

注:使用super时,要注意super与this一样指的是调用“对象”本身

1,通过super不仅可以直接访问父类中定义的字段和方法,还可以访问间接父类中定义的字段和方法。

2,由于它指的是对象实例,所以super和this一样,不能在static环境中使用,包括类变量(static,field),类方法(static method),static语句块。

本篇文章多处选自唐大什老师的《java程序设计》第三版

可能有地方写的不对,希望各位嘴下留情,评论区指出


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@*SHAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值