简体   繁体   English

Java接口和继承

[英]Java Interfaces and inheritance

I have a very basic doubt about Java Interfaces and inheritance. 我对Java接口和继承有一个非常基本的疑问。 Suppose I have two classes A and B and one interface C with following definitions 假设我有两个类A和B以及一个具有以下定义的接口C

interface C{
 public void check();
}

class A implements C{
 public void check(){
  System.out.println("A");
 }
}

class B extends A implements C{

// Is the following method overriding the method from class A or implementing the method from C?
 public void check(){
  System.out.println("B");
 }

}

I am confused that whether it is over-riding or implementation of check() method in class B? 我很困惑,它是在类B中重写或实现check()方法?

It does both, they are not mutually exclusive. 两者兼有,它们不是互斥的。 The purpose of an interface is to define a method signature that should be available inside the implementing class. 接口的目的是定义在实现类内部应该可用的方法签名。

You should annotate the method with @Override though, it's just good form because it makes clear that it comes from a baseclass and it'll guard you against accidental typos. 不过,您应该使用@Override注释该方法,这是一种很好的形式,因为它清楚地表明它来自基类,并且可以防止意外的拼写错误。

As @Jeroen Vannevel and @EJP have mentioned above it's both overriding and implementing. 正如@Jeroen Vannevel和@EJP上面提到的那样,它既是重写又是实现。

In order to understand this I think you need to see it in the context of compile/run time. 为了理解这一点,我认为您需要在编译/运行时的上下文中查看它。

You have the following possible scenarios: 您有以下几种可能的情况:

C c = new B();
c.check();

At compile-time you see C#check() (you can use your IDE to get you where c.check() points to) at runtime you see the overridden B#check() 编译时,您会看到C#check() (可以使用IDE来获取c.check()指向的位置),在运行时您会看到被覆盖的B#check()

A a = new B();
a.check();

At compile-time you see A#check() (you can use your IDE to get you where c.check() points to) at runtime you see the overridden B#check() 编译时,您会看到A#check() (可以使用IDE来获取c.check()指向的位置),在运行时,您会看到被覆盖的B#check()

B b = new B();
b.check();

At compile-time you see B#check() (you can use your IDE to get you where c.check() points to) at runtime you see the overridden B#check() 编译时,您会看到B#check() (可以使用IDE来获取c.check()指向的位置),在运行时您会看到被覆盖的B#check()

If alternatively you are passing the method call directly in a method: 如果您要在方法中直接传递方法调用,请执行以下操作:

someMethod(new B().check())

then this equates the last of the above scenarios 那么就等于上述情况的最后一个

它既是最重要的,也是实现的。

In your example: 在您的示例中:

class B extends A implements C{

// Is the following method overriding the method from class A or implementing the method from C?
 public void check(){
  System.out.println("B");
 }

}

You are defining the check method in interface C as: 您正在接口C中将check方法定义为:

 public void check(){
  System.out.println("B");

You are allowed to do this as interfaces don't contain the definition of the method in the interface when they are created and can thus be used over and over again for things which are similar enough to use the same method with a few tweaks. 允许您执行此操作,因为接口在创建时不包含接口中方法的定义,因此可以一遍又一遍地用于类似的事情,只需稍作调整即可使用相同的方法。

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

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