类的内部使用this表示当前对象的引用。

在一个类的内部使用this表示当前对象的引用,

然而有时类的方法内部还有类的定义需要使用外部类的方法时就需要使用this对象,

此时this只是内部使用类的对象,在内部类方法定义的类中如何引用外部类此时就要用到类名.this方法。

Kotlin里用 this@类名 使用外部类实例

类名.this表示外部类的实例。

Class classA {

	private void main(){
		this.methodA(0) //表示当前对象
	}

	 new Thread(){
		@overide
		public void run(){
			classA.this.methdA(1);//外部类对象  
			AsyncDummy asyncDummy = new asyncDummy(classA.this::callbackA)//当需要来访问类的构造方法,对象方法,静态方法时,用关键字::             执行用.   引用用::
		}
	}

	private void  methodA(int xx){
		//do some thing
	}

	private void  callbackA(int xx){
		//do some thing
	}
}

 

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐