在为子类创建对象时如何从 hashMap 实现属性?
How do I implement attributes from a hashMap when creating an object for a subclass?
我想做的基本上是迭代我的流派并使用我的 hashMap 作为查找 table 然后在创建对象时选择流派。我不知道接下来我需要做什么,因为当我已经有了一个新的构造函数时,它总是给我错误。
import java.util.HashMap;
import java.util.Scanner;
public class disc {
private String title;
private String releaseDate;
HashMap<Integer, String> genre = new HashMap<Integer, String>();
Scanner mainInput = new Scanner(System.in);
public disc(String releaseDate, String title, HashMap<Integer, String> genre) {
this.title = title;
this.releaseDate = releaseDate;
this.genre = genre;
this.genre.put(1, "Horror");
this.genre.put(2, "Action");
this.genre.put(3, "Hip-Hop");
this.genre.put(4, "Pop");
this.genre.put(5, "Jazz");
this.genre.put(6, "Thriller");
}
子类:
import java.util.HashMap;
public class game extends disc{
private int PEGIRating;
private String Platform;
public game(int PEGIRating, String Platform, String title, String releaseDate, HashMap<Integer, String> genre){
super(releaseDate, title, genre);
this.PEGIRating = PEGIRating;
this.Platform = Platform;
game g = new game(18,"PS5","Evil Within","Feb 2018",1);
}
}
您正在尝试在 game() 构造函数中初始化游戏对象...
public game(int PEGIRating, String Platform, String title, String releaseDate, HashMap<Integer, String> genre){
super(releaseDate, title, genre);
this.PEGIRating = PEGIRating;
this.Platform = Platform;
// >>> game g = new game(18,"PS5","Evil Within","Feb 2018",1); <<<<
}
如上所述,您正在游戏构造函数中初始化您的游戏对象,这将编译但可能导致 运行 时间问题。
除此之外,您将 int 数据类型传递到游戏构造函数的最后一个参数中,而不是传递 HashMap ,因为您在构造函数签名中有具体说明。创建一个采用精确参数列表但将 int 作为最后一个数据类型的构造函数,或者将 HashMap 作为最后一个参数传递。
我想做的基本上是迭代我的流派并使用我的 hashMap 作为查找 table 然后在创建对象时选择流派。我不知道接下来我需要做什么,因为当我已经有了一个新的构造函数时,它总是给我错误。
import java.util.HashMap;
import java.util.Scanner;
public class disc {
private String title;
private String releaseDate;
HashMap<Integer, String> genre = new HashMap<Integer, String>();
Scanner mainInput = new Scanner(System.in);
public disc(String releaseDate, String title, HashMap<Integer, String> genre) {
this.title = title;
this.releaseDate = releaseDate;
this.genre = genre;
this.genre.put(1, "Horror");
this.genre.put(2, "Action");
this.genre.put(3, "Hip-Hop");
this.genre.put(4, "Pop");
this.genre.put(5, "Jazz");
this.genre.put(6, "Thriller");
}
子类:
import java.util.HashMap;
public class game extends disc{
private int PEGIRating;
private String Platform;
public game(int PEGIRating, String Platform, String title, String releaseDate, HashMap<Integer, String> genre){
super(releaseDate, title, genre);
this.PEGIRating = PEGIRating;
this.Platform = Platform;
game g = new game(18,"PS5","Evil Within","Feb 2018",1);
}
}
您正在尝试在 game() 构造函数中初始化游戏对象...
public game(int PEGIRating, String Platform, String title, String releaseDate, HashMap<Integer, String> genre){
super(releaseDate, title, genre);
this.PEGIRating = PEGIRating;
this.Platform = Platform;
// >>> game g = new game(18,"PS5","Evil Within","Feb 2018",1); <<<<
}
如上所述,您正在游戏构造函数中初始化您的游戏对象,这将编译但可能导致 运行 时间问题。
除此之外,您将 int 数据类型传递到游戏构造函数的最后一个参数中,而不是传递 HashMap