如下例子看起來一樣都能達到目的。
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;
}
}
uj5u.com熱心網友回復:
在你本例中沒有區別,因為你沒有用到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();
}uj5u.com熱心網友回復:
你好,第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;
}
}
uj5u.com熱心網友回復:
限定的范圍不一樣,前者引數回傳值都要求是同一型別,后者就不一定了,你可以傳入數字,然后回傳任意比如字串或定義的物件等。uj5u.com熱心網友回復:
看來你還是沒理解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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/26053.html
標籤:Java SE
下一篇:idea git 使用問題
