父子类java出现不兼容错误 [英] Getting incompatible error for parent child classes java
问题描述
我定义了一个HashMap:
I have a HashMap defined:
HashMap<String,ArrayList<Thing>> hashmap = new HashMap<>();
我确实需要一个ArrayList,因为我希望一个键具有多个值.这是我针对该问题找到的最佳解决方案.Thing类是其他许多类的父类.问题是当我尝试添加时出现错误.
I do need an ArrayList since I want one key to have multiple values. This was the best solution I found for that problem. The Thing class is a parent class for a bunch of other classes. Problem is when I try to add I get an error.
hashmap.put(b, world.ports);
这是错误:
no suitable method found for put(String,ArrayList<SeaPort>)
method Map.put(String,ArrayList<Thing>) is not applicable
(argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
method AbstractMap.put(String,ArrayList<Thing>) is not applicable
(argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
method HashMap.put(String,ArrayList<Thing>) is not applicable
(argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
`我不明白这是因为SeaPort扩展了Thing,它是否应该兼容?我读过很多上下游的线程,但是在这里看不到它们如何应用.这是Thing类:
` I dont understand this since SeaPort extends Thing isn't it supposed to be compatible? I have read a bunch of upcasting and downcasting threads but I don't see how they apply here. Here is the Thing class:
package seaportprogram;
import java.util.Scanner;
public class Thing implements Comparable<Thing>{
String name;
int index;
int parent;
World world;
public Thing(){
name = null;
index = 0;
parent =0;
}
//Thing Scanner Constructor
public Thing (Scanner sc){
if(sc != null){
name = sc.next();
index = sc.nextInt();
parent = sc.nextInt();
}
}
// Get Index
public int getIndex(){
return index;
}
//Get Name
public String getName(){
return name;
}
//Get Parent
public int getParent(){
return parent;
}
//Thing To String
public String toString(){
return name + " " + index;
}
//Auto-Gen Compare Method
@Override
public int compareTo(Thing o) {
// TODO Auto-generated method stub
return 0;
}
}//End - Class Thing
这是SeaPort类:
Here is the SeaPort class:
package seaportprogram;
import java.util.ArrayList;
import java.util.Scanner;
class SeaPort extends Thing {
ArrayList<Dock> docks;
ArrayList<Ship> que;
ArrayList<Ship> ships;
ArrayList<Person> persons;
//Sea Port Scanner Constructor
public SeaPort(Scanner sc){
super(sc);
docks = new ArrayList<>();
que = new ArrayList<>();
ships = new ArrayList<>();
persons = new ArrayList<>();
}
//Set Docks
public void setDocks(ArrayList<Dock> docks){
this.docks = docks;
}
//Get Docks
public ArrayList<Dock> getDocks(){
return docks;
}
//Set Ships
public void setShips(ArrayList<Ship> ships){
this.ships = ships;
}
//Get Ships
public ArrayList<Ship> getShips(){
return ships;
}
//Set Que
public void setQue(ArrayList<Ship> que){
this.que = que;
}
//Get Que
public ArrayList<Ship> getQue(){
return que;
}
//Sea Port To String
public String toString(){
String string = "\n\n Sea Port: " + super.toString() + "\n";
for(Dock md: docks){
string += "\n" + md + "\n";
}
string += "\n\n --- List of all ships in Que: ";
for(Ship ms: que){
string += "\n > " + ms;
}
string += "\n\n --- List of all Ships:";
for(Ship ms: ships){
string += "\n > " + ms;
}
string += "\n\n --- List of all Persons:";
for(Person mp: persons){
string += "\n > " + mp;
}
return string;
}//End
}// End Sea Port Class
这是世界一流的课程:
package seaportprogram;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class World extends Thing{
ArrayList<SeaPort> ports;
PortTime time;
//World Scanner Constructor
public World(Scanner sc){
super (sc);
ports = new ArrayList<>();
}
//Set Ports
}
有什么办法可以做到这一点?TIA!更新:我正在尝试打印地图,但出现了空指针异常.我认为这是因为该列表在海港和MyHashMap类中都已初始化.我似乎找不到解决此问题的方法,我甚至不确定这是错误的原因.这是toString():
Any way to make this happen? TIA! update: I am trying to print the map and I get an null pointer exception. I think this is because the list is being initialized both in seaport and in the MyHashMap class. I cant seem to find a way to get around this and I'm not even sure this is the reason for the error. Here is the toString():
public String toString(){
if(map.isEmpty())
{
System.out.println("The hashMap is empty");
return "empty";
}
String display=" ";
Iterator<String> itr = map.keySet().iterator();
while (itr.hasNext()) {
display =display + itr.next();
Iterator<ArrayList<T>> itr2 = map.values().iterator();
while (itr2.hasNext()) {
display +=itr2.next();
}
}
return display;
}
这是gui对它的调用:
and this is the call to it from the gui:
jta.setText(map.toString());
推荐答案
ArrayList< Derived_Class>
不扩展 ArrayList< Base_Class>
.
如果要使用这样的HashMap,一种解决方案可能是使用模板编写自己的包装器类:
If you want to use an HashMap like this, one solution could be to write your own wrapper class using templates:
import java.util.ArrayList;
import java.util.HashMap;
public class MyHashMap<T extends Thing> {
private HashMap<String, ArrayList<T>> map;
public MyHashMap() {
map = new HashMap<>();
}
public void add(String s, T element) {
ArrayList<T> list = null;
if ((list = map.get(s)) == null)
map.put(s, list = new ArrayList<T>());
list.add(element);
}
public ArrayList<T> get(String s) {
return map.get(s);
}
}
如果要使用SeaPorts的HashMap,则可以像这样创建新的HashMap,
if you want an HashMap of SeaPorts, you can create a new HashMap like this,
MyHashMap<SeaPort> map = new MyHashMap<SeaPort>();
并使用自定义的setter和getter来访问数组列表
and use custom setters and getters to access the arraylists
这篇关于父子类java出现不兼容错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!