无法解析Java多态方法

无法解析Java多态方法,java,compiler-errors,polymorphism,abstract-class,subclass,Java,Compiler Errors,Polymorphism,Abstract Class,Subclass,我是一个编程新手,所以请容忍我。我搜索了一下,没有找到一个可以回答这个问题的现有线程。我编写了下面的代码,根据用户是否将安全对象标识为股票或债券,该代码应该可以写出stock.toString()或bond.toString()固定短语。然而,我得到了“安全性无法解决”的编译器错误。我认为这是一个问题,因为安全对象的类没有在编译时定义。这是真的吗?如果是这样的话,有没有办法不借助反射方法来解决这个问题?谢谢大家! public static void main(String[] args) {

我是一个编程新手,所以请容忍我。我搜索了一下,没有找到一个可以回答这个问题的现有线程。我编写了下面的代码,根据用户是否将安全对象标识为股票或债券,该代码应该可以写出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既不是“股票”也不是“债券”的情况下进行清理:


问题不在于类型

您在内部范围中定义了
security
,而它根本不存在于外部(在您执行
security.setPrice
的地方)。更糟糕的是,代码无法轻松修复,因为
security
securityType
既不是“债券”也不是“股票”时,根本不会定义
security
(无论是否是内部范围)

但是我想你想做的是:

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

}

当然,这是一个糟糕的解决方案,但你必须首先明确自己的任务。

在if-else之外声明它,以便在if-else之后可用

假设
股票
债券
的超类,如果不申报
证券
的话

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


    • 问题在变量范围内。
      安全性仅在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);
      

      然后可能出现NullReferenceException。OP首先需要制定他们想要做的事情。代码似乎并不好。PS:哎呀,我注意到问题出在Java上。乍一看我以为是C:)哦,如果
      Bond
      不是
      Stock
      的祖先,你的代码就会失败在这种情况下,编译器说类型对象的setEn收益(Double)是未定义的(或者如果我用
      code
      Security Security=null
      code
      初始化安全性-这是真的。该方法仅在Stock类中定义。调用method时将其转换为Stock。您的对象
      Bond
      Stock
      有何关联?
      Bond
      扩展
      Stock
      ?Bond和Stock是名为Security的抽象类Thank you penartur。我正试图从中学习。Bond和Stock是抽象类Security的子类,我希望程序根据证券类(Stock或Bond)运行相应版本的toString()(隐式在System.out.println(Security)调用中)在运行时,无需重写调用语句以及setPrice和setShares调用(这两种方法都是从Security继承的)。我有一种感觉,这在将来会很有用。@MattM如果
      securityType
      既不是债券也不是股票怎么办?是的,我意识到我需要加入一个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);