创建了一个 Info
的泛型类,用来存储数据。
名称 | 介绍 |
---|---|
Solution.upperLimit() | 接收一个 Info ,只能接收 Number 及其 Number 的子类,并获取 Info 中的值返回 |
Solution.lowerLimit() | 接收一个 Info ,只能接收 String 或 Object 类型的泛型,并获取 Info 中的值返回 |
Info.T | 保存数据的位置 |
Info.set(T t) | 将传入的数据保存下来 |
Info.get() | 获取保存的数据 |
请您在 Solution
类中实现方法 upperLimit()
和 lowerLimit()
的功能。
符合要求通过时显示:
Verification passed.
验证失败,返回结果:
Verification failure.
info.java
public class Info<T> {
private T t;
public void set(T t){
this.t = t;
}
public T get(){
return t;
}
}
Main.java
import java.io.FileReader;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
PrintStream ps = null;
try {
String inputPath = args[0];
String outputPath = args[1];
Scanner in = new Scanner(new FileReader(inputPath));
ps = new PrintStream(outputPath);
// get data
Solution solution = new Solution();
Class<? extends Solution> aClass = solution.getClass();
Method[] declaredMethods = aClass.getDeclaredMethods();
int index = 0;
for (Method md : declaredMethods) {
if("upperLimit".equals(md.getName())) {
Type[] genericParameterTypes = md.getGenericParameterTypes();
for (Type type : genericParameterTypes) {
if (type.getTypeName().contains("Number")) {
index++;
}
}
if (md.getReturnType().getName().contains("Number")) {
index++;
}
}
if("lowerLimit".equals(md.getName())) {
Type[] genericParameterTypes = md.getGenericParameterTypes();
for (Type type : genericParameterTypes) {
if (type.getTypeName().contains("String")) {
index++;
}
}
if (md.getReturnType().getName().contains("Object")) {
index++;
}
}
}
if (index < 4) {
ps.println("Verification failure.");
return;
}
Info<Integer> info = new Info<>();
info.set(200);
Number number = solution.upperLimit(info);
if(!number.equals(200)) {
ps.println("Verification failure.");
return;
}
Info<String> infos = new Info<>();
infos.set("Xx");
Object oj = solution.lowerLimit(infos);
if(!oj.equals("Xx")) {
ps.println("Verification failure.");
return;
}
ps.println("Verification passed.");
ps.close();
in.close();
} catch (Exception e) {
ps.println("Verification failure.");
}
}
}
例解
Solution.java
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
public class Solution {
public Number upperLimit(Info<? extends Number>s){
return s.get();
}
public Object lowerLimit(Info<? super String>s){
return s.get();
}
}