如何在每次程序循环时创建一个新对象?

huangapple go评论65阅读模式
英文:

How can I create a new object every time my program loops?

问题

如何设置我的程序是我的代码运行并允许用户创建一个新的电影对象然后将其存储到构造函数中然后它会提供选项来创建一个新的电影然后将其存储在同一个电影对象中这最终会覆盖先前创建的电影对象许多实现方法会在循环中进行对象创建但这会要求将所有多个值都一次性存储到多个对象中我不打算这样做我不确定如何解决这个问题
以下是我的代码
电影类

public class Movie {
    private int id;
    private String name;
    private String description;
    private String[] genre;
    private String[] actors;
    private String[] language;
    private String countryOfOrigin;
    private Map<String, Integer> ratings;

    Movie() {
    }

    // 构造函数
    public Movie(int id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.genre = genre;
        this.actors = actors;
        this.language = language;
        this.countryOfOrigin = countryOfOrigin;
        this.ratings = ratings;
    }

    // 设置器
    public void setid(int id) {
        this.id = id;
    }

    public void setname(String name) {
        this.name = name;
    }

    // 获取器
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "\n电影编号:" + id + "\n电影名称:" + name + "\n电影描述:" + description + "\n电影类型:" + Arrays.toString(genre) + "\n电影演员:" + Arrays.toString(actors) + "\n电影语言:" + Arrays.toString(language) + "\n原产国:" + countryOfOrigin + "\n评分:" + ratings + "";
    }
}

主类

public class Main {
    // --- 私有全局扫描器初始化 --- //
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        while (loopAgain) {
            Random rand = new Random();
            int id = rand.nextInt(MAX);

            System.out.println("\n创建投票");

            System.out.println("\n输入电影名称:");
            String name = input.nextLine();
            Movie movie1 = new Movie(id, name, description, genre, actors, language, countryOfOrigin, mapRatings);

            System.out.println("创建新电影?(是/否):");
            String answer = input.nextLine();

            if (answer.equals("是") || answer.equals("Y")) {
                loopAgain = true;
            }
        }
    }
}
英文:

How my program is set up is that my code runs and allows the user to create a new movie object which is then stored to the constructor. It then gives the option to create a new movie which is then stored in the same movie object, which ultimately overwrites the previous movie object that was created. Many implementation go about putting the object creation in a loop, but this asks for all the multiple values to be stored into multiple objects all at once, I'm not looking to do that. I'm not sure how to go about solving this.
Here's my code
Movie class

public class Movie {
private int id;
private String name;
private String description;
private String[] genre;
private String[] actors;
private String[] language;
private String countryOfOrigin;
private Map&lt;String, Integer&gt; ratings;
Movie() {
}
//Constructor
public Movie(int id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map&lt;String, Integer&gt; ratings) {
this.id = id;
this.name = name;
this.description = description;
this.genre = genre;
this.actors = actors;
this.language = language;
this.countryOfOrigin = countryOfOrigin;
this.ratings = ratings;
}
//setters
public void setid(int id) {
this.id = id;
}
public void setname(String name) {
this.name = name;
}
//getters
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return &quot;\nMovie Id: &quot; + id + &quot; \nMovie Name: &quot; + name + &quot;\nMovie Description: &quot; + description + &quot;\nMovie Genre(s): &quot; + Arrays.toString(genre) + &quot;\nMovie Actor(s): &quot; + Arrays.toString(actors) + &quot;\nMovie Language(s): &quot; + Arrays.toString(language) + &quot;\nCountry of Origin: &quot; + countryOfOrigin + &quot;\nRatings: &quot; + ratings +&quot;&quot;;
}
}

Main class

public class Main {
// --- Private global scanner initialization --- //
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws Exception {
while (loopAgain) {
Random rand = new Random();
int id = rand.nextInt(MAX);
System.out.println(&quot;\nCreate Poll&quot;);
System.out.println(&quot;\nEnter the Movie Name: &quot;);
String name = input.nextLine();
Movie movie1 = new Movie(id, name, description, genre, actors, language, countryOfOrigin, mapRatings);
System.out.println(&quot;Create new movie? (y/n): &quot;);
String answer = input.nextLine();
if (answer.equals(&quot;y&quot;) || answer.equals(&quot;Y&quot;)) {
loopAgain = true;
}
}
}
}

答案1

得分: 2

你可以在创建电影对象后,将它们存储在某种容器中。例如,你可以使用 ArrayList:

ArrayList<Movie> movies = new ArrayList<Movie>();

你可以将这段代码放在 while 循环之前。然后在创建对象后,你可以将对象添加到容器中,比如 movies.add(movie1),你可以将这行代码放在你调用构造函数的地方。稍后要访问对象,你可以使用 movies.get(index)

英文:

You can store the movie objects in some kind of container after you create them. So you can use an ArrayList for example

ArrayList&lt;Movie&gt; movies= new ArrayList&lt;Movie&gt;();.

You can put this above your while loop. Then after you create the object you can add it into the container such as movies.add(movie1) you can put this right under where you called the constructor. Later on to access the object, you can use movies.get(index)

huangapple
  • 本文由 发表于 2020年3月16日 08:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/60699155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定