简体   繁体   English

实现不完整接口的抽象类

[英]abstract classes which implements an incomplete interface

For example, I have an interface with 4 methods. 例如,我有一个有4种方法的接口。

If I implement this interface incomplete in a class, the class must be abstract. 如果我在类中实现此接口不完整,则该类必须是抽象的。 Right? 对?

For example, I leave one method out. 例如,我留下了一种方法。 So now I am writing a subclass which extends this class. 所以现在我正在编写一个扩展这个类的子类。 Now I implement the last method of the interface. 现在我实现接口的最后一个方法。

What happens, if I call this method in the abstract superclass? 如果我在抽象超类中调用此方法会发生什么? Nothing! 没有! It works. 有用。 But why? 但为什么?

What will happen, if I write several classes, extending this abstract class and implement the fourth method of the interface? 如果我编写几个类,扩展这个抽象类并实现接口的第四个方法会发生什么? Which one will be called? 哪一个会被称为?

A interface is a contract you define the signature of your methods, only behaviour and constants, all methods are public and abstract. 接口是您定义方法签名的合约,只有行为和常量,所有方法都是公共和抽象的。

In an Abstract Class you define behaviour and state, you can have some implementation and abstract methods. Abstract Class ,您可以定义行为和状态,您可以使用一些实现和抽象方法。

For example for your question: 例如,您的问题:

If I implement this interface incomplete in a class, the class must be abstract. 如果我在类中实现此接口不完整,则该类必须是抽象的。 Right? 对?

Right

For example, I leave one method out. 例如,我留下了一种方法。 So now I am writing a subclass which extends this class. 所以现在我正在编写一个扩展这个类的子类。 Now I implement the last method of the interface. 现在我实现接口的最后一个方法。

What happens, if I call this method in the abstract superclass? 如果我在抽象超类中调用此方法会发生什么? Nothing! 没有! It works. 有用。 But why? 但为什么?

Will run cause in runtime execution knows what class is. 将在运行时执行运行导致知道什么是类。 this is polymorphism. 这是多态性。

What will happen, if I write several classes, extending this abstract class and implement the fourth method of the interface. 如果我编写几个类,扩展这个抽象类并实现接口的第四个方法,会发生什么。 Which one will be called? 哪一个会被称为?

The one you instanciate in your client code :D 您在客户端代码中实例化的那个:D

public interface Operation{ 
 void operation1();
 void operation2();

}

I don't implement operation2 我没有实现operation2

public abstract class ClaseBase implements Operation{
//with final im saying childs wont override
 public final void operation1{
   // do something
   operation2();
   // do something
 }


}

//have to implements operation2 cause it's a concrete class //必须实现operation2,因为它是一个具体的类

public class Child extends ClaseBase{
 void operation2(){
   System.out.println("Something");
 }

}

You can't instanciate an AbstractClass. 您无法实现AbstractClass。

In your client code 在您的客户端代码中

ClaseBase base = new Child();
base.operation1(); // and it's gonna to call operation2 in childClass 

Abstract class is useful with Template Method pattern. 抽象类对于Template Method模式很有用。

Keep in mind that when you instantiate a subclass and refer it as super/abstract class or interface, it's still the instance of the subclass . 请记住,当您实例化一个子类并将其称为超级/抽象类或接口时, 它仍然是子类的实例 Any method called on the object bubbles up from subclass to super class if its not available in the sub class. 如果在子类中不可用,则调用该对象的任何方法都会从子类冒泡到超类。

Thus if you have: 因此,如果你有:

  interface GemeInterface
        getStartScreen()
        getCloseScreen()

  abstract class AndroidGame implement GemeInterface
        getCloseScreen()

  class MrNomGame extends AndroidGame
        getStartScreen()

  class MrPetNomGame  extends AndroidGame
        getStartScreen()

and using as 并使用as

//You can't instantiate AndroidGame hence the it has to be instance of a subclass
AndroidGame androidGame1 = new MrNomGame ();
androidGame1.getStartScreen(); 

//You can't instantiate AndroidGame hence the it has to be instance of a subclass
AndroidGame androidGame2 = new MrPetNomGame ();
androidGame2.getStartScreen();

Then also it works as androidGame1.getStartScreen() --> calls the method from MrNomGame, while androidGame2.getStartScreen() --> calls the method from MrPetNomGame . 然后它也可以作为androidGame1.getStartScreen() - >从MrNomGame调用方法,而androidGame2.getStartScreen() - >调用MrPetNomGame中的方法

Both the calls ie androidGame1.getCloseScreen() and androidGame2.getCloseScreen() will end up calling the method from AndroidGame as it's not available in the sub classes. 两个调用,即androidGame1.getCloseScreen()androidGame2.getCloseScreen()都将最终从AndroidGame调用该方法,因为它在子类中不可用。

It depends on the object that you created. 这取决于您创建的对象。 Super class can refer to subclass objects. 超类可以引用子类对象。 So the actual object's method would be called. 因此将调用实际对象的方法。

interface A{

public void test1();

public void test2();

}

public abstract class B implements A{

public void test1(){

}

public abstract public void test2();

}

public class C extends B{

public void test2(){

}
}

B b = new C();
b.test2();//This works because the object that is refered by B is actually an object of C

Infact you can also do: 事实上你也可以这样做:

A a = new C();
a.test1();
a.test2();

Again though A is an interface(super type), but it is actually referring to a concrete implementation C(subtype) object and hence this works. 虽然A是一个接口(超类型),但它实际上是指一个具体的实现C(子类型)对象,因此这是有效的。

So at the compile time, compiler checks if A/B has a method called test2(), if yes compile is happy and it compiles successfully. 所以在编译时,编译器会检查A / B是否有一个名为test2()的方法,如果是,编译很开心并且编译成功。 But at the runtime, you invoke test2() method on the object and object actually is of class C which has the complete implementation. 但是在运行时,你在对象和对象上调用test2()方法实际上是C类,它具有完整的实现。

What happens, if I call this method in the abstract superclass? 如果我在抽象超类中调用此方法会发生什么? Nothing! 没有! It works. 有用。 But why? 但为什么?

You cannot invoke this method on the abstract superclass because it can't be instantiated. 您无法在abstract超类上调用此方法,因为它无法实例化。 You would only be invoking it on a sub-class of this abstract super-class which would be forced to provide an implementation (or be declared abstract itself). 你只会在这个抽象超类的子类上调用它,它将被迫提供一个实现(或者被声明为抽象本身)。

So, when you invoke this method, its the implementation provided by the non-abstract sub-class that gets executed. 因此,当您调用此方法时,它将由执行的非抽象子类提供

The funny thing is that a method of the class 'AndroidGame' calls 'getStartScreen()'. 有趣的是,类'AndroidGame'的方法调用'getStartScreen()'。 But there could be several classes like 'MrNomGame' which extending 'AndroidGame'. 但是可能有几个类似'MrNomGame'的类扩展了'AndroidGame'。 Then which method would be executed? 那么将执行哪种方法?

The method code executed would depend on the runtime type of the actual object that you invoke this method on. 执行的方法代码取决于您调用此方法的实际对象的运行时类型。 So, you could have a code like this 所以,你可以有这样的代码

AndroidGame game1 = new MrNomGame();
AndroidGame game2 = new SomeOtherGame();

game1.getStartScreen(); // invokes MrNomGame's version
game2.getStartScreen(); // invokes SomeOtherGame's version

and due to dynamic binding the JVM would invoke the getStartScreen() as implemented by the class type of the actual object that the reference points to. 并且由于动态绑定 ,JVM将调用getStartScreen() ,这是由引用指向的实际对象的类类型实现的。

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

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