请教两种泛型方法有什么不同。

SilentHunter460 2020-08-09 01:14:16
如下例子看起来一样都能达到目的。


public class WildCardNeedDemo {

public static void main(String[] args) {
GenericStack<Integer> intStack = new GenericStack<>();
intStack.push(1);
intStack.push(2);
intStack.push(-2);
System.out.println("the max number is: " + max1(intStack));
System.out.println("the max number is: " + max2(intStack));
}

public static <type extends Number> double max1(GenericStack<type> stack) {
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return max;
}

public static double max2(GenericStack<? extends Number> stack) {
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return max;
}
}
...全文
3123 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2020-08-10
  • 打赏
  • 举报
回复
看来你还是没理解2L的例子
不管你用自定义的类型,还是系统自带的类型,都不影响他们的区别
用你的代码来说明吧

public class question {

public static void main(String[] args) {
ArrayList<Integer> intStack = new ArrayList<>();
intStack.add(1);
intStack.add(2);
intStack.add(-4);
System.out.println("the max number is: " + max1(intStack));
Integer max1 = max1(intStack); //区别:这里不需要强行转换

ArrayList<Integer> intStack2 = new ArrayList<>();
intStack2.add(1);
intStack2.add(2);
intStack2.add(-4);
System.out.println("the max number is: " + max2(intStack2));
System.out.println(intStack2.size());
Integer max2 = (Integer)max2(intStack2); //这里需要强行转换,因为没法用?作为返回值类型
}

static <type extends Number> type max1(ArrayList<type> stack) { //这里返回值可以直接用type类型
type max = stack.get(0);
for (type t : stack) { //这里能用type来for循环
if (t.doubleValue() > max.doubleValue()) {
max = t;
}
}
return max;
}

static Number max2(ArrayList<? extends Number> stack){ //这里返回值不能用?作为类型
Number max = stack.get(0);
for (Number n: stack) { //这里不能用?来for循环
if (n.doubleValue() > max.doubleValue()) {
max = n;
}
}
return max;
}
}
qybao 2020-08-09
  • 打赏
  • 举报
回复
在你本例中没有区别,因为你没有用到type
但是,如果你改成以下就有区别
      public static <type extends Number> type max1(GenericStack<type> stack) { //返回值类型改成type
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return stack.pop();
}

public static Number max2(GenericStack<? extends Number> stack) { //你会发现这里的返回值类型不能改成[?],这就是区别
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return stack.pop();
}


鸡窝里的毛 2020-08-09
  • 打赏
  • 举报
回复
限定的范围不一样,前者参数返回值都要求是同一类型,后者就不一定了,你可以传入数字,然后返回任意比如字符串或定义的对象等。
SilentHunter460 2020-08-09
  • 打赏
  • 举报
回复
引用 1 楼 qybao 的回复:
在你本例中没有区别,因为你没有用到type 但是,如果你改成以下就有区别
你好,第1个帖子用的GenericStack是自己写的类没贴出来,不利于讨论问题。重新写一个例子请教一下:

public class question {

      public static void main(String[] args) {
            ArrayList<Integer> intStack = new ArrayList<>();
            intStack.add(1);
            intStack.add(2);
            intStack.add(-4);
            System.out.println("the max number is: " + max1(intStack));

            ArrayList<Integer> intStack2 = new ArrayList<>();
            intStack2.add(1);
            intStack2.add(2);
            intStack2.add(-4);
            System.out.println("the max number is: " + max2(intStack2));
            System.out.println(intStack2.size());
      }

      static <type extends Number> double max1(ArrayList<type> stack) {
            double max = stack.remove(stack.size() - 1).doubleValue();
            while(!stack.isEmpty()){
                  double value = stack.remove(stack.size()-1).doubleValue();
                  if(value>max){
                        max = value;
                  }
            }
            return max;
      }

      static double max2(ArrayList<? extends Number> stack){
            double max = stack.remove(stack.size() - 1).doubleValue();
            while(!stack.isEmpty()){
                  double value = stack.remove(stack.size()-1).doubleValue();
                  if(value>max){
                        max = value;
                  }
            }
            return max;
      }
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧