public class Car{
private String name;
private String color;
private String tire;
private Long longs;
public Car(){}
public Car(Long l){
this.longs = l;
}
public Car(String name,String color,Long longs){
this.name = name;
this.color = color;
this.longs = longs;
}
public String getName() {
return name;
}
@InvokeName(description = "这车真不错",thisName = "大师为汽车命名了")
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Long getLongs() {
return longs;
}
public void setLongs(Long longs) {
this.longs = longs;
}
public String getTire() {
return tire;
}
public void setTire(String tire) {
this.tire = tire;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", tire='" + tire + '\'' +
", longs=" + longs +
'}';
}
}
class Test {
public static void main(String[] args) {
Test test = new Test();
try {
Car car = test.test();
System.out.println("=========年轻人的第一台车==========");
System.out.println(car);
} catch (Exception e) {
e.printStackTrace();
}
}
public <R,A extends Car,O extends String> R buildCar(A car,
Consumer<A> drowFunction,//A是参数 有参数没有返回值
Function<A,A> tireFunction,//有返回值 有参数
Supplier<O> countSupplier,//没有参数有返回值
Consumer<A> shellFunction,//有参数没有返回值
FinishFunction<A,R,O> finishFunction,
Supplier<O> annotationProcessor) throws Exception {
if(!Objects.nonNull(car)){
throw new Exception("汽车不存在");
}
//1.装轮胎 (返回这辆车)
A tire = tireFunction.apply(car);
//2.装合适的外壳
shellFunction.accept(tire);
//3.喷漆 Consumer 有参数没有返回值
drowFunction.accept(car);
//4.大师为其命名
O name = countSupplier.get();
annotationProcessor.get();
//5.建造完成
R result = finishFunction.apply(tire,name);
return result;
}
public Car test() throws Exception {
ColorWorker colorWorker = new ColorWorker();
TireWorker tireWorker = new TireWorker();
ShellWorker shellWorker = new ShellWorker();
FinishWorker finishWorker = new FinishWorker();
CountSupplier countSupplier = new CountSupplier();
AnnotationProcessor annotationProcessor = new AnnotationProcessor();
//空车架
Car car = new Car(5L);
return buildCar(
car,
colorWorker::drowColor,
tireWorker::tire,
countSupplier::getName,
shellWorker::shell,
finishWorker::finish,
annotationProcessor::process);
}
}
//创建属于自己的function
interface FinishFunction<A,R,O>{
R apply(A a,O name);
}
interface AnnotationProcessorFunction<A,R,O>{
R apply(A a,O name);
}
//喷漆工
class ColorWorker{
public void drowColor(Car car){
car.setColor("骚红色");
System.out.println("喷漆");
}
}
//轮胎工
class TireWorker{
public Car tire(Car car){
System.out.println("装轮胎");
car.setTire("米其林轮胎");
return car;
}
}
//外壳工
class ShellWorker{
public void shell(Car car){
System.out.println("装外壳");
}
}
//完成工
class FinishWorker{
public Car finish(Car car,String name){
System.out.println("出厂");
car.setName(name);
return car;
}
}
class CountSupplier{
public String getName(){
return "小米su7";
}
}
//注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface InvokeName{
String thisName() default "小米";
String description() default "";
}
class AnnotationProcessor{
public String process(){
for (Method method : Car.class.getDeclaredMethods()) {
if (method.isAnnotationPresent(InvokeName.class)) {
InvokeName invokeName = method.getAnnotation(InvokeName.class);
System.out.println(invokeName.thisName());
System.out.println(invokeName.description());
}
}
return "";
}
}
Java Consumer,Function,Supplier函数
于 2024-04-09 23:28:28 首次发布
文章描述了一个名为Car的Java类,包含属性和构造方法,以及Test类中使用装饰器模式构建Car对象的过程。同时提及了@InvokeName注解用于方法命名。
摘要由CSDN通过智能技术生成