📄 computer.java
字号:
/**
* <p> 类:Computer 实现一个表示计算机的类 </p>
* @author 肖磊
* @version 1.0
*/
public class Computer
{
// 以下为域成员定义------------------------------------------------
private String brand; // 品牌
private String color; // 颜色
private String CPU; // CPU型号
private int memSize; // RAM容量,单位M
private int HDSize; // 硬盘容量,单位G
private double price; // 价格
private boolean isStarted; // 计算机是否已打开,true-已启动
private boolean isSuspended; // 计算机是否被挂起,true-已挂起
// 以下是构造方法--------------------------------------------------
public Computer() // 无参构造方法
{
brand = "未知";
color = "未知";
CPU = "未知";
memSize = 0;
HDSize = 0;
price = 0.0;
isStarted = false;
isSuspended = false;
}
public Computer(String br,String co,String cp,int ms,int hs,double pr) // 有参构造方法
{
brand = br;
color = co;
CPU = cp;
memSize = ms>0 ? ms : 0;
HDSize = hs>0 ? hs : 0;
price = pr>0 ? pr : 0;
isStarted = false;
isSuspended = false;
}
// 以下是设置/返回域的值的方法-------------------------------------
public void setBrand(String s) { brand = s;}
public String getBrand() { return brand; }
public void setColor(String s) { color = s;}
public String getColor() { return color; }
public void setCPU(String s) { CPU = s;}
public String getCPU() { return CPU; }
public void setMemSize(int size) { memSize = size;}
public int getMemSize() { return memSize;}
public void setHDSize(int size) { HDSize = size;}
public int getHDSize() { return HDSize;}
public void setPrice(double pr) { price = pr;}
public double getPrice() { return price;}
public boolean isStarted() { return isStarted;}
public boolean isSuspended() { return isSuspended;}
// 以下是对计算机操作的方法----------------------------------------
public void switchPower() // 开/关电脑
{
isStarted = !isStarted;
}
public boolean switchSleep() // 挂起/恢复,返回true操作成功,false操作失败
{
if(isStarted) // 开机状态
{
isSuspended = !isSuspended;
return true;
}
else // 关机状态
return false;
}
// 以下为显示对象信息的方法----------------------------------------
public void showComputer() // 显示计算机的信息
{
System.out.println("计算机配置如下:");
System.out.println("\t品 牌: "+brand);
System.out.println("\t颜 色: "+color);
System.out.println("\t处 理 器: "+CPU);
System.out.println("\t内存容量: "+memSize+"M");
System.out.println("\t硬盘容量: "+HDSize+"G");
System.out.println("\t价 格: "+price+"元");
}
public void showStatus() // 显示计算机的信息
{
String status;
if(!isStarted) status = "关闭";
else
{
if(isSuspended) status = "开启 睡眠";
else status = "开启 正常";
}
System.out.println("计算机状态: "+status);
}
}
// 用于测试Computer类的类---------------------------------------------
class ComputerTester
{
public static void main(String args[])
{
Computer c1 = new Computer("DELL","RED","P IV 2.0G",512,80,7000.0);
c1.showComputer();
c1.switchPower();
c1.showStatus();
c1.switchSleep();
c1.showStatus();
c1.switchPower();
c1.showStatus();
}
}
// 程序结束 ---------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -