对初始化“类”的理解

一、"类"嵌套的情景演示

二、给"MobilePhoneConfiguration"初始化值

三、思想解析:

四、"嵌套类"的代码块

class MobilePhoneConfiguration {
    private String phoneMobile;
    private String phoneType;
    private String phoneOperatingSystem;
    private int phonePrice;
    private int phoneInternal;
    private PhoneFunction phoneFunction;

    public MobilePhoneConfiguration(String phoneMobile,
                                    String phoneType, String phoneOperatingSystem,
                                    int phonePrice, int phoneInternal,
                                    PhoneFunction phoneFunction) {
        this.phoneMobile = phoneMobile;
        this.phoneType = phoneType;
        this.phoneOperatingSystem = phoneOperatingSystem;
        this.phonePrice = phonePrice;
        this.phoneInternal = phoneInternal;
        this.phoneFunction = phoneFunction;
    }

    @Override
    public String toString() {
        return "MobilePhoneConfiguration{" +
                "phoneMobile='" + phoneMobile + '\'' +
                ", phoneType='" + phoneType + '\'' +
                ", phoneOperatingSystem='" + phoneOperatingSystem + '\'' +
                ", phonePrice=" + phonePrice +
                ", phoneInternal=" + phoneInternal +
                ", phoneFunction=" + phoneFunction +
                '}';
    }

    public String getPhoneMobile() {
        return phoneMobile;
    }

    public void setPhoneMobile(String phoneMobile) {
        this.phoneMobile = phoneMobile;
    }

    public String getPhoneType() {
        return phoneType;
    }

    public void setPhoneType(String phoneType) {
        this.phoneType = phoneType;
    }

    public String getPhoneOperatingSystem() {
        return phoneOperatingSystem;
    }

    public void setPhoneOperatingSystem(String phoneOperatingSystem) {
        this.phoneOperatingSystem = phoneOperatingSystem;
    }

    public int getPhonePrice() {
        return phonePrice;
    }

    public void setPhonePrice(int phonePrice) {
        this.phonePrice = phonePrice;
    }

    public int getPhoneInternal() {
        return phoneInternal;
    }

    public void setPhoneInternal(int phoneInternal) {
        this.phoneInternal = phoneInternal;
    }

    public PhoneFunction getPhoneFunction() {
        return phoneFunction;
    }

    public void setPhoneFunction(PhoneFunction phoneFunction) {
        this.phoneFunction = phoneFunction;
    }
}

//-------------------------------------------------------

class PhoneFunction {
    private String phoneCall;
    private String phoneGame;
    private String phoneMusic;

    public PhoneFunction(String phoneCall, String phoneGame, String phoneMusic) {
        this.phoneCall = phoneCall;
        this.phoneGame = phoneGame;
        this.phoneMusic = phoneMusic;
    }

    @Override
    public String toString() {
        return "PhoneFunction{" +
                "phoneCall='" + phoneCall + '\'' +
                ", phoneGame='" + phoneGame + '\'' +
                ", phoneMusic='" + phoneMusic + '\'' +
                '}';
    }

    public String getPhoneCall() {
        return phoneCall;
    }

    public void setPhoneCall(String phoneCall) {
        this.phoneCall = phoneCall;
    }

    public String getPhoneGame() {
        return phoneGame;
    }

    public void setPhoneGame(String phoneGame) {
        this.phoneGame = phoneGame;
    }

    public String getPhoneMusic() {
        return phoneMusic;
    }

    public void setPhoneMusic(String phoneMusic) {
        this.phoneMusic = phoneMusic;
    }
}

//-------------------------------------------------------

class TestMobilePhoneConfiguration {

    public static void main(String[] args) {
        MobilePhoneConfiguration mobilePhoneConfiguration[] = new MobilePhoneConfiguration[3];
//初始化过程
        mobilePhoneConfiguration[0] = new MobilePhoneConfiguration("XiaoMi",
                "Utral",
                "安卓系统",
                4000,
                128,
                new PhoneFunction("打电话", "打游戏", "听音乐"));

        mobilePhoneConfiguration[1] = new MobilePhoneConfiguration("HuaWei",
                "Utral",
                "鸿蒙系统",
                8000,
                256,
                new PhoneFunction("打电话", "打游戏", "听音乐"));

        mobilePhoneConfiguration[2] = new MobilePhoneConfiguration("Apple",
                "IOS系统",
                "苹果",
                5000,
                256,
                new PhoneFunction("打电话", "打游戏", "听音乐"));

        System.out.println("手机配置:");
        for (MobilePhoneConfiguration m : mobilePhoneConfiguration) {
            System.out.println(Arrays.asList(m));
        }

    }
}

posted on 2022-12-05 23:06  陈嘻嘻-  阅读(35)  评论(0)    收藏  举报

导航