gpt4 book ai didi

java - 试图理解 Java 中的泛型

转载 作者:行者123 更新时间:2023-11-30 11:08:53 27 4
gpt4 key购买 nike

我正在修改一本书中的示例类,并尝试将其转换为使用泛型来帮助我了解它的工作原理。您可以在下面我尝试的地方看到我的代码。泛型 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

最佳答案

有关泛型有意义的一些更好的示例,请查看各种 Java Collections类。例如,从任何事物到任何事物(字符串到整数、整数到字符串、整数到整数、人到字符串)创建一个映射是有意义的。您只需要一个类来实现所有这些映射,但是通过实例化每个映射及其类型,

Map<Person,Person> motherMap = new HashMap<>();

编译器和 IDE 知道必须给 get() 什么样的 key 方法以及结果的类型,从而在一开始就防止大量编码错误。

寻找使用泛型产生良好效果的示例的另一个地方是 google 库:参见 GuavaExplained .

作为练习,您可以尝试实现具有左右子节点的二叉树,其中树节点都是相同的泛型类型 T。

对于编译错误,泛型不能用于基本类型——小写类型,如 boolean 和 int——尽管你可以使用 Boolean 和 Integer。此外,接口(interface)通常也是通用的:HashMap<K,V> implements Map<K,V>

关于java - 试图理解 Java 中的泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28358047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com