Javaを勉強中です。
thisを必要な時と必要ではない時の違いがわかりません。
class CustomerCard{ int id; String name; String address; double shoeSize; CustomerCard(int id,String name,String address,double shoeSize){ this.id = id; this.name = name; this.address = address; this.shoeSize = shoeSize; } void printInfo(){ System.out.println("ID:"+this.id); System.out.println("氏名"+this.name); System.out.println("住所:"+this.address); System.out.println("靴のサイズ:"+this.shoeSize); } } class CustomerManager{ public static void main(String args[]){ CustomerCard[] cards = new CustomerCard[100]; cards[0] = new CustomerCard(1001,"山田太郎","東京都",26.5); cards[1] = new CustomerCard(1002,"佐藤花子","神奈川県",24.5); cards[2] = new CustomerCard(1003,"鈴木健児","茨城県",26.0); for (int i = 0;i < 100;i++){ if(cards[i] == null){ break; } System.out.println(i + "番目の顧客カードに記載の情報"); cards[i].printInfo(); System.out.println("==============="); } } }
と書いたコードを実行すると目的のアウトプットができました。
一方で
printInfo()を
void printInfo(){ System.out.println("ID:"+id); System.out.println("氏名"+name); System.out.println("住所:"+address); System.out.println("靴のサイズ:"+shoeSize); }
と書いても最初のコードと同じアウトプットができました。
ここで分からなくなったのが、thisを必要な時と必要ではない時の違い です。
本には、this.変数は
「インスタンスから見た自分自身のインスタンス変数」という意味になるそうで、この自分自身のインスタンス変数の意味が理解できませんでした。
thisを必要な時と必要ではない時の違いをどのように見分ければ良いのでしょうか?

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/08/02 07:54
2017/08/02 08:05