如何使用toString和getter以及setter方法

时间:2015-11-08 20:29:32

标签: java string override

我按照下面的说明骑着奋车。我无法弄清楚如何使用toString()方法来打印我的数据值。我也不知道如何将颜色打印成字符串来表示" Black"或"蓝"。而且我无法弄清楚如何使用布尔值来表示" connected"或"断开"。

  

创建一个名为HeadPhone的Java类来表示耳机集。该课程包含:

     

•三个常量,分别为LOW,MEDIUM和HIGH,值为1,2和3   表示耳机音量。

     

•名为volume的私有int数据字段,指定的卷   耳机默认音量为MEDIUM。

     

•名为pluggedIn的私有布尔数据字段,用于指定是否为   耳机已插入。默认值为false。

     

•名为manufacturer的私有String数据字段,用于指定   耳机制造商的名称。

     

•名为headPhoneColor的私有颜色数据字段,用于指定   耳机的颜色。

     

•所有数据字段的getter和setter方法。

     

•无参数构造函数,用于创建默认耳机。

     

•一个名为toString()的方法,它返回一个描述该字符串的字符串   耳机的当前字段值。

     

•名为changeVolume(value)的方法,用于更改音量   耳机到传递给方法的值

     

创建一个构造至少3个HeadPhone的TestHeadPhone类   对象。

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 请将Head Phones插入设备。

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

要求输出 制造商:Bose 颜色:黑色 目前:断开连接 音量设置为:MEDIUM 请将Head Phones插入设备。

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

3 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:1)

如果覆盖toString()方法最终无法用于项目,则始终可以使用其原始值有条件地显式格式化显示字符串。即。

System.out.println("Currently: " + (headPhone.getStatus() ? "connected" : "disconnected"));
...

请注意每次要在代码的其他部分打印status时需要执行此操作的问题。覆盖toString(),无处不在。

答案 2 :(得分:-1)

看看这段代码。这可能有所帮助。

public class TestHeadPhones {

/**
 * @param args
 */
public static void main(String[] args) {
    HeadPhones h1 = new HeadPhones();
    h1.setVolume(2);
    h1.setHeadPhoneColor("CYAN");
    h1.setManufacturer("Bass");
    h1.setPluggedIn(true);
    HeadPhones h2 = new HeadPhones();
    h2.setVolume(1);
    h2.setHeadPhoneColor("blue");
    h2.setManufacturer("Bass");
    h2.setPluggedIn(true);
    HeadPhones h3 = new HeadPhones();
    h3.setVolume(HeadPhones.HIGH);
    h3.setHeadPhoneColor("DARK GRAY");
    h3.setManufacturer("Bass");
    h3.setPluggedIn(true);

    // Print description of all headphones
    System.out.println("Description of Headphone 1");
    System.out.println(h1.toString() + "\n");
    System.out.println("Description of Headphone 2");

    System.out.println(h2.toString() + "\n");
    System.out.println("Description of Headphone 3");

    System.out.println(h3.toString() + "\n");


    //change volume of headphone 1
    h1.changeVolume(3);
    System.out.println("Description of Headphone 1");
    System.out.println(h1.toString() + "\n");

}

}

这是HeadPhones类

public class HeadPhones {
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
private int volume = MEDIUM;
private boolean pluggedIn = false;
private String manufacturer;
private String headPhoneColor;

/**
 * Default Constructor
 */
public HeadPhones() { 

}

/***
 * Change the voulme
 * @param value
 */
public void changeVolume(int value) {
    setVolume(value);
}

/**
 * Return Description of Object
 */
public String toString() {
    return "Volume: " + getVolume() + "\n" + "Plugin is set: "
            + isPluggedIn() + "\n" + "Color of HeadePhone: "
            + getHeadPhoneColor() + "\n" + "Manufacturer: "
            + getManufacturer();
}

/**
 * Set volume
 * @param volume
 */
public void setVolume(int volume) {
    this.volume = volume;
}

/***
 * Get Volume
 * @return volume
 */
public int getVolume() {
    return volume;
}

/**
 * Set plugin
 * @param pluggedIn
 */
public void setPluggedIn(boolean pluggedIn) {
    this.pluggedIn = pluggedIn;
}

/***
 * Get Plugin is true or false
 * @return pluggedIn
 */
public boolean isPluggedIn() {
    return pluggedIn;
}

/***
 * Set Manufacture
 * @param manufacturer
 */
public void setManufacturer(String manufacturer) {
    this.manufacturer = manufacturer;
}

/***
 * Get Manufacture
 * @return manufacturer
 */
public String getManufacturer() {
    return manufacturer;
}

/***
 * Set the Color
 * @param headPhoneColor
 */
public void setHeadPhoneColor(String headPhoneColor) {
    this.headPhoneColor = headPhoneColor;
}

/**
 * This method will return the color
 * @return headPhoneColor
 */
public String getHeadPhoneColor() {
    return headPhoneColor;
}

}
相关问题