采用面向对象思想组装一台计算器

计算机的主要组成部分有:CPU、硬盘、内存

计算机类代码:

package com.computer;

public class Computer implements CPU,HardDisk,Memory{

    String brand;
    public String brand(String brand) {
        this.brand = brand;
        return brand;
    }

    String frequence;
    public String frequence(String frequence) {
        this.frequence = frequence;
        return frequence;
    }

    String capacity1;
    public String capacityY(String capacity) {
        this.capacity1 = capacity;
        return capacity;
    }

    String capacity2;
    public String capacityN(String capacity) {
        this.capacity2 = capacity;
        return capacity;
    }

    public void printer(){
        System.out.println("计算机的信息如下:");
        System.out.println("CPU的品牌是:"+brand+"主频为:"+frequence);
        System.out.println("硬盘容量是:"+capacity1);
        System.out.println("内存容量是:"+capacity2);
    }

}

  CPU接口代码:

package com.computer;

public interface CPU {

    public String brand(String brand);
    public String frequence(String brand);

}

硬盘接口代码:

package com.computer;

public interface HardDisk {

    public String capacityY(String capacity);

}

内存接口代码:

package com.computer;

public interface Memory {

    public String capacityN(String capacity);

}

测试类代码:

package com.computer;

public class Test {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.brand("Inter");
        computer.frequence("3.8GHz");
        computer.capacityY("3000GB");
        computer.capacityN("8GB");
        computer.printer();
    }
}

运行结果:

 采用面向对象思想组装一台计算器_第1张图片

 

转载于:https://www.cnblogs.com/yangwenxiang/p/10399057.html

你可能感兴趣的:(采用面向对象思想组装一台计算器)