2016-10-25 3 views
0

静的コンテキストから参照できないエラーメッセージではない静的変数parts[0] = new Part("Nut", 12, 0.05);「this」コールによってオーバーロードされたコンストラクタを実装する方法を教えてください。"this"コールでオーバーロードされたコンストラクタを実装する

Part[] parts = new Part[5]; 

    parts[0] = new Part("Nut", 12, 0.05); 
    parts[1] = new Part("Bolt", 15, 0.07); 
    // default name "Unknown" 
    parts[2] = new Part(); 
    // default quantity -1 
    parts[3] = new Part("Screw"); 
    // default price .01 
    parts[4] = new Part("Grommet", 4); 

    System.out.println("\nPARTS LIST"); 
    for(Part p : parts) 
    { 
    System.out.println(p.getDescription() + ", " + p.getQuantity() 
     + ", " + p.getCost()); 
    } 

    parts[0].setDescription("Hex nut"); 
    parts[1].setQuantity(30); 
    parts[2].setCost(0.19); 
    parts[3].setDescription("Rubber grommet"); 
    parts[4].setQuantity(40); 

    System.out.println("\nPARTS LIST AFTER CHANGES"); 
    for(Part p : parts) 
    { 
    // Uses the toString method! 
    System.out.println(p); 
    } 
    } 

class Part 
{ 
private String partDesc; 
private int quantity;   // parts quantity 
private double cost;   // parts cost 

public Part(String d, int q, double c) // Constructor for Part 
{ 
    partDesc = d; 
    quantity = q; 
    cost = c; 
} 

public Part(String partDesc, double cost) 
{ 
    this("Unknown", -1, cost); 
} 

public Part(String partDesc) 
{ 
    this(partDesc, -1, .01); 
} 

public Part(String PartDesc, int quantity) 
{ 
    this(partDesc, quantity, 0.1); 
} 

public String getDescription() // getter for parts description 
{ 
    return partDesc; 
} 

public int getQuantity()  // getter for parts quantity 
{ 
    return quantity; 
} 
public double getCost()   // getter for parts cost 
{ 
    return cost; 
} 

public String setDescription(String d) //setter for parts description 
{ 
    partDesc = d; 
    return partDesc; 
} 
public int setQuantity(int newQuantity)   //setter for parts quantity 
{ 
    quantity = newQuantity; 
} 
public double setCost(double newCost)     //setter for parts cost 
{ 
    cost = newCost; 
} 
public String toString() 
{ 
    return partDesc + " , " + quantity + " , " + cost; 
} 
} 
+0

どのコード行にエラーが表示されますか? – Eran

+0

_オブジェクトが不完全または無効な値で構成されることを目的としていますか?_複数のコンストラクタは、通常、継ぎ目が不安定になるような値を指定することを避けることによってプログラマの生活を楽にします。しかし実際には、正しく構成されたオブジェクトに依存することができず、したがってプログラム全体で一貫性チェックを広めなければならないため、人生はより困難になります。利便性の他に本当の理由がある場合にのみ、複数のコンストラクタを提供することで、より簡単に生き生きとしてください。 –

答えて

0

あなたは多くの間違いを犯しました。私はそれを修正した。 デフォルトコンストラクタは定義されていません。ここでは、デフォルトのコンストラクタを持つオブジェクトを作成するためです。 一部のメソッドはreturn文を追加しません。

ここにコメントを追加しました。次のことを行う必要がありますが、引数が必要な場合は、あなたがそれを使用する必要があり、すべてのhttp://beginnersbook.com/2013/05/constructor-overloading/

public class Part { 

private String partDesc; 
private int quantity; // parts quantity 
private double cost; // parts cost 

//Define default constructor. Because You have created object with this. 
public Part() // Constructor for Part 
{ 

} 

public Part(String d, int q, double c) // Constructor for Part 
{ 
    partDesc = d; 
    quantity = q; 
    cost = c; 
} 

public Part(String partDesc, double cost) 
{ 
    this("Unknown", -1, cost); 
} 

public Part(String partDesc) 
{ 
    this(partDesc, -1, .01); 
} 

public Part(String partDesc, int quantity)//Here p should be simple.unless show error 
{ 
    this(partDesc, quantity, 0.1); 
} 



public String getDescription() // getter for parts description 
{ 
    return partDesc; 
} 

public int getQuantity()  // getter for parts quantity 
{ 
    return quantity; 
} 
public double getCost()   // getter for parts cost 
{ 
    return cost; 
} 

public String setDescription(String d) //setter for parts description 
{ 
    partDesc = d; 
    return partDesc; 
} 
public int setQuantity(int newQuantity)   //setter for parts quantity 
{ 
    quantity = newQuantity; 
    return quantity;//Here return value 
} 
public double setCost(double newCost)     //setter for parts cost 
{ 
    cost = newCost; 
    return cost;//Here return value 
} 
public String toString() 
{ 
    return partDesc + " , " + quantity + " , " + cost; 
} 





     Part[] parts = new Part[5]; 

     parts[0] = new Part("Nut", 12, 0.05); 
     parts[1] = new Part("Bolt", 15, 0.07); 
     // default name "Unknown" 
     parts[2] = new Part(); 
     // default quantity -1 
     parts[3] = new Part("Screw"); 
     // default price .01 
     parts[4] = new Part("Grommet", 4); 

     System.out.println("\nPARTS LIST"); 
     for(Part p : parts) 
     { 
     System.out.println(p.getDescription() + ", " + p.getQuantity() 
      + ", " + p.getCost()); 
     } 

     parts[0].setDescription("Hex nut"); 
     parts[1].setQuantity(30); 
     parts[2].setCost(0.19); 
     parts[3].setDescription("Rubber grommet"); 
     parts[4].setQuantity(40); 


     System.out.println("\nPARTS LIST AFTER CHANGES"); 
     for(Part p : parts) 
     { 
     // Uses the toString method! 
     System.out.println(p); 
     } 
+0

これはあなたが望むコードであることを確認してください –

+0

多分あなたが何をしたかについてのいくつかの説明? – biziclop

+0

説明を完了しました –

0

まず、そう、代わりに

public Part(String partDesc, double cost) { 
    this("Unknown", -1, cost); 
} 

の:

は、参照してください

public Part(String partDesc, double cost) { 
    this(partDesc, -1, cost); 
} 

"Unknown"引数は、実際にはpartDescを指定しない場合に使用する必要があります、デフォルトコンストラクタのように。

そして親切な助言:すべての部品にはコストがかかりますから、常に価格を尋ねるべきです。しかし、costを使わずにオーバーロードされたコンストラクタを作成することを主張する場合は、単純に0.0dに設定します。私は私が助けたと思っている

class Part { 

    private String partDesc; 
    private int quantity;   // parts quantity 
    private double cost;   // parts cost 

    public Part() { //Default constructor 
     this("Unknown", 1, 0.0d); 
    } 

    public Part(String d, int q, double c) // Constructor for Part 
    { 
     partDesc = d; 
     quantity = q; 
     cost = c; 
    } 

    public Part(String partDesc, double cost) { 
     this(partDesc, , 1, cost); 
    } 

    public Part(String partDesc) { 
     this(partDesc, 1, 0.0d); 
    } 

    public Part(String PartDesc, int quantity) { 
     this(partDesc, quantity, 0.0d); 
    } 

    //Rest of the class... 

} 

だから、あなたはこのようなものを持つことができます。

よろしくお願いいたします。 :)

関連する問題