Java抛出错误“不是抽象的,并且不会覆盖中的抽象方法”

我无法编译下面编写的代码,Java总是抛出一个错误。 你能帮帮我吗(PS:我是Java新手,还处于学习阶段)。 如果在任何地方,我写错了代码,请帮我纠正它,这样我就能很好地学习它。

Error Thrown(4):1。加法不是抽象的,不会覆盖calci中的抽象方法Div()

  1. 减法不是抽象的,并且不会覆盖calci中的抽象方法Div()

  2. 除法不是抽象的,也不会覆盖calci中的抽象方法Div()

  3. 乘法不是抽象的,并且不会覆盖calci中的抽象方法Div()

    import java.util.Scanner; interface calci { void add(); void sub(); void mul(); void div(); } 

加类

 class addition implements calci { public void add() { System.out.println("Enter two numbers:"); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int n1= scn.nextInt(); int result = n+n1; System.out.println("The Result of the two numbers is:"+result); } } 

类减法

 class subtraction implements calci { public void sub()`enter code here` { System.out.println("Enter two numbers:"); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int n1= scn.nextInt(); int result = n-n1; System.out.println("The Result of the two numbers is:"+result); } } 

类乘法

 class multiplication implements calci { public void mul() { System.out.println("Enter two numbers:"); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int n1= scn.nextInt(); int result = n*n1; System.out.println("The Result of the two numbers is:"+result); } } 

class级划分

 class division implements calci { public void div() { System.out.println("Enter two numbers:"); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int n1= scn.nextInt(); int result = n/n1; System.out.println("The Result of the two numbers is:"+result); } } 

class级计算器1

 class calculator1 { public static void main(String[] args) { int i =0; addition a1 = new addition(); subtraction s1 = new subtraction(); multiplication m1 = new multiplication(); division d1 = new division(); System.out.println("Enter Your Name:"); Scanner scn = new Scanner(System.in); String a = scn.next(); System.out.println("Good Morning!"+a); System.out.println(); System.out.println("Please choose option from below"); System.out.println(); System.out.println("1.Addition 2.Subtraction 3.Multiplication 4.Division"); int option = scn.nextInt(); while (i!=0) { if (option==1) { a1.add(); } else if (option == 2) { s1.sub(); } else if (option == 3) { m1.mul(); } else if (option == 4) { d1.div(); } else { System.out.println("Please enter valid number"); } } System.out.println("Do you wish to continue"); int b = scn.nextInt(); if (b==0) { System.out.println("Thanks for using the calculator Program"+a); System.out.println("Have a great day!"); } } } 

实现接口时,需要在实现类中实现该接口的所有方法。

在您的示例中,您应该在实现接口calci任何类中实现add()sub()mul()div()

正如错误所说 – 实现calci接口的某些类不实现该接口的所有方法。 当您创建实现接口的类时,您需要实现该接口的所有方法,而不仅仅是您感兴趣的某些方法。这就是接口的全部要点。 否则你将无法对接口进行编码,例如calci test = getCalciInstance().XXX()其中XXX()是在calci声明的方法可能无法实现,具体取决于实现的实例getCalciInstance()将返回!

所以你的所有类都需要实现以下方法:

 void add(); void sub(); void mul(); void div(); 

您可能希望在界面上阅读一些内容。 它们是Java和面向对象设计的最强大的方面之一。

如果您创建一个名为“calci”的接口,那么您正在创建一个契约,任何实现该接口的类都将实现您在calci中定义的内容。 因此,您可能想重新考虑当前的设计方式。

要描述它的工作原理,请将接口更改为只有一个名为“perform”的方法。 现在,在实现类的加法,减法,乘法和除法中,创建一个也称为“执行”的方法。 除了“执行”之外,您几乎可以将其称为“执行”,但要使其成为您认为calci实现的所有类或扩展calci的其他接口应该保证的特性。

然后就可以做到:

 calci addition = new addition(); addition.perform(); calci multiplication = new multiplication(); multiplication.perform(); 

等等

您可以在实现方法上使用@Override注释,以确保在IDE中捕获拼写错误(如Eclipse)。