前提
抽象クラスissueのサブクラスにBondクラスを加え、BondクラスのコンストラクタにIssueを引数とする段階でつまづいています。下記のエラー文が表示されるのはBondコンストラクタ引数のissueから何も検出されないため、Issueクラス内のコンストラクタと相違が生じるためでしょうか。また修正案などについてもご教授いただきたいです。よろしくお願いします。
同じディレクトリ内でenum型定義用のBondType.javaファイルも作成しているためソースを併せて添付しておきます。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- IssueをBondコンストラクタの引数として扱いたい。
発生している問題・エラーメッセージ
Issue.java:29: エラー: クラス Issueのコンストラクタ Issueは指定された型に適用できません。 public Bond (Issue issue, int maturity, double coupon) { ^ 期待値: String,String 検出値: 引数がありません 理由: 実引数リストと仮引数リストの長さが異なります
該当のソースコード
Issue.java
Java
1import java.util.Objects; 2 3public abstract class Issue { 4 private String code; 5 private String name; 6 7 public Issue (String code, String name) { 8 this.code = code; 9 this.name = name; 10 if (code == null || name == null) { 11 throw new IllegalArgumentException("nullです。"); 12 } 13 } 14 // getter 15 public String getCode() { 16 return this.code; 17 } 18 public String getName() { 19 return this.name; 20 } 21} 22 23 class Bond extends Issue { 24 private Issue issue; 25 private int maturity; 26 private double coupon; 27 private BondType type; 28 29 public Bond (Issue issue, int maturity, double coupon) { 30 this.issue = issue; 31 this.maturity = maturity; 32 this.coupon = coupon; 33 // チェック処理 34 if (maturity < 20000101 || 29991231 < maturity || coupon < 0) { 35 throw new IllegalArgumentException("数値が範囲外です"); 36 } 37 if (coupon == 0) { 38 type = BondType.ZERO_COUPON_BOND; 39 } else { 40 type = BondType.COUPON_BOND; 41 } 42 } 43 //getter 44 public Issue getIssue() { 45 return this.issue; 46 } 47 public int getMaturity() { 48 return this.maturity; 49 } 50 public double getCoupon() { 51 return this.coupon; 52 } 53 public BondType getBondType() { 54 return this.type; 55 } 56 57 @Override 58 public String toString() { 59 StringBuilder builder = new StringBuilder(); 60 builder.append(issue); 61 builder.append(maturity); 62 builder.append(coupon); 63 builder.append(type); 64 return builder.toString(); 65 } 66 67 //equalsメソッド 68 @Override 69 public boolean equals (Object obj) { 70 if (this == obj) { 71 return true; 72 } 73 if (obj == null) { 74 return false; 75 } 76 if (getClass() != obj.getClass()) { 77 return false; 78 } 79 Bond other =(Bond)obj; 80 if (this.issue != other.issue) { 81 return false; 82 } 83 if (maturity == 0) { 84 if (other.maturity != 0) { 85 return false; 86 } 87 } else if (maturity != other.maturity) { 88 return false; 89 } 90 if (Double.compare(other.coupon,coupon) != 0) { 91 return false; 92 } 93 94 return true; 95 } 96 97 //hashCode 98 @Override 99 public int hashCode() { 100 return Objects.hash(this.issue, this.maturity, this.coupon); 101 } 102}
BondType.java
public enum BondType { COUPON_BOND, ZERO_COUPON_BOND };

回答2件
あなたの回答
tips
プレビュー