Day10
类中属性定义
public class AccessControl { public String public_str = "public_str"; protected String protectes_str = "protectes_str"; String defalut_str = "defalut_str"; private String private_str = "private_str"; public static void main(String[] args) { AccessControl control = new AccessControl(); System.out.println(control.public_str); System.out.println(control.protectes_str); System.out.println(control.defalut_str); System.out.println(control.private_str); } }
成员内部类
public class MemberOuterClass { //属性 static String name; int age; //方法 public void run() { MemberInnerClass innerClass = new MemberInnerClass(); System.out.println(innerClass.name); innerClass.run("tom"); } public static void eat() { } class MemberInnerClass{ //属性 String name; int age; //方法 public void run(String name) { System.out.println(name); System.out.println(this.name); System.out.println(MemberOuterClass.name); System.out.println(MemberOuterClass.this.age); MemberOuterClass.this.run(); MemberOuterClass.eat(); } public void eat() { } } }
静态内部类
public class StaticOuterClass { //属性 static String name; int age; //方法 public void run() { MemberInnerClass innerClass = new MemberInnerClass(); System.out.println(innerClass.name); System.out.println(MemberInnerClass.age); innerClass.run("1"); MemberInnerClass.eat(); } public static void eat() { } static class MemberInnerClass{ //属性 String name; static int age; //方法 public void run(String name) { System.out.println(name); System.out.println(this.name); System.out.println(MemberInnerClass.age); System.out.println(StaticOuterClass.name); //System.out.println(StaticOuterClass.this.age); StaticOuterClass.eat(); MemberInnerClass.eat(); } public static void eat() { } } }
Test类
//import com.briup.day10.MemberOuterClass.MemberInnerClass; import com.briup.day10.StaticOuterClass.MemberInnerClass; public class InnerClassTest { public static void main(String[] args) { //使用成员内部类 //MemberOuterClass outerClass = new MemberOuterClass(); //MemberInnerClass innerClass = outerClass.new MemberInnerClass(); //innerClass.run("tom"); //System.out.println(innerClass.name); //使用静态内部类 System.out.println(MemberInnerClass.age); MemberInnerClass.eat(); MemberInnerClass innerClass = new MemberInnerClass(); System.out.println(innerClass.name); innerClass.run("1"); } }