1 public class Zoo 2 { 3 4 public static void main(String args[]) 5 { 6 7 Feeder f = new Feeder("小李"); 8 9 // 饲养员小李喂养一只狮子 10 11 f.feedLion(new Lion()); 12 13 // 饲养员小李喂养十只猴子 14 15 for (int i = 0; i < 10; i++) 16 { 17 18 f.feedMonkey(new Monkey()); 19 20 } 21 22 // 饲养员小李喂养5只鸽子 23 24 for (int i = 0; i < 5; i++) 25 { 26 27 f.feedPigeon(new Pigeon()); 28 29 } 30 31 } 32 33 } 34 35 36 class Feeder 37 { 38 39 40 public String name; 41 42 43 public Feeder(String name) 44 { 45 46 this.name = name; 47 48 } 49 50 51 public void feedLion(Lion l) 52 { 53 54 l.eat(); 55 56 } 57 58 59 public void feedPigeon(Pigeon p) 60 { 61 62 p.eat(); 63 64 } 65 66 67 public void feedMonkey(Monkey m) 68 { 69 70 m.eat(); 71 72 } 73 74 } 75 76 77 class Lion 78 { 79 80 81 public void eat() 82 { 83 84 System.out.println("我不吃肉谁敢吃肉!"); 85 86 } 87 88 } 89 90 91 class Monkey 92 { 93 94 public void eat() 95 { 96 97 System.out.println("我什么都吃,尤其喜欢香蕉。"); 98 99 } 100 101 } 102 103 104 class Pigeon 105 { 106 107 108 public void eat() 109 { 110 111 System.out.println("我要减肥,所以每天只吃一点大米。"); 112 113 } 114 115 }
动物园代码
1 public final class Address 2 { 3 private final String detail; 4 private final String postCode; 5 6 //在构造方法里初始化两个实例属性 7 public Address() 8 { 9 this.detail = ""; 10 this.postCode = ""; 11 12 } 13 public Address(String detail , String postCode) 14 { 15 this.detail = detail; 16 this.postCode = postCode; 17 } 18 //仅为两个实例属性提供getter方法 19 public String getDetail() 20 { 21 return this.detail; 22 } 23 24 public String getPostCode() 25 { 26 return this.postCode; 27 } 28 //重写equals方法,判断两个对象是否相等。 29 public boolean equals(Object obj) 30 { 31 if (obj instanceof Address) 32 { 33 Address ad = (Address)obj; 34 if (this.getDetail().equals(ad.getDetail()) && this.getPostCode().equals(ad.getPostCode())) 35 { 36 return true; 37 } 38 } 39 return false; 40 } 41 public int hashCode() 42 { 43 return detail.hashCode() + postCode.hashCode(); 44 } 45 }
标签:总结,12,String,void,detail,eat,postCode,public,java10 From: https://www.cnblogs.com/Lyh3012648079/p/17760323.html