怎么用公共类中的方法输出类类型中的属性

公共类是Computer,类类型是CPU和RAM,怎么用show方法输出类类型中的属性

img


img

img

cpu.getSpeed(), ram.getCapacity()


public class Computer {
    //这两个是作为属性而不是内部类在里面
    private CPU cpu;

    private RAM ram;

    public void setCpu(CPU cpu){
        this.cpu = cpu;
    }

    public void setRam(RAM ram){
        this.ram = ram;
    }

    public void show(){
        System.out.println("这个电脑的转速是:"+cpu.getSpeed()+", 容量是:"+ram.getCapacity());
    }

    public static void main(String[] args) {
        CPU cpu = new CPU(2400);
        RAM ram = new RAM(8);
        Computer computer = new Computer();
        computer.setCpu(cpu);
        computer.setRam(ram);
        computer.show();
    }
}

class CPU{
   private double speed;

   public CPU(double speed){
       this.speed = speed;
   }

   public double getSpeed(){
       return speed;
   }
}

class RAM {
    private double capacity;

    public RAM(int capacity){
        this.capacity = capacity;
    }

    public double getCapacity(){
        return capacity;
    }

}