Penggunaan Nested Class dalam java
program dibawah adalah contoh penggunaan nested class dengan menggunakan bahasa java.
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
class SecondLevel{
public int x= 3;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("this.first.x = " + FirstLevel.this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
ShadowTest.FirstLevel.SecondLevel ud = fl.new SecondLevel();
ud.methodInFirstLevel(23);
}
}
jika program diatas dicompile, maka akan ada class tambahan,
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
class SecondLevel{
public int x= 3;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("this.first.x = " + FirstLevel.this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
ShadowTest.FirstLevel.SecondLevel ud = fl.new SecondLevel();
ud.methodInFirstLevel(23);
}
}
jika program diatas dicompile, maka akan ada class tambahan,
kira-kira seperti pada gambar diatas!
Komentar
Posting Komentar