无法解析Java多态方法

问题描述 投票:0回答:3

我是编程新手,所以请多多包涵。我进行了搜索,但找不到可以回答此问题的现有线程。我编写了以下代码,该代码根据用户将安全对象识别为股票还是债券来吐出stock.toString()或bond.toString()固定短语。但是,我收到“无法解决的安全性”编译器错误。我认为这是一个问题,因为安全对象的类未在编译时定义。真的吗?如果是这样,有什么办法可以不依靠反射方法呢?谢谢!

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和其他人。这是我们想出的解决方案,但请告诉我是否有更好的选择。并且我添加了else语句来清理,以防万一securityType既不是“ stock”也不是“ bond”:)

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

}
java compiler-errors polymorphism abstract-class subclass
3个回答
6
投票

将其声明为其他,以便在其他情况下可用

假设StockBond的超类,如果没有像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);
    }

See


4
投票

问题不在于类型。

您在内部作用域中定义了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);
    }

}

当然,这是一个糟糕的解决方案,但是您必须首先对任务进行澄清。


1
投票

问题出在您的变量范围内。 security仅在if / else块内定义。

您可以将代码更改为如下所示:

Object security; //you can use common superclass of Bond and Stock (maybe Security?)

if (securityType.compareToIgnoreCase("stock") == 0) {
    /* ... */
    Stock stock = new Stock();
    stock.setEarnings(theEarnings);
    security = stock;
}

else if (securityType.compareToIgnoreCase("bond") == 0) {
    /* ... */
    Bond bond = new Bond();
    bond.setRate(theRate);
    security = bond;
}

/* ... */
System.out.println(security);
© www.soinside.com 2019 - 2024. All rights reserved.