Java多态方法无法解决

Mat*_*t M 0 java polymorphism abstract-class compiler-errors subclass

我是一个编程新手,所以请耐心等待我.我搜索并找不到可以回答这个问题的现有主题.我写了下面的代码,它应该根据用户是将安全对象识别为股票还是债券来吐出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);
}
Run Code Online (Sandbox Code Playgroud)

感谢@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); 

}
Run Code Online (Sandbox Code Playgroud)

Jig*_*shi 6

将其声明为if else,以便在if之后可用

假设Stock是超一流的Bond,如果不申报security喜欢

 Object security = null;
Run Code Online (Sandbox Code Playgroud)

做了

 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);
    }
Run Code Online (Sandbox Code Playgroud)

看到