我正在修改一本书中的示例类,并尝试将其转换为使用泛型来帮助我了解它是如何工作的。你可以在我尝试的地方看到我的代码。通用A,B和C曾经是String String和int。
public class Student<A,B,C> implements Person {
A id;
B name;
C age;
public Student(A i, B n, C a) {
id = i;
name = n;
age = a;
}
protected int studyHours() {return age/2;}
public A getID() {return id;}
public B getName() {return name;}
public C getAge() {return age;}
public boolean equals(Person other) {
if(!(other instanceof Student)) return false;
Student s = (Student) other;
return id.equals(s.id);
}
public String toString() {
return "Student(ID: " + id + ", Name: " + name + ", Age: " + age + ")";
}
public static void main(String[] var0) {
Student<String,String,int> studentOne = new Student("123", "Guillermo", 34);
Student<String,String,int> studentTwo = new Student("345", "Cheryl", 35);
int numberOfStudyHours;
numberOfStudyHours = studentOne.studyHours();
System.out.println("Guillermo studies " +numberOfStudyHours+ " hours");
}
}
人员界面位于
之下public interface Person {
public boolean equals(Person other); //is this the same person?
public String getName();
public int getAge();
}
非常感谢您的专家指导。我收到了这个错误:
Student.java:4: error: Student is not abstract and does not override abstract method getAge() in Person
public class Student<A,B,C> implements Person {
^
Student.java:18: error: getAge() in Student cannot implement getAge() in Person
public C getAge() {return age;}
^
return type C is not compatible with int
where C is a type-variable:
C extends Object declared in class Student
Student.java:17: error: getName() in Student cannot implement getName() in Person
public B getName() {return name;}
^
return type B is not compatible with String
where B is a type-variable:
B extends Object declared in class Student
Student.java:15: error: bad operand types for binary operator '/'
protected int studyHours() {return age/2;}
^
first type: C
second type: int
where C is a type-variable:
C extends Object declared in class Student
Student.java:28: error: unexpected type
Student<String,String,int> studentOne = new Student("123", "Guillermo", 34);
^
required: reference
found: int
Student.java:29: error: unexpected type
Student<String,String,int> studentTwo = new Student("345", "Cheryl", 35);
^
required: reference
found: int
答案 0 :(得分:1)
有关泛型有意义的更好示例,请查看各种Java Collections类。例如,将Map从任何内容转换为其他内容(String to Integer,Integer to String,Integer to Integer,Person to String)是有意义的。您只需要一个类来实现所有这些映射,但是通过使用其类型实例化每个映射,
Map<Person,Person> motherMap = new HashMap<>();
编译器和IDE知道必须为get()
方法提供什么类型的密钥,以及结果的类型,从而防止在开始时出现大量编码错误。
另一个查找仿制药效果良好的示例的地方是Google图书馆:请参阅GuavaExplained。
对于练习练习,您可以尝试使用左右子节点实现二叉树,其中树节点都是相同的泛型类型T.
对于编译错误,Generics不能用于原始类型 - 小写的类似boolean和int-尽管你可以使用Boolean和Integer。此外,界面通常也是通用的:HashMap<K,V> implements Map<K,V>
答案 1 :(得分:0)
对于您的示例,您似乎应该将界面更改为:
public interface Person<B,C> {
public boolean equals(Person other); //is this the same person?
public B getName();
public C getAge();
}
然后
public class Student<A,B,C> implements Person<B,C> {
A id;
B name;
C age;
...
}
But this isn't really a good use of generics。另请注意,您不能将int
或其他原语用于泛型。你必须使用Integer
或其他拳击课。