Java:0 arg 构造函数和构造函数的问题



我在尝试构建变量已经具有默认值的构造函数时遇到了很多问题,然后调用我稍后在TestGuitar类中设置的新变量。我已经成功地编译了我的程序并打印了默认值。我还没有成功地保留我试图在TestGuitar类的newGuitar变量中设置的新变量。我知道所有的问题都来自我的构造函数。在这种情况下,我需要一个创建默认吉他的 0 arg 构造函数和一个创建非常特定的吉他的新构造函数。

我为默认吉他设置了一些私有变量。我希望我的程序返回默认变量,如果我从不向 TestGuitar 类中的 newGuitar 变量传递任何东西。如果我确实传递了一些东西,即吉他长度 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();
}
}

默认初始值属于 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);
}
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);
}

最新更新