简体   繁体   English

在抽象类Java中的类中声明接口并实现

[英]Declaring an Interface and implementing in classes in an abstract class Java

I am doing an exercise, the book is not helping me grasp the concept, neither are the online resources. 我正在练习,这本书没有帮助我理解概念,在线资源也没有。 This may seem really silly but I don't know what I'm missing!!! 这看起来似乎很愚蠢,但我不知道我在想什么!!! I am quite new to Java and have had a look at other examples on stack but to no avail :s I need to declare 3 interfaces. 我对Java很陌生,并且看过堆栈上的其他示例,但无济于事:s我需要声明3个接口。 Each interface needs to declare a method with the same name as its interface. 每个接口都需要声明一个与其接口名称相同的方法。 Then the abstract class is extended by 3 classes which implement the aforementioned interfaces.Each class needs to be instantiated. 然后将抽象类扩展为3个实现上述接口的类,每个类都需要实例化。 If anyone could explain the procedure to this I would be eternally grateful. 如果有人能对此解释一下程序,我将永远感激不已。

interface antiLockBrakes{
    public void antiLockBrakes();
}

interface cruiseControl{
    public void cruiseControl();
}

interface powerSteering{
    public void powerSteering();
}

public abstract class Auto{
    abstract class Model1 extends Auto implements antiLockBrakes{
        public abstract void antiLockBrakes();
        Model1 mod1 = new Model1();
        mod1.antiLockBrakes();
    }

    public static void main(String[] args){
    }   
}

this is your question: someone to explain how exactly to declare and interface and then have it implemented in the abstract class right?? 这是您的问题: 有人解释如何准确地声明和接口,然后在抽象类中实现它吗?

Here's the answer for it. 这是答案。

See lets consider I have an interface 看到让我考虑我有一个界面

interface someInterface{
    public void someMethod();
}

Now to implement the someInterface in abstract class 现在在抽象类中实现someInterface

public abstract class SomeClass implements someInterface{
    public void someMethod(){
              System.out.println("Inside someMethod");
    }
    public abstract myMethod();
}

See in the class SomeClass we have implemented interface by giving definition to method someMethod() and since we want this SomeClass to be a abstract class we have defined one abstract method myMethod() for it. 在类SomeClass中,我们已经通过给方法someMethod()定义来实现了接口,并且由于我们希望该SomeClass是一个抽象类,因此我们为其定义了一个抽象方法myMethod()

Now any class which extends from SomeClass will also implement interface someInterface implicitly (because SomeClass has implemented it) and if it want its own definition for someMethod() it can override it. 现在,从SomeClass扩展的任何类也将隐式实现接口someInterface(因为SomeClass已经实现了它),如果它想要自己的someMethod()定义,则可以重写它。 But if a child class wants to be a concrete class ( a class in which all its method will have implementation) then it has to provide implementation for abstract method myMethod(). 但是,如果子类想成为一个具体的类(所有方法都将在其中实现的类),则它必须为抽象方法myMethod()提供实现。

HTH:) HTH :)

this is what I like to use to see the difference between abstract classes and interface classes 这就是我喜欢用来查看抽象类和接口类之间的区别的东西

interface class 接口类

//I say all motor vehicles should look like that :
interface MotorVehicle {


    void run();

    int getFuel();
}

// my team mate complies and write vehicle looking that way
class Car implements MotorVehicle {

    int fuel;

    public void run() {
        System.out.println("Wrroooooooom");
    }

    public int getFuel() {
        return this.fuel;
    }
}

abstract class 抽象类

// I say all motor vehicles should look like that :
abstract class MotorVehicle2 {

    int fuel;

    // they ALL have fuel, so why let others implement that ?
    // let's make it for everybody
    int getFuel() {
        return this.fuel;
    }

    // that can be very different, force them to provide their
    // implementation
    abstract void run();

}

// my team mate complies and write vehicle looking that way
class Car2 extends MotorVehicle2 {
    void run() {
        System.out.println("Wrroooooooom");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM