当前位置:网站首页>装饰器(一)
装饰器(一)
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
边栏推荐
- 在Python中创建文字云或标签云
- Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
- opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
- Brief VIM training strategy
- Proficient in high concurrency and multithreading, but can't use ThreadLocal?
- 快来看看!AQS 和 CountDownLatch 有怎么样的关系?
- PAT_甲级_1056 Mice and Rice
- 如何将PyTorch Lightning模型部署到生产中
- Development and deployment of image classifier application with fastai
- Programmers should know the URI, a comprehensive understanding of the article
猜你喜欢
MongoDB数据库
接口测试工具Eolinker进行post请求
信息安全课程设计第一周任务(7条指令的分析)
Why need to use API management platform
The interface testing tool eolinker makes post request
Development and deployment of image classifier application with fastai
Iterm2 configuration and beautification
选择排序
Iptables from introduction to mastery
给大家介绍下,这是我的流程图软件 —— draw.io
随机推荐
Five factors to consider before choosing API management platform
深拷贝
Constructors and prototypes
【Elasticsearch 技术分享】—— 十张图带大家看懂 ES 原理 !明白为什么说:ES 是准实时的!
给大家介绍下,这是我的流程图软件 —— draw.io
Looking for a small immutable dictionary with better performance
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
PAT_ Grade A_ 1056 Mice and Rice
不是程序员,代码也不能太丑!python官方书写规范:任何人都该了解的 pep8
Using GaN based oversampling technique to improve the accuracy of model for mortality prediction of unbalanced covid-19
Newbe.ObjectVisitor 样例 1
An online accident caused by improper use of thread pool
Select sort
RSA asymmetric encryption algorithm
使用Fastai开发和部署图像分类器应用
Looking for better dynamic getter and setter solutions
趣文分享:C 语言和 C++、C# 的区别在什么地方?
后缀表达式转中缀表达式
Express框架
线程池运用不当的一次线上事故