不良代码展示-父类中可以抽象出子类的行为
2011-11-03 17:19
507 查看
原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6932952
把一段代码,业务逻辑剥离后,给大家看看。
基类:
子类1:
子类2:
调用的类:
运行结果当然是对的。可是,子类的行为,却定义在了父类之中了。也就是说,基类的“书”,却根据每本书的颜色去做行为判断的。
if (type == 1) {
System.out.println("Print with the green color.");
} else if (type == 2) {
System.out.println("Print with the blue color.");
} else {
System.out.println("Print with the white color.");
}
这个结构,扩展性不好,也不好维护,也不好阅读。
问当事人,当事人说,我在父类没法知道是哪个子类啊。但是这个业务是可以抽取的,所以就写成这个样子了。
其实,可以这么写的。
基类:
子类1:
子类2:
执行类:
把属于子类的行为,定义成抽象函数,由子类去实现。OK
把一段代码,业务逻辑剥离后,给大家看看。
基类:
package testjava; /** * 书的基类 */ public class BaseBook { private int type = -1; public BaseBook(int type) { this.type = type; } /** * 打印页面 */ public void printPage() { if (type == 1) { System.out.println("Print with the green color."); } else if (type == 2) { System.out.println("Print with the blue color."); } else { System.out.println("Print with the white color."); } } }
子类1:
package testjava; /** * 绿色的书 */ public class GreenBook extends BaseBook { public GreenBook(int type) { super(type); } /** * 得到颜色 * @return 颜色 */ public String getColor() { return "Green"; } }
子类2:
package testjava; /** * 蓝色的书 */ public class BlueBook extends BaseBook { public BlueBook(int type) { super(type); } /** * 得到颜色 * @return 颜色 */ public String getColor() { return "Blue"; } }
调用的类:
package testjava; /** * 执行类 */ public class Testjava { /** * @param args the command line arguments */ public static void main(String[] args) { BlueBook bb = new BlueBook(2); bb.printPage(); GreenBook gb = new GreenBook(1); gb.printPage(); } }
运行结果当然是对的。可是,子类的行为,却定义在了父类之中了。也就是说,基类的“书”,却根据每本书的颜色去做行为判断的。
if (type == 1) {
System.out.println("Print with the green color.");
} else if (type == 2) {
System.out.println("Print with the blue color.");
} else {
System.out.println("Print with the white color.");
}
这个结构,扩展性不好,也不好维护,也不好阅读。
问当事人,当事人说,我在父类没法知道是哪个子类啊。但是这个业务是可以抽取的,所以就写成这个样子了。
其实,可以这么写的。
基类:
package testjava.right; /** * 书的基类 */ public abstract class BaseBook { public abstract String getColor(); /** * 打印页面 */ public void printPage() { System.out.println("Print with the " + getColor() + " color."); } }
子类1:
package testjava.right; /** * 蓝色的书 */ public class BlueBook extends BaseBook { /** * 得到颜色 * @return 颜色 */ public String getColor() { return "Blue"; } }
子类2:
package testjava.right; /** * 绿色的书 */ public class GreenBook extends BaseBook { /** * 得到颜色 * @return 颜色 */ public String getColor() { return "Green"; } }
执行类:
package testjava.right; /** * 执行类 */ public class Testjava { /** * @param args the command line arguments */ public static void main(String[] args) { BlueBook bb = new BlueBook(); bb.printPage(); GreenBook gb = new GreenBook(); gb.printPage(); } }
把属于子类的行为,定义成抽象函数,由子类去实现。OK
相关文章推荐
- 继承体系下除了overridden 技术外使得子类与父类表现出不同行为外,反射也可以,而且反射使得继承不需要overridden
- 继承的概念:继承是指类与类之间的继承关系 ,子类继承父类,子类可以将父类的属性方法继承下来 网址:www.iphonetrain.com 以下代码是了车类与出租车类 共同继承了Car属性
- 为什么父类引用可以指向子类对象 子类引用不能指向父类对象 泛型
- 子类实现父类的抽象方法,父类调用抽象方法
- C++的指针的坑:“new[]的指针需要delete[]”和“子类指针可以转父类指针”的两条规则成功的冲突到了一起
- 如果父类实现并重写了compareTo方法,那么其子类同样具有该方法,可以直接加入TreeSet
- 子类行为不能超过父类行为——由模式谈面向对象的基本原则里氏替换原则
- Android代码学习-嵌套类,嵌套接口,匿名类,接口引用,子类调用父类的方法
- 不良代码展示-Android排序的实现
- 父类实现了序列化,如何阻止子类可以序列化?
- 子类引用变量可以赋给父类引用变量
- 子类实现父类的抽象方法,父类调用抽象方法
- 黑马--程序员 父类中有抽象的方法,必须子类建立对象覆盖才可以使用.
- 泛型限定二---用父类定义比较器,用于子类,减少代码,节约空间
- 将IRepository接口进行抽象,使它成为数据基类的一个对象,这样每个子类都可以有自己的最基础的CURD了
- 类型兼容---子类的对象可以给父类指针赋值
- 不良代码展示-很多判断的写法-给新手
- JAVA继承关系中父类子类代码执行顺序
- 子类实现父类的抽象方法,父类调用抽象方法
- 子类继承父类 ( 代码复用 )