【问题标题】:How do you call a method from another class which has a switch statement ready to call two methods in the same class?你如何从另一个类调用一个方法,它有一个 switch 语句准备好调用同一个类中的两个方法?
【发布时间】:2020-06-26 07:05:27
【问题描述】:

我不知道如何表达这个问题而不会让它听起来令人困惑,但它本质上是这样的:

TL;DR: 我一直试图从位于主类Shapes 中的inputProcess() 中的switch 语句调用位于子类Circle 中的circleChoice()。我不知道怎么做,但我在控制台上得到了java.lang.StackOverflowError。我可能搞砸了逻辑。如果不是需要使用子类的学校项目,我可以轻松解决此问题。

    package shapes;
import java.lang.Math.*;
import java.util.Scanner;

public class Shapes {
    //CONSTRUCTORS

        //choosing
static Scanner sc = new Scanner(System.in);
        //inputs
protected String radDiameter;   
protected int choice;
protected double a;
protected double b;   //b1
protected double d;   //b2
protected double c;
protected double h;
protected double radius;
        //outputs
protected double perimeter;
protected double area;
    //CONSTUCTORS   
    
public class Circle extends Shapes{  //error

    public void circleChoice() {
        System.out.println("Pick your poison: ");
        System.out.println("[P] Perimeter");
        System.out.println("[A] Area");
        radDiameter = sc.next();
        this.radDiameter.toUpperCase();
        switch (radDiameter) {
        case "P":
            System.out.println("Enter Radius for Perimeter: ");
            CirclePerimeter();
            break;
        case "A":
            System.out.println("Enter Radius for Area: ");
            CircleArea();
            break;
        }       
    }
    
    public double CirclePerimeter() {
        super.radius = sc.nextDouble();
        this.perimeter = 2 * Math.PI * super.radius;
        System.out.println(perimeter);
        return perimeter;
    }
    
    public double CircleArea() {
        super.radius = sc.nextInt();
        this.area = Math.PI * (super.radius*super.radius);
        System.out.println(this.area);
        return area;
    }
    
}   
public class Triangle extends Shapes{
    public void triangleChoice() {
        switch (choice) {
        case 1:
            System.out.println("Enter Radius for Perimeter: ");
            TrianglePerimeter();
            break;
        case 2:
            System.out.println("Enter Radius for Area: ");
            TriangleArea();
            break;
        }
        
    }

    public double TrianglePerimeter() {
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        
        perimeter = a*b*c;
        System.out.println(this.perimeter);
        return perimeter;
    }
    public double TriangleArea() {
        b = sc.nextInt();
        h = sc.nextInt();
        area = 12 * (b*h);
        System.out.println(this.area);
        return area;
    }
}


private Circle obj1 = new Circle();       //error
private Triangle obj2 = new Triangle();


    public void inputProcess() {

         Shapes s = new Shapes();
         
            System.out.println("Geometric Shapes");
            System.out.println("Choose Shape to continue:");
            System.out.println("[1] Circle");
            System.out.println("[2] Triangle");
            System.out.println("[0] Exit");
            System.out.println("Enter your choice: ");
            s.choice = sc.nextInt();
            
        switch (s.choice) {
            case 1 :
            obj1.circleChoice();
                break;
                
            
            case 2 :
            obj2.triangleChoice();  
                break;
                
            case 0:
                System.exit(s.choice);
                break;
            default :
                System.out.println("Invalid output! Please run again!");
                
                break;
            }

    }


    
    public static void main(String[] args) {    
        
        Shapes p = new Shapes();
        p.inputProcess();
    }

}

程序应该如何工作: 我有一个包含方法inputProcess 的类,它有一个switch 语句,该语句应该从类Circle 调用方法circleChoice(),或者从类Triangle 调用方法triangleChoice,具体取决于用户的输入。让我们关注Circle,让它更容易一些。

circleChoice() 有一个来自radDiameter 的扫描器输入和一个将调用circlePerimeter()circleArea() 的switch 语句。前两种方法的公式可以求解圆的周长或面积,这些公式由每种方法中的另一个扫描仪输入 (radius) 给出,效果很好。

之后,我在每个子类之外创建了对象private Circle obj1 = new Circle();,但仍在主类Shapes 内,以便在inputProcess() 中调用来自Circle 的方法。

inputProcess() 有一个 switch 语句,它应该调用 obj1.circleChoice();obj2.triangleChoice();,具体取决于 s.choice 的值。

问题: 当我尝试运行它时,它只在控制台上显示java.lang.StackOverflowError,突出显示第 24 行和第 93 行。这显然是一个逻辑错误,因为 Eclipse 没有显示任何警告突出显示。

通常情况下,我可以在不使用任何其他课程的情况下完成这项任务,但这是针对一个需要我使用它们的学校项目。

【问题讨论】:

  • Java 编码约定的类以大写字母(“Circle”)开头,而变量和方法以小写字母(“circlePerimeter”)开头。

标签: java methods switch-statement stack-overflow subclass


【解决方案1】:

答案很简单。因为 Circle 是 Shape 的子类,所以当您实例化 Shape,然后将 Circle 作为其属性进行实例化时,Circle 将 Circle 实例化为其属性(因为 Circle 是 Shape),然后 Circle 将 Circle 实例化为其属性等等......所以你有无限递归。

你真的应该重构你的代码,超类不应该聚合子类。而且它们也不应该是它们超类的内部类。那么它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2011-01-15
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多