Java多态方法无法解析
我是一个编程新手,请耐心等待。我搜索并找不到可以回答此问题的现有主题。我写了以下代码,根据安全对象是否被用户标识为股票或债券,应该吐出stock.toString()或bond.toString()罐头短语。但是,我收到“安全无法解决”编译器错误。我想这是一个问题,因为安全对象的类没有在编译时定义。真的吗?如果是这样,是否有任何解决方法,而不诉诸反思方法?谢谢!Java多态方法无法解析
public static void main(String[] args) {
double thePrice;
double theShares;
double theEarnings;
double theRate;
String securityType;
Scanner in = new Scanner(System.in);
System.out.println("Is it a stock or a bond?");
securityType = in.nextLine();
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully set to STOCK");
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
Stock security = new Stock();
security.setEarnings(theEarnings);
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully set to BOND");
System.out.println("What is the rate?");
theRate = in.nextDouble();
Bond security = new Bond();
security.setRate(theRate);
}
System.out.println("What is the price");
thePrice = in.nextDouble();
System.out.println("How many shares are there?");
theShares = in.nextDouble();
security.setPrice(thePrice);
security.setShares(theShares);
System.out.println(security);
}
感谢@Jigur Joshi,@penartur和其他人。这是我们提出的解决方案,但让我知道是否有更好的选择。如果securityType既不是“股票”,也不是“债券”,我要添加一个else语句来清理:)
public static void main(String[] args) {
...
Security security = null;
String securityType;
Scanner in = new Scanner(System.in);
System.out.println("Is it a stock or a bond?");
securityType = in.nextLine();
System.out.println("What is the price");
thePrice = in.nextDouble();
System.out.println("How many shares are there?");
theShares = in.nextDouble();
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully registered STOCK");
security = new Stock();
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
((Stock) security).setEarnings(theEarnings);
}
if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully registered BOND");
security = new Bond();
System.out.println("What is the rate?");
theRate = in.nextDouble();
((Bond) security).setRate(theRate);
}
security.setPrice(thePrice);
security.setShares(theShares);
System.out.println(security);
}
问题不在于类型。
你必须在内部范围内定义security
,它只是在那里你做security.setPrice
不存在外(。更糟的是,代码不能很容易地固定,如security
不会在所有的定义(无论是内部范围或没有)时securityType
既不是“羁绊”,也不是“股票”
不过我猜那是你想做的事是这样的:
public static void main(String[] args) {
double thePrice;
double theShares;
double theEarnings;
double theRate;
String securityType;
Scanner in = new Scanner(System.in);
System.out.println("Is it a stock or a bond?");
securityType = in.nextLine();
System.out.println("What is the price");
thePrice = in.nextDouble();
System.out.println("How many shares are there?");
theShares = in.nextDouble();
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully set to STOCK");
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
Stock security = new Stock();
security.setEarnings(theEarnings);
security.setPrice(thePrice);
security.setShares(theShares);
System.out.println(security);
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully set to BOND");
System.out.println("What is the rate?");
theRate = in.nextDouble();
Bond security = new Bond();
security.setRate(theRate);
security.setPrice(thePrice);
security.setShares(theShares);
System.out.println(security);
}
}
当然,这是一个可怕的解决方案,但你必须先明确自己的任务。
谢谢你。我正试图从中吸取教训。 Bond和Stock是抽象类Security的子类,我希望程序运行相应版本的toString()(隐式在System.out中)。println(安全性)调用),而不必重写该调用语句和setPrice和setShares调用(这两种方法都是从安全性继承的),这些调用基于安全性级别(Stock或Bond)。我有一种将来会有用的感觉。 –
@MattM如果'securityType'既不是债券也不是股票? – penartur
是的,我意识到在这种情况下我需要放入一个else语句来扫描,但我更关心现在理解编译时错误。 –
声明出来的,如果其他人,使其可用后,也可以侧面的if else
假设Stock
是超一流的Bond
,如果不申报security
像
Object security = null;
让它
Stock security = null;
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully set to STOCK");
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
security = new Stock();
security.setEarnings(theEarnings);
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully set to BOND");
System.out.println("What is the rate?");
theRate = in.nextDouble();
security = new Bond();
security.setRate(theRate);
}
请参阅
的问题是在你的变量的作用域。
security
仅在if/else块内部定义。
您可以更改代码以这样的事:
3210
如何乌尔对象'Bond'和'Stock'有关?是'Bond'扩展'Stock'吗? – Kshitij
债券和股票是抽象类的子类,称为安全 –