我很难理解如何使用来自两个不同类的具有相同名称的多个方法(在这种情况下,是两个相同的方法)。驱动程序类(计算机)正在使用Model和Laptop类来显示来自用户输入的信息。在“模型”和“便携式计算机”中,有两种名称相同但参数不同的方法。我对在笔记本电脑中为setModel方法设置三个参考变量的设置感到困惑。我曾尝试为setModel方法处理模型对象,但未成功。
我正在从UML图编写。笔记本电脑和型号之间存在聚合关系,特别是型号HAS-A与笔记本电脑的关系(笔记本电脑上有一条菱形,通过一条线连接到型号)。
import java.util.*;
public class Computer
{
public static void main (String[] args)
{
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
String inputInfo;
String brandName, modelName;
double price, cpuSpeed;
int ramSize;
String line = new String();
// instantiate a Laptop object
Laptop laptop1 = new Laptop();
printMenu();
//Create a Scanner object to read user input
Scanner scan = new Scanner(System.in);
do // will ask for user input
{
System.out.print("What action would you like to perform?\n");
line = scan.nextLine();
if (line.length() == 1)
{
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
// matches one of the case statement
switch (input1)
{
case 'A': //Add the laptop
System.out.print("Please enter the laptop information:\n");
System.out.print("What is the laptop\'s brand?\n");
brandName = scan.nextLine();
laptop1.setBrand(brandName);
System.out.print("What is the laptop\'s model?\n");
modelName = scan.nextLine();
System.out.print("What is the laptop\'s CPU speed(in GHz)?\n");
cpuSpeed = Double.parseDouble(scan.nextLine());
System.out.print("What is the laptop\'s RAM size(in GB)?\n");
ramSize = Integer.parseInt(scan.nextLine());
laptop1.setModel(modelName, cpuSpeed, ramSize);
System.out.print("How much is the laptop\'s price?\n");
//price = scan.nextDouble();
price = Double.parseDouble(scan.nextLine());
laptop1.setPrice(price);
break;
case 'D': //Display Pet
System.out.print(laptop1);
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
}
else
{
System.out.print("Unknown action\n");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"A\t\tAdd a Laptop\n" +
"D\t\tDisplay the Laptop\n" +
"Q\t\tQuit\n" +
"?\t\tDisplay Help\n\n");
}
}
import java.text.NumberFormat;
import java.util.Locale;
public class Laptop {
// TODO Auto-generated method stub
private String BrandName;
private Model model;
private double price;
public String getBrand()
{
return BrandName;
}
public Model getModel()
{
return model;
}
public double getPrice()
{
return price;
}
public void setBrand(String newBrand)
{
BrandName = newBrand;
}
public void setModel(String newModel, double newCPU, int newRAM)
{
}
public void setPrice(double newPrice)
{
price = newPrice;
}
public String toString()
{
NumberFormat mf = NumberFormat.getCurrencyInstance(Locale.US);
return ("\nBrand:\t" + BrandName + model.toString() +"\n" + "Price:\t " + mf.format(price) + "\n");
}
}
import java.text.DecimalFormat;
public class Model {
private String modelName;
private double cpuSpeed;
private int ramSize;
// TODO Auto-generated method stub
public Model()
{
this.modelName = "?";
this.cpuSpeed = 0.0;
this.ramSize = 0;
}
public String getModel()
{
return modelName;
}
public double getCPU()
{
return cpuSpeed;
}
public int getRAM()
{
return ramSize;
}
public void setModel(String newModel)
{
modelName = newModel;
}
public void setCPU(double newCPU)
{
cpuSpeed = newCPU;
}
public void setRAM(int newRam)
{
ramSize = newRam;
}
public String toString()
{
DecimalFormat df = new DecimalFormat("0.00");
return ("Model:\t" + modelName + "\n CPU:\t " + df.format(cpuSpeed) + "\n RAM:\t " + ramSize+"GB" + "\n" );
}
}
答案 0 :(得分:1)
如果方法是在不同的类中声明的,则甚至不需要其他参数,只需使用对象名称进行调用,然后自动调用响应方法ist。
答案 1 :(得分:1)
这就是让我感到困惑的地方,我不会以这种方式实现它,但是我必须这样做。
我通过最初的实现找到了解决方案
public void setModel(String newModel, double newCPU, int newRAM)
{
model.setModel(newModel);
model.setCPU(newCPU);
model.setRAM(newRAM);
}
然后我修复了Model对象的实例
private Model model = new Model();