###前提・実現したいこと
最近こちらを知り、勉強させていただいております。
非常に初歩的なことで申し訳ないのですが、調べてもわからなかったので質問させてください。
Mainクラス→Stuffクラス→Anotherクラスで値を受け渡したいのですが、うまくいきません。
具体的には、Mainクラスで値を変数name,numにセットし、セットした値をAnotherクラスで取得したいです。
しかし、Anotherクラスで値が取得できず(?)、null,0になってしまいます。
ご指導いただければ嬉しいです。
よろしくお願い致します。
###発生している問題・エラーメッセージ
佐藤:1 //MainでStuffのPrintを呼び出した結果 佐藤:1 //MainでSystem.out.printlnの結果 null:0 //AnotherでStuffのPrintを呼び出した結果 null:0 //AnotherでSystem.out.printlnの結果
###該当のソースコード
Java
1public class Main { 2 public static void main(String[] args) { 3 Stuff stf = new Stuff(); 4 stf.setName("佐藤"); 5 stf.setNum(1); 6 stf.Print(); 7 System.out.println(stf.getName() + ":" + stf.getNum()); 8 9 Another an = new Another(); 10 an.AnotherPrint(); 11 } 12} 13 14public class Stuff { 15 private String name; 16 private int num; 17 18 public Stuff() { 19 } 20 21 public Stuff(String name, int num){ 22 this.name = name; 23 this.num = num; 24 } 25 26 public String getName() { 27 return name; 28 } 29 public void setName(String name) { 30 this.name = name; 31 } 32 public int getNum() { 33 return num; 34 } 35 public void setNum(int num) { 36 this.num = num; 37 } 38 39 public void Print() { 40 System.out.println(name + ":" + num); 41 } 42} 43 44public class Another { 45 public void AnotherPrint() { 46 Stuff stf = new Stuff(); 47 stf.Print(); 48 System.out.println(stf.getName() + ":" + stf.getNum()); 49 } 50} 51
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/14 05:10