使用对象变量创建字符串

时间:2019-07-02 19:06:59

标签: java

我需要使用“ Guitar”类对象(gibsonDefault,martinGuitar)中的变量创建toString方法,这些变量包括:guitarLength,guitarManufacturer,numStrings和guitarColor。是否使用吉他对象的名称或使用吸气剂和吸气剂来构建琴弦?我不确定从哪里开始。任何建议表示赞赏。

public class Guitar {

    private int numStrings; // number of strings on the guitar
    private double guitarLength; // guitar's length in inches
    private String guitarManufacturer; // guitar manufacturer
    private String guitarColor; // defines guitar color
    public String[] toString;


    Guitar() { // constructor containing default values
        numStrings = 6;
        guitarLength = 28.2;
        guitarManufacturer = "Gibson";
        guitarColor = "Green";
    } 


    Guitar(int ns, double gl, String gm, String gc) { // constructor containing values for new guitar types
        int numStrings = ns ;
        double guitarLength = gl;
        String guitarManufacturer = gm;
        String guitarColor = gc;
    }

    public int getNumStrings() {
        return numStrings;
    }
    public double getGuitarLength() {
        return guitarLength;
    }
    public String getGuitarManufacturer() {
        return guitarManufacturer;
    }
    public String getGuitiarColor() {
        return guitarColor;
    }


}

class Main {
    public static void main(String[] args) {

        Guitar gibsonDefault = new Guitar(6, 28.2, "Gibson", "Green"); // default guitar object
        Guitar martinGuitar = new Guitar(5, 26.5, "Martin", "Green"); // new guitar object


    }
}

0 个答案:

没有答案