本文介绍了我应该实现抽象类中存在的所有方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码片段:

public abstract class MyAbstractClass {

    public abstract void a();
    public abstract void b();
}


public class Foo extends MyAbstractClass {

    public void a() {
        System.out.println("hello");
    }

    public void b(){
        System.out.println("bye");
    }
}


public class Bar extends MyAbstractClass {

    public void a() {
        System.out.println("hello");
    }

    public void delta() {
        System.out.println("gamma");
    }
}

我有几个问题:

Q-1 :- 我应该实现 abstract 类中的所有方法吗?

Q-1 :- Should I implement ALL the methods in abstract class?

Q-2 :- 实现类可以有自己的方法吗?

推荐答案

当你扩展一个 Interface 或一个 Abstract 类时,你正在与那个.您在合同中说:

When you extend an Interface or an Abstract class you are creating a contract of sorts with that superclass. In the contract you are saying:

我将在我的超类中实现所有未实现的方法"

如果你不实现所有未实现的方法,那么你就违反了合同.一种不破坏契约的方法是让你的子类 Abstract 以及一种说法

If you do not, implement all the unimplemented methods, then you are breaking your contract. A way to not break your contract is make your subclass Abstract as well as a way of saying

我还没有实现合同中的所有类,我将让我的子类实现它们".

对于你现在的 bar 类,你必须实现 b() 或使 bar 成为一个 Abstract 类或者您没有履行与 MyAbstractClass

For your class bar right now, you must implement b() or make bar an Abstract class or you are not fulfilling your contract with MyAbstractClass

基本思想是:

接口:我的方法都没有实现.子类必须实现我的所有方法才能实现我.(注意:我相信默认接口已添加到 Java 8 中,这可能会有所改变)

Interface: None of my methods are implemented. A subclass must implement all my methods in order to implement me. (Note: I believe default interfaces have been added to Java 8 which may change this a bit)

示例:

 public interface myInterface
 {
     //My subclasses must implement this to fulfill their contract with me
     public void methodA();

     //My subclasses must implement this to fulfill their contract with me
     public void methodB();
 }

抽象:我可能会实现我的一些方法,但我也会将方法保留为抽象,以便我的子类必须实现,因为它们可以比我更好地实现这些类来满足他们的需求.

Abstract: I may implement some of my methods, but I will also leave methods as abstract so that my subclasses must implement because they can implement those classes to suit their needs better than I can.

示例:

 public abstract class myAbstractClass
 {
     //My subclasses must implement this to fulfill their contract with me
     public abstract void methodC();

     public void helloWorld()
     {
         System.out.println("Hello World");
     }
 }

Abstract 类还可以扩展接口,因此它们可以实现它们的一些方法.但是它们也可以保留一些未实现的方法,以便子类可以实现它们.如果一个接口方法没有实现,就不需要声明它是抽象的,它已经在契约中了.

Abstract classes can also extend interfaces so they can implement some of their methods. But they can also leave some of the methods unimplemented so the subclass can implement them. If you leave an interface method unimplemented, there is not need to declare it abstract, it is already in the contract.

示例:

  public abstract class myAbstractClass2 implement myInterface
  {
      @Override
      public void methodA()
      {
          // this fulfills part of the contract with myInterface.
          // my subclasses will not need to implement this unless they want to override
          // my implementation.
      }

      //My subclasses must implement this to fulfill their contract with me
      public abstract void methodD();
  }

所以本质上,抽象类与其超类没有那么严格的契约,因为它可以将其方法委托给它的子类.

So in essence, an abstract class doesn't have as strict a contract with it's superclass because it can delegate its methods to its subclasses.

Regular Class:(我使用常规表示非接口和非抽象).我必须从我的所有超类中实现所有未实现的方法.这些类具有绑定契约.

Regular Class: (I use regular to mean non-interface, and non-abstract). I must implement all unimplemented methods from all of my superclasses. These classes have a binding contract.

示例:

 public class mySubClass extends myAbstractClass2
 {
     @Override
     public void methodB()
     {
         //must be implemented to fulfill contract with myInterface
     }

     @Override
     public void methodD()
     {
         //must be implemented to fulfill contract with myAbstractClass2
     }

     public void myMethod()
     {
        //This is a method specifically for mySubClass.
     }
 }

这篇关于我应该实现抽象类中存在的所有方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:30