简体   繁体   English

为什么java中需要非抽象的class?

[英]Why do you need non-abstract class in java?

I've written a Bike interface and implementing MountainBike class as below:我编写了一个 Bike 接口并实现了 MountainBike class,如下所示:

//interface

package com.company;

public interface Bike {

    void changeCadence(int newValue);
    void changeGear(int newValue);
    void speedUp(int increment);
    void applyBrakes(int decrement);

}
//implementing class 

package com.company;


public class MyBike implements Bike{

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    // The compiler will now require that methods
    // changeCadence, changeGear, speedUp, and applyBrakes
    // all be implemented. Compilation will fail if those
    // methods are missing from this class.

    public void changeCadence(int newValue) {
            cadence = newValue;
    }

    public void changeGear(int newValue) {
            gear = newValue;
    }

    public void speedUp(int increment) {
        speed = speed + increment;
    }

    public void applyBrakes(int decrement) {
        speed = speed - decrement;
    }

    void printState(){
        System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear);
    }

    public static void main(String[] args) {
        MyBike mBike = new MyBike();
        mBike.changeCadence(20);
        mBike.changeGear(3);
        mBike.speedUp(20);
        mBike.applyBrakes(20);
        mBike.printState();
    }
}

I understand that in this case I have to implement all the classes in interface within implementing class.我知道在这种情况下,我必须在实现 class 中实现接口中的所有类。 However, if I use abstract class, I don't need to implement them all.但是,如果我使用抽象 class,我不需要全部实现它们。 If that's the case why not always use abstract class, in case I might implement only partially or maybe all classes from interface?如果是这种情况,为什么不总是使用抽象 class,以防我可能只实现接口中的部分类或所有类? Why do we need to use non-abstract class?为什么我们需要使用非抽象的 class?

An abstract classs can NOT be instantiated by using new operator.不能使用 new 运算符来实例化抽象类。 Hence you need concrete implemented classes, of which you can make an instance object.因此,您需要具体实现的类,您可以在其中创建一个实例 object。

See Why can't we instantiate a abstract class in JAVA?请参阅为什么我们不能在 JAVA 中实例化抽象 class? for more details.更多细节。

Good read Interface vs Abstract Class Interface vs Abstract Class (general OO) Good read Interface vs Abstract Class Interface vs Abstract Class(通用OO)

If you have a abstract class if you call the constructor you have to manualy override the missing methods, thats not realy clean and looks a bit sketchy, if you dont use abstract classes you have all code in your class or a super class of that class If you have a abstract class if you call the constructor you have to manualy override the missing methods, thats not realy clean and looks a bit sketchy, if you dont use abstract classes you have all code in your class or a super class of that class

Using abstract classes, which implements an interface without actually implementing the methods directly, doesn't absolve you from implementing the interface methods at some point somewhere.使用实现接口而不直接实现方法的抽象类并不能免除您在某处某处实现接口方法的责任。 The task to implement the method has just be moved to the next class which inherits from your abstract class.实现该方法的任务刚刚移至下一个 class,它继承自您的抽象 class。 It is correct to write写是对的

public abstract class MyBike implements Bike {
}

but then the next class has this task to deal with the not implemented methods.但是接下来的 class 有这个任务来处理未实现的方法。 In the end, one class in the class inheritance hierarchy have to implement the methods defined by the interface.最后,class inheritance 层次结构中的一个 class必须实现接口定义的方法。

Keep in mind, you cannot write new MyBike() when MyBike is declared as static .请记住,当MyBike被声明为static时,您不能编写new MyBike()

To achieve the effect you describe add default methods to the Bike interface for the methods that you do not want the implementing class to have to instantiate and make the MyByke class concrete.为了达到您描述的效果,将默认方法添加到 Bike 接口,用于您不希望实现 class 必须实例化的方法并使 MyByke class 具体化。 I am not saying this will make sense for this particular example, only that it will solve the specific issue.我并不是说这对这个特定的例子有意义,只是说它会解决特定的问题。

BTW I suggest using conventional naming: setCadence rather than changeCadence, etc.顺便说一句,我建议使用常规命名:setCadence 而不是 changeCadence 等。

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

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