免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4551 | 回复: 3
打印 上一主题 下一主题

假如有很多if else,为了体现面向对象,用什么来代替呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-10 01:06 |只看该作者 |倒序浏览
if(animal.IsCat()) {  }  
else if(animal.IsDog()) {  }
else if(animal.IsKoala()) {  }  
. . .  else if(A.isType(Type10)) { }
上面的代码段中使用一个对象命名为“动物。“假设有一个动物的类层次结构。下面哪一项技术可以取代是以if - then - else构造
1持久?
2Messaging
3抽象
4封装
5多态

论坛徽章:
0
2 [报告]
发表于 2012-08-10 19:55 |只看该作者
  1. /**
  2. * File:Animal.java
  3. * ------------------
  4. * Polymorphism
  5. */
  6. package org.cudemo;

  7. /**
  8. * @author isaacxu
  9. *  *
  10. */
  11. public class Animal{
  12.         public void spark( ) {
  13.           
  14.           }

  15. }
复制代码
  1. /**
  2. * File:Cat.java
  3. * -----------------
  4. * Cat objects are Animal
  5. * because they have the same interface:
  6. */
  7. package org.cudemo;

  8. public class Cat extends Animal{
  9.         // Redefine interface method
  10.         public void spark() {
  11.                 System.out.println("I am a Cat!");
  12.                   
  13.           }

  14. }
复制代码
  1. /**
  2. * File:Dog.java
  3. * ---------------
  4. * Dog objects are Animal
  5. * because they have the same interface:
  6. */
  7. package org.cudemo;

  8. public class Dog extends Animal{
  9.         // Redefine interface method
  10.         public void spark( ) {
  11.                 System.out.println("I am a Dog!");   
  12.           }

  13. }
复制代码
  1. /**
  2. * File:Koala.java
  3. * -------------------------
  4. * Koala objects are Animal
  5. * because they have the same interface:
  6. */
  7. package org.cudemo;

  8. /**
  9. * @author isaacxu
  10. *
  11. */
  12. public class Koala extends Animal{
  13.         // Redefine interface method
  14.         public void spark( ) {
  15.                 System.out.println("I am a Koala!");   
  16.           }

  17. }
复制代码
  1. /**
  2. * File:Sound.java
  3. * ---------------
  4. * Test file like <Think in Java>:Inheritance & upcasting
  5. */
  6. package org.cudemo;

  7. public class Sound {

  8.         public static void barking(Animal a) {
  9.             // ...
  10.             a.spark();
  11.           }
  12.         public static void barkingAll(Animal[] all) {
  13.             for(Animal i : all)
  14.               barking(i);
  15.           }       
  16.         /**
  17.          * @param args
  18.          */
  19.         public static void main(String[] args) {
  20.                
  21.                 Animal[] pets = {
  22.                                 new Cat(),
  23.                                 new Dog(),
  24.                                 new Koala()
  25.                                 //.....
  26.                 };
  27.                 barkingAll(pets);

  28.         }

  29. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-08-11 11:32 |只看该作者
回复 2# isaacxu


    谢谢啊,例子很好
这个叫多态么?

论坛徽章:
0
4 [报告]
发表于 2012-08-13 18:48 |只看该作者
看你的代码差不多是多态,但还是要看具体操作了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP