纯手打,转载请附上本文网址!!!

Java中内置类和静态内置类存在应用,编写了一个示例以供参考,本例中的所有类都是在同一个包中的。

代码地址:https://github.com/xianzhixianzhixian/thread.git

内置类的实例化方法:

外置类类名.内置类类名  innerClass = 外置类对象名.new 内置类类名();

内置静态类实例化方法:

外置类类名.内置类类名  innerClass = 外置类类名.new 内置类类名();

PublicClass.java

package inner.staicclass;

/**
 * 内置类
 * @author: xianzhixianzhixian
 * @date: 2018-12-24 19:45
 */
public class PublicClass {

    private String username;
    private String password;
    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

PublicStaticClass.class

package inner.staicclass;

/**
 * 静态内置类
 * @author: xianzhixianzhixian
 * @date: 2018-12-24 19:46
 */
public class PublicStaticClass {

    static private String username;
    static private String password;
    static class PrivateStaticClass{
        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 static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        PublicStaticClass.username = username;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        PublicStaticClass.password = password;
    }
}

Run.java

package inner.staicclass;

/**
 * 内置类和静态内置类的使用
 * @author: xianzhixianzhixian
 * @date: 2018-12-24 19:49
 */
public class Run {

    public static void main(String[] args) {
        //内置类的使用
        PublicClass publicClass = new PublicClass();
        publicClass.setUsername("0u");
        publicClass.setPassword("0p");
        System.out.println(publicClass.getUsername()+" "+publicClass.getPassword());
        //内置类实例化方法
        PublicClass.PrivateClass privateClass = publicClass.new PrivateClass();
        privateClass.setAge("0a");
        privateClass.setAddress("0ad");
        System.out.println(privateClass.getAge()+" "+privateClass.getAddress());

        //静态内置类的使用
        PublicStaticClass publicStaticClass = new PublicStaticClass();
        publicStaticClass.setUsername("0us");
        publicStaticClass.setPassword("0ps");
        System.out.println(publicStaticClass.getUsername()+" "+publicStaticClass.getPassword());
        PublicStaticClass.PrivateStaticClass privateStaticClass = new PublicStaticClass.PrivateStaticClass();
        privateStaticClass.setAge("0as");
        privateStaticClass.setAddress("0ads");
        System.out.println(privateStaticClass.getAge()+" "+privateStaticClass.getAddress());
    }
}

运行结果:

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐