1、代码:
1 public class ExplorationJDKSource { 2 public static void main(String[] args) { 3 System.out.println(new A()); 4 } 5 } 6 class A{}
运行结果:输出的是类A的地址。
2、代码:
1 public class Fruit{ 2 public String toString(){ 3 return "Fruit toString."; 4 } 5 public static void main(String args[]){ 6 Fruit f=new Fruit(); 7 System.out.println("f="+f); 8 // System.out.println("f="+f.toString()); 9 } 10 }
运行结果:字符串相加是字符串连接。
3、代码:
1 public class ParentChildTest { 2 public static void main(String[] args) { 3 Parentq parent=new Parentq(); 4 parent.printValue(); 5 Childq child=new Childq(); 6 child.printValue(); 7 8 parent=child; 9 parent.printValue(); 10 11 parent.myValue++; 12 parent.printValue(); 13 14 ((Childq)parent).myValue++; 15 parent.printValue(); 16 17 } 18 } 19 class Parentq{ 20 public int myValue=100; 21 public void printValue() { 22 System.out.println("Parent.printValue(),myValue="+myValue); 23 } 24 } 25 class Childq extends Parentq{ 26 public int myValue=200; 27 public void printValue() { 28 System.out.println("Child.printValue(),myValue="+myValue); 29 } 30 }
运行结果:一二三行的结果很好理解,第四行是parent.myValue++;parent.printValue();代码的结果,加的是parent中的myValue,方法是Child的。第五行是((Childq)parent).myValue++;parent.printValue();代码的结果,加的是child中的myValue,方法是Child的
4、代码:
1 public class Zoo1 { 2 public static void main(String args[]){ 3 Feeder f = new Feeder("小李");// 饲养员小李喂养一只狮子 4 f.feedLion(new Lion());// 饲养员小李喂养十只猴子 5 for (int i = 0; i < 10; i++){ 6 f.feedMonkey(new Monkey()); 7 }// 饲养员小李喂养5只鸽子 8 for (int i = 0; i < 5; i++){ 9 f.feedPigeon(new Pigeon()); 10 } 11 } 12 } 13 14 class Feeder { 15 public String name; 16 public Feeder(String name){ 17 this.name = name; 18 } 19 public void feedLion(Lion l){ 20 l.eat(); 21 } 22 public void feedPigeon(Pigeon p){ 23 p.eat(); 24 } 25 public void feedMonkey(Monkey m){ 26 m.eat(); 27 } 28 } 29 30 class Lion{ 31 public void eat() { 32 System.out.println("我不吃肉谁敢吃肉!"); 33 } 34 } 35 36 class Monkey { 37 public void eat() { 38 System.out.println("我什么都吃,尤其喜欢香蕉。"); 39 } 40 } 41 42 class Pigeon { 43 public void eat() { 44 System.out.println("我要减肥,所以每天只吃一点大米。"); 45 } 46 }
运行结果:
5、代码:
1 public class Zoo2 { 2 public static void main(String args[]){ 3 Feeder f = new Feeder("小李");//饲养员小李喂养一只狮子 4 f.feedAnimal(new Lion());//饲养员小李喂养十只猴子 5 for (int i = 0; i < 10; i++) { 6 f.feedAnimal(new Monkey()); 7 }//饲养员小李喂养5只鸽子 8 for (int i = 0; i < 5; i++) { 9 f.feedAnimal(new Pigeon()); 10 } 11 } 12 } 13 14 class Feeder { 15 public String name; 16 Feeder(String name) { 17 this.name = name; 18 } 19 public void feedAnimal(Animal an) { 20 an.eat(); 21 } 22 } 23 24 abstract class Animal { 25 public abstract void eat(); 26 } 27 28 class Lion extends Animal { 29 public void eat() { 30 System.out.println("我不吃肉谁敢吃肉!"); 31 } 32 } 33 34 class Monkey extends Animal { 35 public void eat() { 36 System.out.println("我什么都吃,尤其喜欢香蕉。"); 37 } 38 } 39 40 class Pigeon extends Animal { 41 public void eat() { 42 System.out.println("我要减肥,所以每天只吃一点大米。"); 43 } 44 }
运行结果:
6、代码:
1 public class Zoo3 { 2 public static void main(String args[]) { 3 Feeder f = new Feeder("小李"); 4 Animal[] ans = new Animal[16];//饲养员小李喂养一只狮子 5 ans[0] = new Lion();//饲养员小李喂养十只猴子 6 for (int i = 0; i < 10; i++) { 7 ans[1 + i] = new Monkey(); 8 }//饲养员小李喂养5只鸽子 9 for (int i = 0; i < 5; i++) { 10 ans[11 + i] = new Pigeon(); 11 } 12 f.feedAnimals(ans); 13 } 14 } 15 16 class Feeder { 17 public String name; 18 Feeder(String name) { 19 this.name = name; 20 } 21 public void feedAnimals(Animal[] ans) { 22 for (Animal an : ans) { 23 an.eat(); 24 } 25 } 26 } 27 28 abstract class Animal { 29 public abstract void eat(); 30 } 31 32 class Lion extends Animal { 33 public void eat() { 34 System.out.println("我不吃肉谁敢吃肉!"); 35 } 36 } 37 38 class Monkey extends Animal { 39 public void eat() { 40 System.out.println("我什么都吃,尤其喜欢香蕉。"); 41 } 42 } 43 44 class Pigeon extends Animal { 45 public void eat() { 46 System.out.println("我要减肥,所以每天只吃一点大米。"); 47 } 48 }
运行结果:
7、代码:
1 import java.util.Vector; 2 3 public class Zoo4 { 4 public static void main(String args[]) { 5 Feeder f = new Feeder("小李"); 6 Vector<Animal> ans = new Vector<Animal>();//饲养员小李喂养一只狮子 7 ans.add(new Lion());//饲养员小李喂养十只猴子 8 for (int i = 0; i < 10; i++) { 9 ans.add(new Monkey()); 10 }//饲养员小李喂养5只鸽子 11 for (int i = 0; i < 5; i++) { 12 ans.add(new Pigeon()); 13 } 14 f.feedAnimals(ans); 15 } 16 } 17 18 class Feeder { 19 public String name; 20 Feeder(String name) { 21 this.name = name; 22 } 23 public void feedAnimals(Vector<Animal> ans) { 24 for (Animal an : ans) { 25 an.eat(); 26 } 27 } 28 } 29 30 abstract class Animal { 31 public abstract void eat(); 32 } 33 34 class Lion extends Animal { 35 public void eat() { 36 System.out.println("我不吃肉谁敢吃肉!"); 37 } 38 } 39 40 class Monkey extends Animal { 41 public void eat() { 42 System.out.println("我什么都吃,尤其喜欢香蕉。"); 43 } 44 } 45 46 class Pigeon extends Animal { 47 public void eat() { 48 System.out.println("我要减肥,所以每天只吃一点大米。"); 49 } 50 }
运行结果:
8、使用多态最大的好处是:
当你要修改程序并扩充系统时,你需要修改的地方较少,对其它部分代码的影响较小!千万不要小看这两个“较”字!程序规模越大,其优势就越突出。