2016-05-06 3 views
0

期限切れの映画の延滞料金を計算するには、オーバーライドされたcalclatefeesメソッドも必要です。現在Javaでの継承と多型性の問題

public class Action extends Movie { 

    protected double latecost; 
    protected double latefees; 

    public Action (String title, String rating, int id, int rentTime, 
      double latecost, double latefees) { 

     super(title, rating, id, rentTime); 
     this.title = title; 
     this.rating= rating; 
     this.id = id; 
     this.rentTime= 8; 
     this.latecost = 2; 

     System.out.println("Overridden " + title + rating + " " + id + " " 
       + latecost + " " + latefees); 
    } 
    public double calclatefees (double latecost) { 

     if (rentTime > 3) 
      latefees = ((rentTime - 3) * latecost); 
     return latefees;} 

    @Override 
    public String toString() { 
    String x = "Movie: " + title + " is rated " + rating + "\nMovie ID   number:" 
      + id + " late fees are $" + latefees; 
    return x; 
    } 
    }  
+1

出力が役立ちます。 – rmlan

+0

あなたが持っている問題を除いて、列挙子にアクション、コメディ、ドラマをリファクタリングすることを考慮すると、継承されたムービーではなくムービーのより良い属性です。:) –

+0

私のtoStringが返されない理由を調べようとしています私の価値観は、私のオーバーロードされたコンストラクタに入れました。なぜ、彼らを私のスーパーに割り当てても、彼らはまだそれをしません。 – sjames14

答えて

0

この版画:完璧です

overloaded - not overridden NR 00 
overridden 

。コンストラクタは特別に扱われることを示しています。それらはすべて最初に親コンストラクタを呼び出します。

そのためが過負荷に印字Movieのコンストラクタを見ている -上書きで、版画Actionキックの後、コンストラクタNR 00を、オーバーライドされません。

0

このキーワードを使用する必要があります。私は、あなたがコンストラクタで同じ名前を使用していない場合、何も割り当てていないと信じています。パラメータ内のタイトルに、割り当てたいものと等しくなるように指示します。下の例では、「上書きしない」です。

public Movie(String title, String racing, int id, int rentTime){ 
    this.title = " not overridden "; 
    // probably should be something like this.title = title after debugging. 
    //etc... 
} 

ため、このキーワードは、現在のオブジェクトのデータメンバーを指し、そうthis.titleは、映画のタイトルではなく、パラメータのメンバーのタイトルになります。

上記の設定方法でもこれを行う必要があります。

public void rating (String rating) {this.rating = rating;} 

編集:コンストラクタのためにこれを試してください

public Movie(String title, String rating, int id, int rentTime){ 
    this.title = " not overridden "; 
    this.rating = " NR "; 
    this.id = 0; 
    this.rentTime = 0; 
    System.out.println(" overloaded -" + title + rating + id + rentTime); 
} 

あなたはあなたが他の子クラスに渡している実際の値を表示したい場合、あなたはこれにそれを変更する必要があり、出力が表示した後、 :

public Movie(String title, String rating, int id, int rentTime){ 
    this.title = title; 
    this.rating = rating; 
    this.id = id; 
    this.rentTime = rentTime; 
    System.out.println(" overloaded -" + title + rating + id + rentTime); 
} 
+0

子クラスtoStringを取得して、配列mainメソッドの値を出力できますか? – sjames14

+0

はい、今のところ印刷する値はありません。あなたのtoStringメソッドは何も表示していませんか?多分あなたが入れた文字列リテラルだけでしょうか?また、あなたのtoStringメソッドの上に@Overrideアノテーションがあることを確認してください。 – Orin

+0

私は@Overrideを持っていますが、リテラル以上のものを表示するにはどうしたらいいですか? – sjames14