当前位置:网站首页>装饰器(一)
装饰器(一)
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
边栏推荐
- JVM真香系列:轻松理解class文件到虚拟机(下)
- . net core cross platform resource monitoring library and dotnet tool
- How to deploy pytorch lightning model to production
- opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
- IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
- Introduction and application of swagger
- Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
- RSA非对称加密算法
- 接口测试工具Eolinker进行post请求
- Wechat applet related
猜你喜欢

iptables从入门到掌握

Tasks of the first week of information security curriculum design (analysis of 7 instructions)

数组初相识

Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan

C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?

Exercise 5

PAT_甲级_1056 Mice and Rice

Creating a text cloud or label cloud in Python

选择API管理平台之前要考虑的5个因素

Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
随机推荐
后缀表达式转中缀表达式
选择API管理平台之前要考虑的5个因素
CMS垃圾收集器
C / C + + learning diary: original code, inverse code and complement code
给大家介绍下,这是我的流程图软件 —— draw.io
11 important operations of Python list
Leetcode 45 jumping game II
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
SQL quick query
C/C++学习日记:原码、反码和补码
Constructors and prototypes
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
Programmers should know the URI, a comprehensive understanding of the article
.NET Core 跨平台资源监控库及 dotnet tool 小工具
接口测试用例思路总结
Simple process of reading pictures by QT program developed by Python
采用注解+拦截器的方式进行异步执行的实现方式
解决go get下载包失败问题
单例模式的五种设计方案
Regular backup of WordPress website program and database to qiniu cloud