在构建一个变量已经具有默认值的构造函数时,我遇到很多问题,然后调用稍后在TestGuitar类中设置的新变量。我已经成功编译了程序并打印了默认值。我没有成功保留尝试在TestGuitar类的newGuitar变量中设置的新变量。我知道所有问题都来自我的构造函数。在这种情况下,我需要一个0 arg构造函数来创建默认吉他,并需要一个新的构造函数来创建非常特定的吉他。
我为默认吉他设置了一些私有变量。如果我从不将任何内容传递给TestGuitar类中的newGuitar变量,我希望程序返回默认变量。如果我确实传递了一些东西,即guitarLength 24.75,我希望我的程序返回该值。更进一步,我希望该新变量可由getter方法调用。我相信我有75%的解决方案,但我需要专门针对构造函数问题的帮助。我只是认为我对构造函数的概念不够了解,无法同时包含0 arg和特定的构造函数。
//File name: Guitar.java
//Autor: Michael Joy
//Date: 09/16/2018
//Purpose: Building and testing guitar objects
import java.awt.Color;
import java.util.*;
import java.util.Random;
class Guitar {
//defines the default values for our default guitar if we do not pass anything new to the object
private int numStrings = 6;
private double guitarLength = 28.2;
private String guitarManufacturer = "Gibson";
private Color guitarColor = Color.RED;
private static int i = 1;
private static Random rand = new Random();
//declaring the constructor
public Guitar (){
//all my problems coming from here
this.numStrings = numStrings;
this.guitarLength = guitarLength;
this.guitarManufacturer = guitarManufacturer;
this.guitarColor = guitarColor;
}
public Guitar (int strings, double length, String manufacturer, Color color){
//more problems here
strings = this.numStrings;
length = this.guitarLength;
manufacturer = this.guitarManufacturer;
color = this.guitarColor;
System.out.printf("toString: %s \n ", this);
}
public int getNumStrings() {
return numStrings;
}
public double getGuitarLength() {
return guitarLength;
}
public String getGuitarManufacturer() {
return guitarManufacturer;
}
public Color getGuitarColor(){
return this.guitarColor;
}
public static void playGuitar(){
String[] musicNotes = {"A", "B", "C", "D", "E", "F", "G"};
String[] musicDuration = {"(0.25)","(0.5)","(1)", "(2)","(4)"};
System.out.print("playGuitar: ");
for (i = 1; i < 17; i++){
int index1 = rand.nextInt(musicNotes.length);
int index2 = rand.nextInt(musicDuration.length);
System.out.print(musicNotes[index1]);
System.out.print(musicDuration[index2] + ",");
}
}
public String toString(){
return String.format("%d, %f, %s, %s", numStrings, guitarLength, guitarManufacturer, guitarColor);
}
}
class TestGuitar extends Guitar{
public static void main (String args[] ){
Guitar newGuitar = new Guitar(6, 24.75, "Les Paul", Color.white);
playGuitar();
}
}
答案 0 :(得分:1)
class Guitar {
private static int i = 1;
private static Random rand = new Random();
private int numStrings;
private double guitarLength ;
private String guitarManufacturer;
private Color guitarColor;
//declaring the constructor
public Guitar (){
//all my problems coming from here
numStrings = 6;
guitarLength = 28.2;
guitarManufacturer = "Gibson";
guitarColor = Color.RED;
}
public Guitar (int strings, double length, String manufacturer, Color color){
this.numStrings = Strings;
this.guitarLength = length;
this.guitarManufacturer = manufacturer;
this.guitarColor = color;
System.out.printf("toString: %s \n ", this);
}
public int getNumStrings() {
return numStrings;
}
public double getGuitarLength() {
return guitarLength;
}
public String getGuitarManufacturer() {
return guitarManufacturer;
}
public Color getGuitarColor(){
return this.guitarColor;
}
public static void playGuitar(){
String[] musicNotes = {"A", "B", "C", "D", "E", "F", "G"};
String[] musicDuration = {"(0.25)","(0.5)","(1)", "(2)","(4)"};
System.out.print("playGuitar: ");
for (i = 1; i < 17; i++){
int index1 = rand.nextInt(musicNotes.length);
int index2 = rand.nextInt(musicDuration.length);
System.out.print(musicNotes[index1]);
System.out.print(musicDuration[index2] + ",");
}
}
public String toString(){
return String.format("%d, %f, %s, %s", numStrings, guitarLength, guitarManufacturer, guitarColor);
}
}
您还可以使用1个构造器:
public Guitar (int strings=6, double length=28.2, String manufacturer="Gibson", Color color=Color.RED){
this.numStrings = Strings;
this.guitarLength = length;
this.guitarManufacturer = manufacturer;
this.guitarColor = color;
System.out.printf("toString: %s \n ", this);
}
答案 1 :(得分:1)
您的默认初始值属于no-args构造函数。然后,您需要更正在第二个构造函数中分配值的顺序。喜欢,
private int numStrings;
private double guitarLength;
private String guitarManufacturer;
private Color guitarColor;
private static int i = 1;
private static Random rand = new Random();
// declaring the constructor
public Guitar() {
this.numStrings = 6;
this.guitarLength = 28.2;
this.guitarManufacturer = "Gibson";
this.guitarColor = Color.RED;
}
public Guitar(int strings, double length, String manufacturer, Color color) {
this.numStrings = strings;
this.guitarLength = length;
this.guitarManufacturer = manufacturer;
this.guitarColor = color;
System.out.printf("toString: %s \n ", this);
}
请注意,第一个构造函数可以重写为使用其他构造函数。喜欢,
// declaring the constructor
public Guitar() {
this(6, 28.2, "Gibson", Color.RED);
}