用java构造函数实现“组装”一台计算机。可以为计算机进行开机、关机、待机等操作,并用计算机的状态性表明。

下面是一个简单的 Java 类来模拟组装计算机、进行开机、关机、待机等操作,并用状态变量表示计算机的状态:

public class Computer {
    private String name;
    private boolean isOn;
    private boolean isSleeping;

    public Computer(String name) {
        this.name = name;
        this.isOn = false;
        this.isSleeping = false;
    }

    public void powerOn() {
        if (!this.isOn) {
            System.out.println("Turning on " + this.name + "...");
            this.isOn = true;
            this.isSleeping = false;
            System.out.println(this.name + " is now on.");
        } else {
            System.out.println(this.name + " is already on.");
        }
    }

    public void powerOff() {
        if (this.isOn) {
            System.out.println("Turning off " + this.name + "...");
            this.isOn = false;
            this.isSleeping = false;
            System.out.println(this.name + " is now off.");
        } else {
            System.out.println(this.name + " is already off.");
        }
    }

    public void sleep() {
        if (this.isOn) {
            System.out.println(this.name + " is going to sleep...");
            this.isOn = false;
            this.isSleeping = true;
            System.out.println(this.name + " is now sleeping.");
        } else {
            System.out.println(this.name + " is not on.");
        }
    }

    public void wakeUp() {
        if (this.isSleeping) {
            System.out.println(this.name + " is waking up...");
            this.isOn = true;
            this.isSleeping = false;
            System.out.println(this.name + " is now on.");
        } else {
            System.out.println(this.name + " is not sleeping.");
        }
    }

    public String getStatus() {
        if (this.isOn) {
            return this.name + " is on.";
        } else if (this.isSleeping) {
            return this.name + " is sleeping.";
        } else {
            return this.name + " is off.";
        }
    }
}

通过以上代码,我们可以构造一台计算机并进行开机、关机、待机等操作,示例如下:

java
Copy code
Computer myComputer = new Computer("My Computer");
System.out.println(myComputer.getStatus()); // My Computer is off.
myComputer.powerOn(); // Turning on My Computer... My Computer is now on.
System.out.println(myComputer.getStatus()); // My Computer is on.
myComputer.sleep(); // My Computer is going to sleep... My Computer is now sleeping.
System.out.println(myComputer.getStatus()); // My Computer is sleeping.
myComputer.wakeUp(); // My Computer is waking up... My Computer is now on.
System.out.println(myComputer.getStatus()); // My Computer is on.
myComputer.powerOff(); // Turning off My Computer... My Computer is now off.
System.out.println(myComputer.getS
// it分享群:439042787
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值