gpt4 book ai didi

java - 如何使用 toString 和 getter 和 setter 方法

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:09:18 24 4
gpt4 key购买 nike

我按照以下说明乘坐奋斗巴士。我不知道如何使用 toString() 方法来打印我的数据值。我也不知道如何将颜色打印为表示“黑色”或“蓝色”的字符串。而且我不知道如何使用 boolean 值来表示“已连接”或“已断开连接”。

Create a Java class named HeadPhone to represent a headphone set. The class contains:

• Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.

• A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.

• A private boolean data field named pluggedIn that specifies if the headphone is plugged in. The default value if false.

• A private String data field named manufacturer that specifies the name of the manufacturer of the headphones.

• A private Color data field named headPhoneColor that specifies the color of the headphones.

• getter and setter methods for all data fields.

• A no argument constructor that creates a default headphone.

• A method named toString() that returns a string describing the current field values of the headphones.

• A method named changeVolume(value) that changes the volume of the headphone to the value passed into the method

Create a TestHeadPhone class that constructs at least 3 HeadPhone objects.

public class TestHeadPhone {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// construct an object
HeadPhone headPhone = new HeadPhone();

System.out.println("Manufacturer: " + headPhone.getManufacturer());
System.out.println("Color: " + headPhone.getColor());
System.out.println("Currently: " + headPhone.getStatus());
System.out.println("Volume: " + headPhone.getVolume());
if(headPhone.getStatus() == false){
System.out.println("Please plug the Head Phones into a device.");
}

headPhone.setNewHeadphone();

System.out.println("\n" + "Manufacturer: " +
headPhone.getManufacturer());
System.out.println("Color: " + headPhone.getColor());
System.out.println("Currently: " + headPhone.getStatus());
System.out.println("Volume: " + headPhone.getVolume());
if(headPhone.getStatus() == true){
System.out.println("Currently playing classical music.");
}

}

}

package testheadphone;
// import color class
import java.awt.Color;

/**
*
* @author
*/
public class HeadPhone {
// class variables
private static final int LOW = 1;
private static final int MEDIUM = 2;
private static final int HIGH = 3;
private int volume = MEDIUM;
private boolean pluggedIn = false;
private String manufacturer;
private Color headPhoneColor;

//default constructor method
public HeadPhone(){
this.manufacturer = "Bose";
this.headPhoneColor = Color.black;
this.volume = MEDIUM;
this.pluggedIn = false;
} // end default constructor
// getter method
public String getManufacturer(){
return manufacturer;
}
// getter method
public Color getColor(){
return headPhoneColor;
}
// getter method
public int getVolume(){
return volume;
}
// getter method
public boolean getStatus(){
return pluggedIn;
}

public int changeVolume(int change){
volume = change;
return volume;
}
// setter method
public void setNewHeadphone(){
manufacturer = "JVC";
headPhoneColor = Color.blue;
pluggedIn = true;
volume = HIGH;
}

// @Override
// public String toString(){
// return "Head Phone 1 has the folllowing parameters: " + "\n" +
// "Manufacturer: " + this.manufacturer + "\n" + "Color: Black" +
// "\n" + "Volume is set to: " + this.volume + "\n" +
// "Currently: disconnected" + "\n" + "Please plug the Head Phone"
// + " into a device";
// }

}

我的输出:制造商:Bose颜色:java.awt.Color[r=0, g=0, b=0]当前:错误体积:2请将耳机插入设备。

制造商:JVC颜色:java.awt.Color[r=0,g=0,b=255]目前:真实体积:3目前正在播放古典音乐。

要求的输出:制造商:Bose颜色:黑色当前:断开连接音量设置为:中等请将耳机插入设备。

Head Phone 2 具有以下参数:制造商:JVC颜色:蓝色当前:已连接音量设置为:低当前播放古典音乐播放列表

最佳答案

您可以覆盖要打印的对象的 toString() 方法。然而,一些对象已经以人类可读的格式为您实现了它们的 toString() 方法。即 Color 类。

    ...
System.out.println("Color: " + headPhone.getColor().toString());
...

另一方面,您可以通过重写来自由指定对象应显示为 String 的格式。 (除非对可以/不能修改的内容有类限制,即 final 关键字。)

关于java - 如何使用 toString 和 getter 和 setter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com