Я сталкиваюсь с ошибкой NoSuchMethodError, когда пытаюсь вызвать метод добавления в экземпляре пользовательский класс.
Код: Выделить всё
import java.util.PriorityQueue;
//patient class:
class Patient implements Comparable
> {
private int diseasePatient;
private T name;
private int age;
public Patient(T name, int diseasePatient, int age){
this.name = name;
this.age = age;
this.diseasePatient = calculatePriority(diseasePatient, age);
}
public int getPriority() {
return diseasePatient;
}
public int calculatePriority(int diseasePatient, int age){
if(age < 10 || age > 65 && diseasePatient != 1){
return diseasePatient - 1;
}
return diseasePatient;
}
public T getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString(){
return name + "(diseasePatient: " + diseasePatient + "/ age: " + age + ")";
}
@Override
public int compareTo(Patient other) {
return Integer.compare(this.diseasePatient, other.diseasePatient);
}
}
//priority queue class
class PriorityQueueManipulation{
private PriorityQueue queue;
public PriorityQueueManipulation(){
queue = new PriorityQueue();
}
public void append(T name, int diseasePatient , int age){
Patient element = new Patient(name, diseasePatient, age);
queue.add(element);
}
public T extract(){
if(!isEmpty()){
return queue.poll().getName();
}
return null;
}
public boolean isEmpty(){
return queue.isEmpty();
}
public String printPriorityQueue(){
StringBuilder sB = new StringBuilder();
for (Patient element : queue) {
sB.append(element).append("\n");
}
return sB.toString();
}
}
//public class
public class gestionPacientesUrgen {
public static void main(String[] args) {
PriorityQueueManipulation queue = new PriorityQueueManipulation();
queue.append("Person1", 2, 90);
queue.append("Person2", 2, 25);
queue.append("Person3", 3, 67);
queue.append("Person4", 1, 9);
System.out.println(queue.printPriorityQueue());
}
}
//
NoSuchMethodError: не найден подходящий метод для добавления (Пациент).
PriorityQueueManipulation может быть избыточным, в этой короткой программе в Java есть класс для этого.
Подробнее здесь: https://stackoverflow.com/questions/793 ... od-in-java
Мобильная версия