publicclass Wealthy ...{ public String answer ="Yes!"; publicvoid wantMoney() ...{ String answer ="No!"; System.out.println("Do you want to give me $1,000,000? > "+ answer); System.out.println("Would you like $1,000,000? > "+ this.answer); } publicstaticvoid main(String[] args) ...{ Wealthy w =new Wealthy(); w.wantMoney(); } } 运行结果: Do you want to give me $1,000,000?> No! Would you like $1,000,000?> Yes!
再来个复杂点的。
publicclass ThisPoint ...{ publicstaticvoid main(String[] args) ...{ A b =new B(); b.fb(); } } class A ...{ public A() ...{ } publicvoid fa() ...{ System.out.println("CLASS A :Function fa Runing......"); } publicvoid fb() ...{ System.out.println("CLASS A :Function fb Runing......"); fa(); System.out.println("CLASS A :Function fb Stop......"); } } class B extends A ...{ public B() ...{ } publicvoid fa() ...{ System.out.println("CLASS B :Function fa Runing......"); } publicvoid fb() ...{ System.out.println("CLASS B :Function fb Runing......"); super.fb(); System.out.println("CLASS B :Function fb Stop......"); } } 运行结果: CLASS B :Function fb Runing...... CLASS A :Function fb Runing...... CLASS B :Function fa Runing...... CLASS A :Function fb Stop...... CLASS B :Function fb Stop......
看代码说话,先来个简单的。public class Wealthy...{ public String answer = "Yes!"; public void wantMoney() ...{ String answer = "No!"; System.out.println("Do you want to give me $1,000,000