gpt4 book ai didi

java - 使用用户输入创建新对象[JAVA]

转载 作者:行者123 更新时间:2023-11-30 08:48:13 25 4
gpt4 key购买 nike

您好,我正在尝试创建一个程序,以便在用户为某个对象输入新信息时创建一个新对象。目前我有这个。

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();
}

计算机父类(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());
}
}

桌面子类

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("");
}
}

笔记本子类

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("");
}
}

所以有一个类和 2 个子类,但我认为这不重要。因此,如果我输入 1,它应该为 Desktop 创建一个新对象,如果我输入 2,它将为 Laptop 创建一个新对象。当我输入 3 时,它应该会显示所有创建的对象。我不知道如何让他们中的任何一个工作,请帮忙。

最佳答案

根据您提供的限制信息,我尝试解决问题:

  1. 我没有向 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() + "]";
}
}

您的 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"));
}

关于java - 使用用户输入创建新对象[JAVA],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32072534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com