黑马day11 装饰类
来源:互联网 发布:淘宝代金券最多 编辑:程序博客网 时间:2024/06/06 16:33
如果要修该一个类中的某个方法一共有三种方法:继承,写一个装饰类,动态代理
这里我主要介绍装饰类:
1.写一个interface Animal
package com.itheima.pool;
public interface Animal {
public void eat();
public void bite();
}
2.写一个Dog类继承Animal接口:
package com.itheima.pool;
public class Dog implements Animal{
public void eat(){
System.out.println("dog is eatting...");
}
public void bite(){
System.out.println("dog is bitting...");
}
}
3.写一个装饰类DecorateDog类同时也继承和Dog同样的接口:
在这个类中提供了一个构造方法,有一个Animal的接口参数,为了是在new DecorateDog的时候传入Dog对象....相当于多态
package com.itheima.pool;
public class DecorateDog implements Animal{
private Animal ani=null;
public DecorateDog(Animal ani){
this.ani=ani;
}
@Override
public void eat() {
ani.eat();
}
@Override
public void bite() {
System.out.println("dog is mie mie....");
}
}
4.写一个测试类DogTest
package com.itheima.pool;
public class DogTest {
public static void main(String[] args) {
Dog dog= new Dog();
Animal ani=new DecorateDog(dog);
ani.bite();
}
}
5.运营结果:
结果运行的是mie mie...这是因为调用了装饰类中被修改的方法。
- 黑马day11 装饰类
- 黑马程序员-day11多线程
- 黑马程序员-装饰类
- 黑马程序员_java基础day11
- 黑马程序员-Java多线程-day11
- 黑马程序员-day11-多线程(概述)
- 黑马程序员--Java基础Day11
- 黑马day11 事务入门案例
- 黑马day11 dbcp连接池
- 黑马day11 c3p0数据库连接池
- day11
- DAY11
- day11
- day11
- day11
- day11
- Day11
- day11
- Hadoop中两表JOIN的处理方法
- DragonFlow导读(by quqi99)
- 前端优化:DNS预解析提升页面速度
- 【组合】Bzoj3142 数列[HNOI2013]
- Stanford UFLDL教程 池化Pooling
- 黑马day11 装饰类
- 程序员必须知道的8大排序和3大查找
- Redis 类型检查和命令多态
- 黑马程序员------学习Comparator和Comparable方法理解
- 成功CRS启动的系统日志
- JavaScript中的数组
- 神经网络中的Early Stop
- [费马小定理]Bzoj3260 跳
- C#RAS加密算法