https://www.v2ex.com/t/689044
package com.example.genericparadigmdemo.paradigm.calculator;
public interface Calculator<T> {
//泛型
public T add(T operand1, T operand2);
public T addMany(T...operands);
public T sub(T operand1, T operand2);
public T multiplication(T...operands);
}
package com.example.genericparadigmdemo.paradigm.calculator;
import org.springframework.stereotype.Component;
@Component
public class IntegerCalculator implements Calculator<Integer> {
@Override
public Integer add(Integer operand1, Integer operand2) {
return operand1 + operand2;
}
@Override
public Integer addMany(Integer... operands) {
Integer result = 0;
for (Integer oper : operands) {
result += oper;
}
return result;
}
@Override
public Integer sub(Integer operand1, Integer operand2) {
return operand1 - operand2;
}
@Override
public Integer multiplication(Integer... operands) {
Integer result = 1;
for (Integer oper : operands) {
result *= oper;
}
return result;
}
public static void main(String[] args) {
IntegerCalculator c1 = new IntegerCalculator();
System.out.println(c1.add(1, 2));
Calculator<Integer> c2 = new IntegerCalculator();
System.out.println(c2.add(3, 4));
Calculator<Integer> c3 = new IntegerCalculator();
System.out.println(c3.multiplication(3,2,2));
Calculator<Integer> c4 = new IntegerCalculator();
System.out.println(c4.addMany(1,2,311,412,3,21,3));
}
}
package com.example.genericparadigmdemo.paradigm.calculator;
import com.example.genericparadigmdemo.paradigm.ParadigmFormatter;
public class IntervalCalculator implements Calculator<ParadigmFormatter<Integer>> {
@Override
public ParadigmFormatter<Integer> add(ParadigmFormatter<Integer> operand1, ParadigmFormatter<Integer> operand2) {
int lower = operand1.getLower() + operand2.getLower();
int upper = operand1.getUpper() + operand2.getUpper();
return new ParadigmFormatter<Integer>(lower,upper);
}
public static void main(String[] args) {
Calculator<ParadigmFormatter<Integer>> c = new IntervalCalculator();
ParadigmFormatter<Integer> operand1 = new ParadigmFormatter<>(1,2);
ParadigmFormatter<Integer> operand2 = new ParadigmFormatter<>(3,4);
ParadigmFormatter<Integer> operand3 = c.add(operand1, operand2);
System.out.println(operand3.getLower());
}
@Override
public ParadigmFormatter<Integer> addMany(ParadigmFormatter<Integer>... operands) {
return null;
}
@Override
public ParadigmFormatter<Integer> sub(ParadigmFormatter<Integer> operand1, ParadigmFormatter<Integer> operand2) {
return null;
}
@Override
public ParadigmFormatter<Integer> multiplication(ParadigmFormatter<Integer>... operands) {
return null;
}
}
package com.example.genericparadigmdemo.paradigm.calculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OperationController {
}
package com.example.genericparadigmdemo.paradigm;
public class ParadigmFormatter<T> {
private T lower;
private T upper;
public ParadigmFormatter(T lower, T upper){
this.lower = lower;
this.upper = upper;
}
public T getLower(){
return lower;
}
public T getUpper(){
return upper;
}
}