这种方式通过从方法的返回值复制实例化对象是否可行?

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

Is it acceptable to instantiate an object by copying from the return value from a method?

问题

我是你的中文翻译,以下是翻译好的内容:

我刚开始学习Java,最近在大学课程中学习了构造函数。我尝试编写了一段代码,用于使用参数实例化对象。脚本使用 Scanner 实例作为参数来获取输入。

问题是,如果我在方法内部创建一个实例化对象的方法,该对象将被视为方法的局部变量。如果我创建一个方法仅用于获取参数,并在 main 方法中使用这些参数实例化对象,代码会变得有些混乱。我的解决方案是创建一个方法,其中包括:1. 获取输入,2. 使用构造函数实例化对象,3. 返回刚创建的对象。我能够得到想要的结果,但我不确定这样做是否可行。

以下是我的代码:

public class Game {
    private int gameID;
    private String gameTitle;
    private String genre;

    // 构造函数
    public Game(int ID, String title, String genre) {
        gameID = ID;
        gameTitle = title;
        this.genre = genre;
    }
}
import java.util.Scanner;

public class GameTest {

    static Scanner kb = new Scanner(System.in);

    public static void main(String[] args) {
        Game myGame = constructGame();
    }


    static Game constructGame() {

        int ID = kb.nextInt();
        kb.nextLine();
        String title = kb.nextLine();
        String genre = kb.nextLine();

        Game newGame = new Game(ID, title, genre);

        return newGame;
    }
}

这个解决方案会有问题吗?老实说,我对这里涉及的内存引用问题有点担心。

英文:

I'm new at Java and I recently learnt about constructors in my uni class. I tried write a code to instantiate an object with arguments. The script get the inputs as arguments with Scanner instance.

The problem is, if I create a method to instantiate an object inside of it, the object would be treated as a local variable of the method. If I create a method to get arguments only and instantiate the object in the main method with them, it becomes somewhat messy. My solution is to create a method which is 1. getting inputs, 2. instantiating an object with constructor, and 3. returning the object just created. I could got a result what I've wanted but I am not sure this is acceptable or not.

Here is my code:

public class Game {
    private int gameID;
    private String gameTitle;
    private String genre;

    // constructor
    public Game(int ID, String title, String genre) {
        gameID = ID;
        gameTitle = title;
        this.genre = genre;
    }
}
import java.util.Scanner;

public class GameTest {

    static Scanner kb = new Scanner(System.in);

    public static void main(String[] args) {
        Game myGame = constructGame();
    }


    static Game constructGame() {

        int ID = kb.nextInt();
        kb.nextLine();
        String title = kb.nextLine();
        String genre = kb.nextLine();

        Game newGame = new Game(ID, title, genre);

        return newGame;
    }
}

Can this solution be a problem? Honestly, I'm bit worried about memory references things here.

答案1

得分: 2

这是完全可以接受的。也许如果constructEmployee将其使用的Scanner作为参数传入,代码可能会更加清晰;然后main方法会将扫描仪传递给它,但是目前的代码没有什么明显的问题。

英文:

This is completely acceptable. It would perhaps be cleaner if constructEmployee took the Scanner it uses as a parameter, and then the main method would pass the scanner to it, but there's nothing wrong per-se with your current code.

huangapple
  • 本文由 发表于 2020年8月22日 19:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63535473.html
匿名

发表评论

匿名网友

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

确定