abstract class Animal {
this.name = name;
}
public void play() {}
}
class Dog extends Animal {
super(name);
}
public void play() {
System.
out.
println("わんわん!" + name
+ "だよ"); }
}
class Cat extends Animal {
private int sleep;
public Cat
(String name,
int sleep
) { super(name);
this.sleep = sleep;
}
public void play() {
System.
out.
println("にゃあ!" + name
+ "だよ"); if(sleep == 1) {
System.
out.
println("...でも寝る"); }
}
}
class Pet
{
public static void main
(String[] args
) { Animal[] pets = {
new Dog("ぽち"),
new Cat("たま", 1)
};
for(Animal pet: pets) {
pet.play();
}
}
}
YWJzdHJhY3QgY2xhc3MgQW5pbWFsIHsKICAgIHB1YmxpYyBTdHJpbmcgbmFtZTsKCiAgICBwdWJsaWMgQW5pbWFsKFN0cmluZyBuYW1lKSB7CiAgICAgICAgdGhpcy5uYW1lID0gbmFtZTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBwbGF5KCkge30KfQoKY2xhc3MgRG9nIGV4dGVuZHMgQW5pbWFsIHsKICAgIHB1YmxpYyBEb2coU3RyaW5nIG5hbWUpIHsKICAgICAgICBzdXBlcihuYW1lKTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBwbGF5KCkgewogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigi44KP44KT44KP44KT77yBIiArIG5hbWUgKyAi44Gg44KIIik7CiAgICB9Cn0KCmNsYXNzIENhdCBleHRlbmRzIEFuaW1hbCB7CiAgICBwcml2YXRlIGludCBzbGVlcDsKICAgIAogICAgcHVibGljIENhdChTdHJpbmcgbmFtZSwgaW50IHNsZWVwKSB7CiAgICAgICAgc3VwZXIobmFtZSk7CiAgICAgICAgdGhpcy5zbGVlcCA9IHNsZWVwOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHBsYXkoKSB7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCLjgavjgoPjgYLvvIEiICsgbmFtZSArICLjgaDjgogiKTsKICAgICAgICBpZihzbGVlcCA9PSAxKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiLi4u44Gn44KC5a+d44KLIik7CiAgICAgICAgfQogICAgfQp9CgpjbGFzcyBQZXQKewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIEFuaW1hbFtdIHBldHMgPSB7CiAgICAgICAgICAgIG5ldyBEb2coIuOBveOBoSIpLAogICAgICAgICAgICBuZXcgQ2F0KCLjgZ/jgb4iLCAxKQogICAgICAgIH07CgogICAgICAgIGZvcihBbmltYWwgcGV0OiBwZXRzKSB7CiAgICAgICAgICAgIHBldC5wbGF5KCk7CiAgICAgICAgfQogICAgfQp9Cg==