*****this******

表示对当前对象的引用。

作用:1.区分实例变量和局部变量(this.name----->实例变量name)

   2.将当前对象当做参数传递给其它对象和方法。利用this可使用当前对象的方法或域。

  this应该在类的实例方法中使用。

this()方法:

  代表的是一个构造方法对其它重载的构造方法的调用。但this必须放在构造方法的第一行,它前面不能再有其它语句。

public class A {
	
	private String name;
	private int salary;
	
	public A(String n,int s){
		//以下的两种写法都是对的!
		//name = n;
		//salary = s;
		this.name = n;
		this.salary =s;
	}
	
	public A(String n){
		this(n,0);
	}
	public A(){
		//int a = 0;//×  this 必须在第一行!
		this("unknown");
	}
	
	public static void main(String[] args){
		
	}
}

 ****super****

  • 表示当前对象的直接父类,代表了父类对象的一个引用,作用是利用super使用父类的方法或域。
  • 任何时候,一个子类需要引用它直接的超类时,都可用关键字super来实现。
  • 调用超累的构造方法,当一个子类调用super时,它调用它的直接超类的构造方法。super()必须是子类构造方法的第一个执行语句。
  • 用来访问被子类成员隐藏的超类成员。super 指这个对象的父类。super可以用来引用父类中(被覆盖的)方法和(被隐藏的)变量。

 

相关文章:

  • 2021-08-21
  • 2021-12-27
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
  • 2021-09-13
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-06-19
  • 2022-12-23
相关资源
相似解决方案