// A top-level class
class OuterMost{
private String str = new String("OuterMost");
public void printName(){
System.out.println("Class name: "+ str);
}
//A static class
static class StaticNestedOne{
static String str = new String("StaticNestedOne");
static void printName(){
System.out.println("Class name: "+str);
}
static class StaticNestedTwo{
static String str = new String("StaticNestedTwo");
static void printName(){
System.out.println("Class name: "+str);
}
}//End of StaticNestedTwo
} //End of StaticNestedOne
//An inner class
class InnerOne{
public String str1 = new String("InnerOne");
//An interface within an inner class
public interface IDPrinter {
static final int BASE_VALUE=100;
public void printID();
}
public void printName(){
System.out.println("Class name: "+ this.str1 );
}
//Nested inner class that implements an interface
class InnerNestedOne implements IDPrinter{
int id;
protected String str = new String("InnerNestedOne");
InnerNestedOne(int id){
this.id = id;
}
public void printName(){
System.out.println("Class name: "+ this.str);
}
public void printID(){
System.out.println(BASE_VALUE+id);
}
public void printAllClasses(){
printName(); //local method
InnerOne.this.printName(); //enclosing class method
//creates an object using a private
//inner class in the outer scope
InnerTwo innerTwo = new InnerTwo();
innerTwo.printName();
OuterMost.this.printName(); //top level method
StaticNestedOne.printName();
StaticNestedOne.StaticNestedTwo.printName();
}
}// End of InnerNestedOne
}//End of InnerOne
// A private inner class
private class InnerTwo{
public String str = new String("InnerTwo");
public void printName(){
System.out.println("Class name: "+ this.str);
}
}// End of InnerTwo
}//End of OuterMost
//A class that uses OuterMost and it's nested and inner classes
class Outside{
public static void main(String arg[]){
OuterMost.StaticNestedOne.printName();
OuterMost.StaticNestedOne.StaticNestedTwo.printName();
OuterMost outer = new OuterMost();
OuterMost.InnerOne inner = outer. new InnerOne();
inner.printName();
OuterMost.InnerOne.InnerNestedOne innerNested = inner. new InnerNestedOne(10);
innerNested.printName();
innerNested.printAllClasses();
}
}// End of Outside
No comments:
Post a Comment