当前位置:网站首页>装饰器(一)
装饰器(一)
2020-11-08 21:03:00 【8Years】
//装饰器的具体应用;
//例如,把一个声音扩大2倍
// 实现过程:1.声音的接口,2.人这个类,3.扩音器这个类
// 4.把人这个类传进扩音器这个类来,从而实现声音的扩大(所以扩音器里面要有一个构造器(用来传入人这个对象))
public class testDerector {
public static void main(String[] args) {
Person p= new Person();
p.say();
Amplifile amplifile=new Amplifile(p);
amplifile.say();
}
interface Say {
void say();
}
public static class Person implements Say{
private int voice=10;
@Override
public void say() {
System.out.println(this.getVoice());
}
public int getVoice() {
return voice;
}
public void setVoice(int voice) {
this.voice = voice;
}
}
static class Amplifile implements Say{
private Person p;
public Amplifile(Person p) {
this.p = p;
}
@Override
public void say() {
System.out.println("扩音器"+p.getVoice()*2);
}
}
}
输出结果 10 扩音器20
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4583813/blog/4708092
边栏推荐
- 都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
- The interface testing tool eolinker makes post request
- git操作与分支管理规范
- IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
- 不是程序员,代码也不能太丑!python官方书写规范:任何人都该了解的 pep8
- [200 interview experience], programmer interview, common interview questions analysis
- PAT_甲级_1056 Mice and Rice
- CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
- Tasks of the first week of information security curriculum design (analysis of 7 instructions)
- 单例模式的五种设计方案
猜你喜欢
随机推荐
综合架构的简述
趣文分享:C 语言和 C++、C# 的区别在什么地方?
Simulink中封装子系统
动态规划设计:最大子数组
Five factors to consider before choosing API management platform
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
RSA非对称加密算法
ITerm2 配置和美化
Using GaN based oversampling technique to improve the accuracy of model for mortality prediction of unbalanced covid-19
Suffix expression to infix expression
Dynamic programming: maximum subarray
存储过程动态查询处理方法
微信小程序相关
Mycat搭建
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
iptables从入门到掌握
第五章
Regular backup of WordPress website program and database to qiniu cloud
PAT_甲级_1056 Mice and Rice
给大家介绍下,这是我的流程图软件 —— draw.io







