使用用户输入创建新对象 [JAVA]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32072534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using user input to create a new object [JAVA]
提问by Gabriel Chan
Hi I am trying to create a program to create a new object whenever the user inputs a new information for a certain object. Currently I have this.
嗨,我正在尝试创建一个程序,以便在用户为某个对象输入新信息时创建一个新对象。目前我有这个。
import java.util.Scanner;
public class Main
{
public static void main (String args[])
{
String input;
Scanner scanner = new Scanner(System.in);
do
{
System.out.println("Computer Menu");
System.out.println("1. Add a new Desktop Information");
System.out.println("2. Add a new Laptop Information");
System.out.println("3. Display all Computer Information");
System.out.println("4. Quit");
System.out.print("Please enter either 1 to 4: ");
input =(scanner.nextLine());
if (input.equals("1"))
{
Desktop desktop1 = new Desktop();
System.out.println("Please enter Desktop ID: ");
desktop1.setID (scanner.nextLine());
System.out.println("Please enter Desktop Processor Speed: ");
desktop1.setProcess (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Desktop RAM: ");
desktop1.setRam (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Desktop Harddisk Space: ");
desktop1.setDisk (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Desktop Monitor Type: ");
desktop1.setMonitor (scanner.nextLine());
System.out.println("Please enter Desktop Price: ");
desktop1.setPrice (Double.parseDouble(scanner.nextLine()));
desktop1.displayComputer();
}
else if (input.equals("2"))
{
Laptop laptop1 = new Laptop();
System.out.println("Please enter Laptop ID: ");
laptop1.setID (scanner.nextLine());
System.out.println("Please enter Laptop Processor Speed: ");
laptop1.setProcess (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Laptop RAM: ");
laptop1.setRam (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Laptop Harddisk Space: ");
laptop1.setDisk (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Laptop Weight: ");
laptop1.setWeight (Double.parseDouble(scanner.nextLine()));
System.out.println("Please enter Laptop Price: ");
laptop1.setPrice (Double.parseDouble(scanner.nextLine()));
laptop1.displayComputer();
}
Computer super class
电脑超级班
public class Computer
{
String ID;
double process;
double ram;
double disk;
double price;
void setID (String _id)
{
ID = _id;
}
void setProcess (double _Process)
{
process = _Process;
}
void setRam (double _Ram)
{
ram = _Ram;
}
void setDisk (double _Disk)
{
disk = _Disk;
}
void setPrice (double _Price)
{
price = _Price;
}
String getID()
{
return ID;
}
double getProcess()
{
return process;
}
double getRam()
{
return ram;
}
double getDisk()
{
return disk;
}
double getPrice()
{
return price;
}
void displayComputer()
{
System.out.println("Computer ID: " + getID());
System.out.println("Processor Speed: " + getProcess());
System.out.println("RAM: " + getRam());
System.out.println("Harddisk: " + getDisk());
System.out.println("Price: " + getPrice());
}
}
Desktop sub class
桌面子类
public class Desktop extends Computer
{
String monitor;
void setMonitor (String _Monitor)
{
monitor = _Monitor;
}
String getMonitor()
{
return monitor;
}
void displayComputer()
{
System.out.println("Computer ID: " + getID());
System.out.println("Processor Speed: " + getProcess());
System.out.println("RAM: " + getRam());
System.out.println("Harddisk: " + getDisk());
System.out.println("Monitor Type: " + getMonitor());
System.out.println("Price: " + getPrice());
System.out.println("");
}
}
Laptop sub class
笔记本子类
public class Desktop extends Computer
{
double Weight;
void setWeight(String _Weight)
{
Weight= _Weight;
}
String getWeight()
{
return Weight;
}
void displayComputer()
{
System.out.println("Computer ID: " + getID());
System.out.println("Processor Speed: " + getProcess());
System.out.println("RAM: " + getRam());
System.out.println("Harddisk: " + getDisk());
System.out.println("Weight: " + getWeight());
System.out.println("Price: " + getPrice());
System.out.println("");
}
}
So There is one class and 2 sub classes but I don't think it matters. So if i entered 1 it should create a new object for Desktop and if i entered 2 it would create a new object for Laptop. And when I enter 3 it should show all the objects created. I don't know how to get any of them to work please help.
所以有一个班级和 2 个子班级,但我认为这并不重要。因此,如果我输入 1,它应该为桌面创建一个新对象,如果我输入 2,它将为笔记本电脑创建一个新对象。当我输入 3 时,它应该显示所有创建的对象。我不知道如何让他们中的任何一个工作,请帮忙。
采纳答案by Ankush soni
With the limit information provided by you I try to solve the issue:
根据您提供的限制信息,我尝试解决问题:
- I am not adding any attributes to Desktop and Laptop class, overriding toString Method in both the Classes i.e.
- 我没有向 Desktop 和 Laptop 类添加任何属性,而是在两个类中都覆盖了 toString 方法,即
public class Laptop {
public String toString() {
return "Laptop [getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
}
public class Desktop {
public String toString() {
return "Desktop [getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
}
Your Main method look like:
您的 Main 方法如下所示:
public static void main (String args[])
{
String input;
Scanner scanner = new Scanner(System.in);
List<Object> listOfObject = new ArrayList<>();
do
{
System.out.println("Computer Menu");
System.out.println("1. Add a new Desktop Information");
System.out.println("2. Add a new Laptop Information");
System.out.println("3. Display all Computer Information");
System.out.println("4. Quit");
System.out.print("Please enter either 1 to 4: ");
input =(scanner.nextLine());
if (input.equals("1")){
Desktop desktop1 = new Desktop();
listOfObject.add(desktop1);
}else if (input.equals("2")){
Laptop laptop1 = new Laptop();
listOfObject.add(laptop1);
}else if(input.equals("3")){
for(Object obj : listOfObject){
if(obj instanceof Desktop){
Desktop d1 = (Desktop)obj;
System.out.println(d1.toString());
}else if(obj instanceof Laptop){
Laptop l1 = (Laptop)obj;
System.out.println(l1.toString());
}
}
}
}while(!input.equals("4"));
}
回答by Adam
Assuming you have a class/interface named Computer from which Laptop and Desktop extend.
假设您有一个名为 Computer 的类/接口,Laptop 和 Desktop 从中扩展。
1) Add a list to store Computer instances.
1) 添加一个列表来存储计算机实例。
List<Computer> computers = new ArrayList<Computer>()
2) Make actions "1" and "2" add to that list, e.g.
2)将操作“1”和“2”添加到该列表中,例如
computers.add(desktop1);
3) Make action "3" print out the list. This assumes you've implemented toString()
3) 使动作“3”打印出列表。这假设您已经实现了 toString()
for (Computer computer : computers) {
System.out.println(computer);
}