package sample;
public class MyDog {
⁄⁄ 犬
public Dog dog;
⁄⁄ メソッドmain
public static void main(String[] args) throws Exception {
final int UNREF_OBJ_CREATED = 10;
if (args.length < 3) {
System.out.println("引数が正しく入力されていません。->[名前][好きなもの][嫌いなもの]");
return;
}
String name = args[0];
String favorite = args[1];
String bad = args[2];
MyDog myDog = new MyDog();
myDog.dog = new Dog(name);
myDog.dog.setFavorite(favorite);
myDog.dog.setBad(bad);
System.out.println("MyDog started");
while (true) {
System.out.println();
Thread.sleep(5000);
⁄⁄ 自己紹介します
myDog.introduce();
Thread.sleep(5000);
for (int i = 0; i < UNREF_OBJ_CREATED; i++) {
new MyDog();
}
System.out.println(UNREF_OBJ_CREATED
+ " unreferenced objects of MyDog has been created");
}
}
⁄* 自己紹介 *⁄
public void introduce() {
dog.name();
dog.favorite();
dog.bad();
}
}
⁄⁄ 犬クラス
class Dog {
private String name;
private String favorite;
private String bad;
public Dog(String name) {
this.name = name;
}
public void setFavorite(String favorite) {
this.favorite = favorite;
}
public void setBad(String bad) {
this.bad = bad;
}
public void name() {
System.out.println("私の名前は" + name + "です。");
}
public void favorite() {
System.out.println("私は" + favorite + "が好きです。");
}
public void bad() {
System.out.println("私は" + bad + "が苦手です。");
}
}
|
|