java内部类(内部静态类)使用

java内部类

public class PublicClass {
    private String userName;
    private String uersPassword;

    class PrivateClass{
        private String age;
        private String address;

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUersPassword() {
        return uersPassword;
    }

    public void setUersPassword(String uersPassword) {
        this.uersPassword = uersPassword;
    }
}

调用测试类

public class Run {
    public static void main(String args[]){
        PublicClass publicClass = new PublicClass();
        publicClass.setUserName("miss");
        publicClass.setUersPassword("word");

        PublicClass.PrivateClass privateClass = publicClass.new PrivateClass();
        privateClass.setAge("18");
        privateClass.setAddress("beijing");

        System.out.println(publicClass.getClass());
        System.out.println(publicClass.getUserName());
        System.out.println(publicClass.getUersPassword());

        System.out.println(privateClass.getClass());
        System.out.println(privateClass.getAge());
        System.out.println(privateClass.getAddress());

    }
}

说明:如果PrivaeClass为静态内部类,则调用测试类实例化内部类的方法为:

PublicClass.PrivateClass privateClass = new PublicClass.PrivateClass();



你可能感兴趣的:(java代码)