package com.company;
class PC{
CPU cpu;
HardDisk HD;
void setCPU(CPU c) {
cpu=c;
}
void setHardDisk(HardDisk h){
HD=h;
}
void show(){
System.out.println("CPU的速度为:"+cpu.getspeed());
System.out.println("硬盘的容量为:"+HD.getAmount());
}
}
class CPU{
int speed;
void setSpeed(int m){
speed=m;
}
int getspeed(){
return speed;
}
}
class HardDisk{
int amount;
void setAmount(int m){
amount=m;
}
int getAmount(){
return amount;
}
}
public class Test{
public static void main(String agrs[]){
CPU cpu=new CPU();
cpu.setSpeed(2200);
HardDisk disk=new HardDisk();
disk.setAmount(200);
PC pc=new PC();
pc.setCPU(cpu);
pc.setHardDisk(disk);
pc.show();
}
}
笔记——在类的方法的形参为对象的例子(对象的组合与复用)
最新推荐文章于 2022-03-19 20:04:02 发布