Java 抛出错误"<Class Name> is not abstract and does not override abstract method in the <Interf



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

抛出错误(4):1.加法不是抽象的,并且不会覆盖cali 中的抽象方法Div()

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

  2. Division不是抽象的,并且不会覆盖cali 中的抽象方法Div()

  3. 乘法不是抽象的,并且不会覆盖cali 中的抽象方法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 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);
    }
}

类计算器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和面向对象设计中最强大的方面之一。

如果您创建了一个名为"cali"的接口,那么您就是在创建一个契约,任何实现该接口的类都将实现您在cali中定义的内容。因此,您可能需要重新思考目前是如何设计的。

要描述它的工作原理,请将接口更改为只有一个名为"perform"的方法。现在,在实现类加法、减法、乘法和除法中,创建一个也称为"performe"的方法。除了"perform",你几乎可以把它称为任何其他的东西,但要让它成为一个你觉得cali实现的所有类或其他扩展cali的接口都应该保证的特性。

然后就可以做到:

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

等等。

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

相关内容

最新更新