Created
February 3, 2021 12:10
-
-
Save reinana/c41946aac05369d5da8f2a3a7c92bc60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 犬を表すクラスです。 | |
*/ | |
public class Dog { | |
/** | |
* 名前。 | |
*/ | |
private String name; | |
/** | |
* 鳴く回数。 | |
*/ | |
private int cryCount; | |
/** | |
* コンストラクタ | |
* @param name 名前 | |
*/ | |
public Dog(String name) { | |
this(name, 1); | |
} | |
/** | |
* コンストラクタ。 | |
* @param name 名前 | |
* @param cryCount 鳴く回数 | |
*/ | |
public Dog(String name, int cryCount) { | |
this.setName(name); | |
this.setCryCount(cryCount); | |
} | |
/** | |
* 名前を設定します。 | |
* @param name 名前 | |
*/ | |
public void setName(String name) { | |
this.name = name; | |
} | |
/** | |
* 鳴く回数を設定します。 | |
* @param cryCount 鳴く回数 | |
*/ | |
public void setCryCount(int cryCount) { | |
this.cryCount = cryCount; | |
} | |
/** | |
* 鳴きます。 | |
*/ | |
public void cry() { | |
System.out.print(this.name + "「"); | |
for(int i = 0; i < this.cryCount; i ++) { | |
System.out.print("ワン"); | |
} | |
System.out.println("」"); | |
} | |
/** | |
* お座りします。 | |
*/ | |
public void sitDown() { | |
System.out.println(this.name + "は座りました。"); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if(this == obj) { | |
return true; | |
} else if(obj == null) { | |
return false; | |
} else if(this.getClass() != obj.getClass()) { | |
return false; | |
} else { | |
// 参照型のキャスト | |
Dog other = (Dog)obj; | |
// 名前と鳴く回数が同じかどうか判定する | |
if(this.name.equals(other.name) && this.cryCount == other.cryCount) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment