当前位置:网站首页>装饰器(一)
装饰器(一)
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
边栏推荐
- [cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
- AI perfume is coming. Will you buy it?
- Creating a text cloud or label cloud in Python
- 在Python中创建文字云或标签云
- LeetCode 45 跳跃游戏II
- Dynamic planning
- 解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
- Looking for a small immutable dictionary with better performance
- 寻找性能更优秀的不可变小字典
- 【200人面试经验】,程序员面试,常见面试题解析
猜你喜欢
后缀表达式转中缀表达式
Five design schemes of singleton mode
Mycat搭建
如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?
CMS垃圾收集器
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
Development and deployment of image classifier application with fastai
Django's simple user system (3)
Simulink中封装子系统
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
随机推荐
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
Constructors and prototypes
微信小程序相关
Why need to use API management platform
200人的程序员面试经验,都在这里了
第五章
Introduction and application of swagger
Five factors to consider before choosing API management platform
SQL quick query
趣文分享:C 语言和 C++、C# 的区别在什么地方?
PAT_甲级_1056 Mice and Rice
go语言参数传递到底是传值还是传引用?
VirtualBox安装centos7
单例模式的五种设计方案
Implementation of warehouse management system with ABP (net core) + easyUI + efcore
Brief VIM training strategy
Is parameter passing in go language transfer value or reference?
Part I - Chapter 1 Overview
Part 1 - Chapter 2 pointer operation
在Python中创建文字云或标签云