泛型的上下限

创建了一个 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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值