前提・実現したいこと
生成したメソッドのフィールドをsetterメソッドを使って設定したいと思っています。
発生している問題・エラーメッセージ
"chを解決できません" "oを解決できません" "hを解決できません" と表示される
該当のソースコード
Java
1public class Main { 2 public static void main(String[] args) { 3 Molecule o = new Molecule("O2", 5, 10); 4 Molecule h = new Molecule("H2", 2, 8); 5 Molecule ch = new Molecule("H2O"); 6 } 7} 8 9public class Molecule { 10 private String name; 11 private int weight; 12 private int sec; 13 14 public Molecule(String name) { 15 this.name = name; 16 } 17 18 public Molecule(String name, int weight, int sec) { 19 this(name); 20 this.weight = weight; 21 this.sec = sec; 22 } 23 24 //getterメソッド 25 public String getName() { 26 return this.name; 27 } 28 public int getWeight() { 29 return this.weight; 30 } 31 public int getSec() { 32 return this.sec; 33 } 34 //setterメソッド 35 public void setName(String name) { 36 this.name = name; 37 } 38 public void setWeight(int weight) { 39 this.weight = weight; 40 } 41 public void setSec(int sec) { 42 this.sec = sec; 43 } 44 public void chemical(Molecule a1, Molecule a2) { 45 System.out.println(a1 + "と" + a2 + "が化学反応を起こした!"); 46 ch.setWeight(o.getWeight() + h.getWeight()); 47 } 48}
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
Eclipseを使っています
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/24 14:52
2019/01/24 14:53
2019/01/24 14:55
2019/01/24 14:59
2019/01/24 15:01
2019/01/24 15:41