【发布时间】:2017-08-27 02:54:18
【问题描述】:
我是 Java 新手,来自 c++ 背景。我在制作具有可比接口的泛型类时遇到了问题。在 LinkedList 类的 SearchByID 和 SerchByName 方法中,它在 temp.value.getID() 和 temp.value.getName() 上给出错误,我正在 main 中创建 Linkedlist 类的对象,因此根据我的理解 temp.value 应该给出我是一个员工对象,我正在为其调用 getID,它是 Employee 类的一个函数。但它仍然给我这个错误。有什么我理解不正确的吗?或在此代码中出错。
找不到符号符号:方法 getID()
public class Employee implements Comparable<Employee>
{
private int empID;
private String name;
private int salary;
private boolean manager;
private int subordinates;
public Employee()
{
empID = 0;
name = "";
salary = 0;
manager = false;
subordinates = 0;
}
public Employee(int id , String name , int salary , boolean manager , int sub)
{
empID = id;
this.name = name;
this.salary = salary;
this.manager = manager;
subordinates = sub;
}
public int getID()
{
return this.empID;
}
public String getName()
{
return this.name;
}
@Override
public int compareTo(Employee other)
{
if (this.empID < other.empID)
{
return -1;
}
else if (this.empID > other.empID)
{
return 1;
}
else
{
return 0;
}
}
这是我的链表类
public class LinkedList<T extends Comparable<T>>
{
private int count;
private Node<T> head;
//completely encapsulated Node class from outer world as they dont need it
private class Node<T>
{
public T value;
public Node<T> next;
public Node(T data)
{
this.value = data;
this.next = null;
}
}
LinkedList()
{
count = 0;
head = null;
}
public Node<T> SearchByID(int id)
{
Node<T> temp ;
for (temp = head; temp.value.getID() != id; temp = temp.next);
return temp;
}
public Node<T> SearchByname(String name)
{
Node<T> temp ;
for (temp = head; temp.value.getName() != name; temp = temp.next);
return temp;
}
【问题讨论】:
-
你的主要类实现是什么?分享问题本身。
-
将
class LinkedList<T extends Comparable<T>>更改为:class LinkedList<T extends Employee>
标签: java class generics cannot-find-symbol