加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA 装饰者模式

发布时间:2020-12-15 07:17:28 所属栏目:Java 来源:网络整理
导读:public interface Person { void introduce();} class Student implements Person { public Student() { // TODO Auto-generated constructor stub } introduce() { System.out.println( "我要去听课" ); }} class Teacher Teacher() { } @Override introduc
public interface Person {
    void introduce();
}
class Student implements Person { public Student() { // TODO Auto-generated constructor stub } introduce() { System.out.println("我要去听课"); } }
class Teacher Teacher() { } @Override introduce() { TODO Auto-generated method stub System.out.println("我要去讲课"); } }

实现方法

class Introduce Person { Person person; Introduce() { Introduce(Person person) { this.person = person; } @Override TODO Auto-generated method stub System.out.println("大家好"); person.introduce(); } }

主函数

class Test { Test() { static main(String[] args) { TODO Auto-generated method stub Student student = new Student(); Introduce sIntroduce = Introduce(student); sIntroduce.introduce(); Teacher teacher = Teacher(); Introduce tIntroduce = Introduce(teacher); tIntroduce.introduce(); } }

运行结果

大家好 我要去听课 大家好 我要去讲课

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读