我正在测试我对Java中的泛型的知识,我遇到了一个问题。
代码
return (t%2==0);
在我的IsEven.java中不会编译,以及
int comparison = t.compareTo(memberT);
在我的GreaterThan.java中
我认为这是如何设置类的通用类型的错误?任何额外的帮助将不胜感激。
ArrayTester.java
import java.util.Arrays;
public class ArrayTester<T> {
private T[] myArray;
CustomTest<T> tester;
public ArrayTester(T[] tArray, CustomTest<T> cTest)
{
myArray = tArray.clone();
tester = cTest;
Arrays.sort(myArray);
}
public void printIfValid()
{
for(int i = 0; i < myArray.length; i++)
{
if(tester.test(myArray[i]))
{
System.out.print(myArray[i] + " ");
}
}
System.out.println();
}
public int countIfValid()
{
int tracker = 0;
for(int i = 0; i < myArray.length; i++)
{
if(tester.test(myArray[i]))
{
tracker++;
}
}
return tracker;
}
}
CustomTest.java
public interface CustomTest<T> {
public boolean test(T t);
}
AllLower.java
public class AllLower implements CustomTest<String> {
public boolean test(String t) {
String lowerCaseVersion = t.toLowerCase();
return t.equals(lowerCaseVersion);
}
}
GreaterThan.java
public class GreaterThan<T extends Object> implements CustomTest<T> {
T memberT;
@Override
public boolean test(T t) {
int comparison = t.compareTo(memberT); //why doesnt this work?
if(comparison == 1)
{
return true;
}else{
return false;
}
}
public GreaterThan(T t)
{
memberT = t;
}
}
ECTester.java
public class ECTester {
public static void main(String[] args) {
Integer[] intArray = {2, 8, 6, 7, 4, 10, 3, 1, 9, 5};
Double[] dblArray = {11.9, 12.2, 5.4, 0.2, 3.3, 3.9, 4.2, 1.1, 123456.0, 10.0001};
String[] strArray = {"Why", "is", "A", "raven", "liKe", "a", "wriTing", "desk?"};
ArrayTester<Integer> intTest = new ArrayTester<Integer>(intArray, new IsEven<Integer>());
ArrayTester<Double> dblTest = new ArrayTester<Double>(dblArray, new IsEven<Double>());
ArrayTester<String> strTest = new ArrayTester<String>(strArray, new AllLower());
ArrayTester<Integer> intCompare = new ArrayTester<Integer>(intArray, new GreaterThan<Integer>(4));
ArrayTester<Double> dblCompare = new ArrayTester<Double>(dblArray, new GreaterThan<Double>(4.5));
ArrayTester<String> strCompare = new ArrayTester<String>(strArray, new GreaterThan<String>("last"));
System.out.println("There are " + intTest.countIfValid() + " valid integers");
System.out.println("They are: ");
intTest.printIfValid();
System.out.println();
System.out.println("There are " + dblTest.countIfValid() + " valid doubles");
System.out.println("They are: ");
dblTest.printIfValid();
System.out.println();
System.out.println("There are " + strTest.countIfValid() + " valid strings");
System.out.println("They are: ");
strTest.printIfValid();
System.out.println();
System.out.println("There are " + intCompare.countIfValid() + " integers greater than 4");
System.out.println("They are: ");
intCompare.printIfValid();
System.out.println();
System.out.println("There are " + dblCompare.countIfValid() + " doubles greater than 4.5");
System.out.println("They are: ");
dblCompare.printIfValid();
System.out.println();
System.out.println("There are " + strCompare.countIfValid() + " strings greater than \"last\"");
System.out.println("They are: ");
strCompare.printIfValid();
System.out.println();
}
}
IsEven.java
public class IsEven<T extends Number> implements CustomTest<T>{
@Override
public boolean test(T t) {
return (t%2==0);
}
//Can't figure out why it wont accept this
}
答案 0 :(得分:0)
不幸的是,像%
这样的数字运算符不能作为多态结构使用:操作数的类型需要在编译时知道,这意味着你不能将它与任意{{Number
一起使用1}}。
自动装箱/取消装箱仅适用于从特定原始包装类型(例如java.lang.Integer
)到相应基元类型(即int
)的情况; unbox to Number
没有原始对应物。
您可以使用longValue()
或doubleValue()
来提取您可以使用的long
或double
{{1}使用,或者可能使用带有%
检查的if / else链。这不会完全解决您的问题,因为instanceof
是一个开放类型 - 存在非原始实现。 (Number
和BigInteger
是众所周知的,但第三方库也可以添加自己的BigDecimal
实现。)
关于Number
的其他问题,如上面的评论所述,您需要compareTo()
才能确保T extends Comparable<? super T>
方法存在。