this的使用

mac2022-06-30  81

1.this表示的是new出来的对象

2.在编译时:this表示当前类的对象

   在运行时:表示运行类的对象

代码

package thisTest; /** * * this的使用方法 * */ public class Demo_01 { public void init(){ System.out.println("3 init"); this.demo(); } public void demo(){ System.out.println("4 demo"); } public static void main(String[] args) { Demo_01 demo = new Demo_01(); demo.init(); } } package thisTest; /** * * this的使用方法 * this指的是new 的那个对象 * * */ public class Demo_02 extends Demo_01{ public void init(){ super.init(); System.out.println("1 init"); this.demo(); } public void demo(){ System.out.println("2 demo"); } public static void main(String[] args) { Demo_02 demo = new Demo_02(); demo.init(); } }

运行结果:

 

转载于:https://www.cnblogs.com/itcx1213/p/8124081.html

最新回复(0)