class Animal{
private String name;
//...
public void eat() {
}
//...
}
class Pig extends Animal{
}
class Wolf extends Animal{
}
private String name;
//...
public void eat() {
}
//...
}
class Pig extends Animal{
}
class Wolf extends Animal{
}
猪 狼 房子
对象确定之后根据题目分析对象的属性 由于你的题目提的比较少
可以确定 猪有个属性 猪的名字pigName(猪老大、猪老二、猪老三)
狼有个属性 wolfName(麦克) 这里可以用继承
房子有类型 或者叫名字也可以
对象、属性 确定之后就要确定对象的行为了(方法)
猪 builhHouse(String type)
狼 attackHouse(String type) eatPig(String pigName)
好了,以上基本信息确定了,下面就是业务逻辑了。狼吹倒草房子,吃猪老三
吹倒房子的条件是什么,这个你就得自己分析了
综上:分析出对象、属性、方法、业务逻辑
public boolean destoryHouse(House.type){
boolean result = null;
if(House.HouseType == "GRASS"){
result = false;
} if(House.HouseType == "WOOD"){
result = true;
}
if(House.HouseType == "BRICK"){
result = false;
}
return result;
}
public boolean eatPig(Pig pig){
boolean result = (Boolean) null;
new House.type type = pig.action();
if(House.type == BRICK ){
result = true;
} if(House.type == GRASS ){
result = false;
}
if(name.equals(House.type == WOOD) ){
result = false;
}
return result;
}
}
public class Pig extends Animal{
String pigName = null;
public Object action(String pigName){
return new House().type;
}
}enum HouseType{
GRASS,BRICK,WOOD
}
public class House {
HouseType type = null;
}abstract public class Animal {
String name = null;
public Object action(){
return new Object();
}
}
public boolean destoryHouse(House.type){
boolean result = null;
if(House.HouseType == "GRASS"){
result = false;
} if(House.HouseType == "WOOD"){
result = true;
}
if(House.HouseType == "BRICK"){
result = false;
}
return result;
}
public boolean eatPig(Pig pig){
boolean result = (Boolean) null;
new House.type type = pig.action();
if(House.type == BRICK ){
result = true;
} if(House.type == GRASS ){
result = false;
}
if(name.equals(House.type == WOOD) ){
result = false;
}
return result;
}
}
public class Pig extends Animal{
String pigName = null;
public Object action(String pigName){
return new House().type;
}
}enum HouseType{
GRASS,BRICK,WOOD
}
public class House {
HouseType type = null;
}abstract public class Animal {
String name = null;
public Object action(){
return new Object();
}
}
Class Wolf{
List<Pig> canEatList; //config by Spring.
List<String> canAttackList; // config by Spring.
public void eat(Pig pig) thrown CannotEatException{ };
public void attack(House house) thrown CannotAttackException();
private boolean checkEat(Pig pig);
private boolean checkAttack(String type);
}Class House{
String type;
//get,set method
}Interface Pig{
public House createHouse();
}Class PigFirst implements Pig{
// implement the abstract method
}
Class PigSecond implements Pig{
// implement the abstract method
}
Class PigThird implements Pig{
// implement the abstract method
}
*接口
*/
public interface Pig {
public String GetHouseMaterial();
}
public class TestClass {
private Pig[] pigs = { new Pig() {
@Override
public String GetHouseMaterial() {
return "grass";
}
}, new Pig() {
@Override
public String GetHouseMaterial() {
return "wood";
}
}, new Pig() {
@Override
public String GetHouseMaterial() {
return "stone";
}
} }; public boolean wofCanBlownHouse(Pig pig) {
if ("grass".equals(pig.GetHouseMaterial())) {
return true;
}
if ("wood".equals(pig.GetHouseMaterial())) {
return true;
}
if ("stone".equals(pig.GetHouseMaterial())) {
return false;
}
return false;
} public static void main(String[] args) {
TestClass t = new TestClass();
for (Pig p : t.pigs) {
if (t.wofCanBlownHouse(p)) {
System.out.println("狼吃掉了房子材料是 " + p.GetHouseMaterial() + "的猪");
}else{
System.out.println("狼没有吃掉房子材料是 " + p.GetHouseMaterial() + "的猪");
}
}
}
}运行结果:
狼吃掉了房子材料是 grass的猪
狼吃掉了房子材料是 wood的猪
狼没有吃掉房子材料是 stone的猪