java扩展或覆盖switch案例

问题描述 投票:1回答:1

我在java课程中,所以我试图对代码有点模糊,所以我可以学习而不是作弊。该任务是采取上周的计划和扩展功能。基本上,我写了一个程序,我在两个案例中使用switch(不确定这是最好的选择,但这就是我所做的),并且我想添加更多用户选项。所以它目前允许输入'w','x',但我想通过用类B扩展A类来添加'y'和'z'作为选项。

A类中有一个默认情况,基本上输出“只输入'w'和'x'。”问题是,即使是扩展它的新B类,它也可以阻止除“w”和“x”之外的任何东西。

我知道我需要B类来覆盖它,所以它允许w,x,y和z,然后输入除了这四个选项之外的任何东西的默认触发器。无论哪种方式,请帮忙!

下面是A类(我解决了我的问题的一些代码,但所有变量,用户输入和扫描仪工作。这是我遇到问题的情况):

import java.util.Scanner;
public class A {
public A()
{
  // define and implement variables
  // call scanner into existance to read inputs from the user
  // Ask for user input (abbreviated section) and store in variables

  oper = myManager.next(".").charAt(0);

  switch(oper)
    {
       // case w call to firstMethod method
       case 'w':
          DoSomething = firstMethod(num1,num2);
          System.out.println(" The result is "+FirstAns);
        break;
       // case w call to secondMethod method
       case 'x':
          DoSomethingElse = secondMethod(num1,num2);
          System.out.println(" The result is "+SecondAns);
        break;
            default:
            System.out.println(" Please Enter 'w' or 'x' only.");
      }
   /* note, this portion I got rid of some work, it's normally
      math related but modified here just to return characters for
      this post since I think it's irrelevant to my question (and I
      don't want to cheat) */

 static char firstMethod(char a)
 {
 return a;
 }
 static char secondMethod(char a)
 {
 return a;
 }
}
}

下面是B级,它扩展了A,我无法说服允许更多的情况。注意,在编译之后,我正在执行B,但它仍然只允许来自A的情况。

 import java.util.Scanner;
 public class B extends A {
 public B() 
 {
  // define and implement variables
  // call scanner into existance to read inputs from the user
  // Ask for user input (abbreviated section) and store in variables

  oper = myManager.next(".").charAt(0);

  switch(oper)
    {
       // case w call to firstMethod method
       case 'w':
          DoSomething = firstMethod(num1,num2);
          System.out.println(" The result is "+FirstAns);
        break;
       // case w call to secondMethod method
       case 'x':
          DoSomethingElse = secondMethod(num1,num2);
          System.out.println(" The result is "+SecondAns);
       break;
       case 'y':
          DoSomethingMore = thirdMethod(num1,num2);
          System.out.println(" The result is "+ThirdAns);
        break;
       // case w call to firstMethod method
       case 'z':
          DoSomethingLast = fourthMethod(num1,num2);
          System.out.println(" The result is "+FourthAns);
       break;
          default:
          System.out.println(" Please Enter 'w', 'x', 'y', or 'z' only.");
        }
      }
   // again, simplified this portion

 static char thirdMethod(char a)
 {
 return a;
 }
 static char fourthMethod(char a)
 {
 return a;
 }
 public static void main(String[] args) {
    B b = new B();
 }
}

然后我更新测试程序以导入类B(而不是导入A的旧程序,因为B应该扩展A)。但它仍然只显示来自A的案例。我知道这是关于程序如何加载案例的操作顺序,只是不确定如何修复它。

java switch-statement override extend
1个回答
1
投票

始终通过子类的默认构造函数首先调用超类的默认构造函数。

在您的示例中,在使用默认构造函数创建类B时,将调用类A构造函数。

解决方案是将您的逻辑移动到两个类中具有相同签名的方法中,并在超类的构造函数中调用该方法。

像这样的东西:

class A {
    public A() {
        logic();
    }

    private void logic() {
        // Your switch of A
    }
}

class B extends A {
    public B() {
        super();
    }

    private void logic() {
        // Your switch of B
    }
}

动态绑定是此解决方案背后的OO原则。

© www.soinside.com 2019 - 2024. All rights reserved.