最后
终极手撕架构师的学习笔记:分布式+微服务+开源框架+性能优化
System.out.println("吃饭");
}
}
//NewPerson,Man,Woman同属于Person体系中,要拓展Man或者Woman的eat功能
//不需要通过Man或者Woman的子类来实现,可以把拓展功能统一定义在NewPerson(装饰类)中。
//进而在Man或者Woman拓展功能时直接被NewPerson装饰就可以实现功能拓展。
class NewPerson extends Person{
private Person p;
NewPerson(Person p){
this.p=p;
}
void eat(){
System.out.println("开胃酒");
p.eat();
System.out.println("吃点水果");
}
}
class Woman extends Person{
void eat(){
System.out.println("女人吃饭");
}
}
class Man extends Person{
void eat(){
System.out.println("男人吃饭");
}
}
一:继承
如通过子类继承来拓展Man类的eat功能
//如果想增强男人的eat功能,就要继承Man,这样通过子类SubMan来增强。
class SubMan extends Man{
void eat(){
System.out.println("开胃酒");
super.eat();
System.out.println("吃点水果");
System.out.println("饭后来根烟");
}
}
通过继承来拓展Woman类的eat功能还需要像Man一样,这样最好导致代码比较臃肿,为了避免这种臃肿,同时为了提高效率,就用到了装饰模式。
class Man extends Person{
void eat(){
System.out.println("男人吃饭");
}
}
//如果想增强男人的eat功能,就要继承Man,这样通过子类SubMan来增强。
class SubMan extends Man{
void eat(){
System.out.println("开胃酒");
super.eat();
System.out.println("吃点水果");
System.out.println("饭后来根烟");
}
}
class SubWoman extends Woman{
void eat(){
System.out.println("开胃酒");
super.eat();
System.out.println("吃点水果");
System.out.println("饭后来根烟");
}
}
二:装饰模式
通过装饰模式来要拓展Man或者Woman的eat功能,就没必要再生成子类了
NewPerson,Man,Woman同属于Person体系中,要拓展Man或者Woman的eat功能
不需要通过Man或者Woman的子类来实现,可以把拓展功能统一定义在NewPerson(装饰类)中。
进而在Man或者Woman拓展功能时直接被NewPerson装饰就可以实现功能拓展。
写完后调用一下试试。
public static void main(String[]args){
Man m=new Man();
NewPerson newm=new NewPerson(m);
newm.eat();
Woman w=new Woman();
NewPerson ww=new NewPerson(w);
ww.eat();
}
最后总结我的面试经验
2021年的金三银四一眨眼就到了,对于很多人来说是跳槽的好机会,大厂面试远没有我们想的那么困难,摆好心态,做好准备,你也可以的。
另外,面试中遇到不会的问题不妨尝试讲讲自己的思路,因为有些问题不是考察我们的编程能力,而是逻辑思维表达能力;最后平时要进行自我分析与评价,做好职业规划,不断摸索,提高自己的编程能力和抽象思维能力。
BAT面试经验
实战系列:Spring全家桶+Redis等
其他相关的电子书:源码+调优
面试真题:
07980)]
[外链图片转存中…(img-JiaR1UF2-1715566007981)]