面向对象关于接口、抽象类、具体类的质疑
1) OnCreate 是 ClsLast 类实例化对象的公共方法。 但我想将 OnCreate 方法限制为受保护。
interface InterFace {
void OnCreate();
}
class ClsFirst implements InterFace {
@Override
public void OnCreate() { }
}
class ClsLast extends ClsFirst{ }
class Test {
static void main(){
ClsLast objClsAct = new ClsLast();
objClsAct.OnCreate();
}
}
2) 如果我在 InterFace 中将 OnCreate 方法设置为受保护,
interface InterFace {
protected void OnCreate();
}
则会收到如下错误: 接口方法OnCreate的修饰符非法;仅公开&允许抽象
3) 如果我在实现 InterFace 的 ClsFirst 类中设置 protected 方法,如下所示:
interface InterFace {
void OnCreate();
}
class ClsFirst implements InterFace {
@Override
protected void OnCreate() { }
}
我收到此错误: 无法降低从 InterFace 继承的方法的可见性
4) 当我将 ClsFirst 更改为抽象类并实现 InterFace 时,
interface InterFace {
void OnCreate();
}
abstract class ClsFirst implements InterFace {
}
我没有在 ClsFirst 类中实现 OnCreate 方法,但是Cls最后为什么?
总结
- 如何设置接口方法只能在派生类中使用?
- 为什么我无法在接口内设置具有受保护访问权限的方法?
- 为什么我实现InterFace后不能将Class的访问器类型设置为与public不同的类型?
- 抽象类,即使它们实现了接口,也不必添加未实现的方法本身,直到一个类派生抽象类。为什么?
非常感谢您从现在开始的友好答复。
1) OnCreate is public method of instantiated object from ClsLast class. But I wanted to restrict OnCreate method as protected.
interface InterFace {
void OnCreate();
}
class ClsFirst implements InterFace {
@Override
public void OnCreate() { }
}
class ClsLast extends ClsFirst{ }
class Test {
static void main(){
ClsLast objClsAct = new ClsLast();
objClsAct.OnCreate();
}
}
2) If I set OnCreate method as protected in InterFace
interface InterFace {
protected void OnCreate();
}
I'm getting error like this:Illegal modifier for the interface method OnCreate; only public & abstract are permitted
3) If I set protected the method inside the ClsFirst class which implements InterFace like this:
interface InterFace {
void OnCreate();
}
class ClsFirst implements InterFace {
@Override
protected void OnCreate() { }
}
I'm getting this error:Cannot reduce the visibility of the inherited method from InterFace
4) When I change the ClsFirst as abstract class and implements InterFace
interface InterFace {
void OnCreate();
}
abstract class ClsFirst implements InterFace {
}
I haven't to implement OnCreate method inside the ClsFirst class but ClsLast why?
Summary
- How can I set Interface methods can only be used in derived classes?
- Why can't I set methods with protected access inside Interface?
- Why can't I set the accessor type of Class different than public after I implement the InterFace?
- abstract classes that even if they implements an interface don't have to add unimplemented methods itself until one class derives abstracted classes. Why?
Thank you so much for your kind answers from now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据定义,
接口
是一个公共契约
。通过实现一个接口,您承诺提供一组方法(以及其他语言的属性)。接口与抽象类没有任何共同点,因为您可以在单个类上实现任意数量的接口,并且您可以
实现
而不是派生
从它。抽象类是一个基类,它就像一个部分功能的基类,用于在不同类之间共享实现细节并提供契约。然而,它更多的是所有派生类的内部契约。大多数时候,抽象类用于强制所有派生类采用通用模式,而不是共享公共契约。
抽象类可以强制派生类实现方法(或使用
virtual
关键字提供合适的默认实现)并使用此方法(这称为模板模式)。如果在抽象类上实现接口,则不必提供所有接口方法,因为抽象类无法实例化。但是,如果您没有实现所有接口方法,则层次结构中的第一个非抽象类必须提供缺少的方法。
An
interface
is per definition apublic contract
. By implementing an interface you promise to provide a set of methods (and properties in other languages).An interface has nothing in common with
abstract
classes because you can implement as many interfaces as you want on a single class and youimplement
instead ofderive
from it.An
abstract class
is a base class, its like a partially functionally base which is used to share implementation details between different classes and provides a contract, too. However it's more an internal contract for all derived classes. Most times abstract classes are used to force all derived classes into a common pattern instead of sharing a public contract.An abstract class can force a derived class to implement a method (or provide a suitable default implementation using the
virtual
keyword) and take use of this method (this is called Template Pattern).If you implement an interface on an abstract class you don't have to provide all interface methods because the abstract class can not be instantiated. However if you don't implement all interface methods the first non abstract class in your hierarchy has to provide the missing methods.
接口方法始终是公共的。您无法缩小这些方法的范围。在抽象类中使用受保护的抽象方法。
Interface methods are always public. You can not reduce the scope of these methods. Use a protected abstract method instead in an abstract class.
你做不到。如果你有一个只实现接口契约中定义的一部分的中间类,你可以将方法设置为虚拟或抽象。
你也不能这样做,原因很简单,接口定义了一个契约,一组要求实现必须具有的操作。您可以在抽象类中显式实现该接口,或者将整个接口限制为“内部”,至少在 C# 中,以防万一您想将其保留在“内部”。
你不能这样做,因为你承诺实现一个接口。
因为一旦将类标记为抽象,您间接指出的另一件事是您无法实例化它。因此,您将实现接口的要求传递给您可以创建实例的第一个类(我的意思是继承链)。
广告1。 (c# 示例1)
You cannot do it. You can set methods both as virtual or abstract if you have an middle class which implements only a part of defined in interface contract.
You also cannot do it and the reason is simple, interface defines a contract, a set of operations which implementation is requested to have. You can implement the interface explicitly in your abstract class or restrict whole interface to be "internal", at least in c# in case if you would like to keep it 'in-house'.
You cannot do it because you promised to implement an interface.
Because another thing which you indirectly pointed once you mark class as an abstract is that you cannot instantiate it. Therefore you pass the requirement of implementing interface to the first class which you can create the instance of (I mean the chain of inheritance).
ad1. (c# example1)
实际上,您可以隐藏该方法,直到通过显式实现接口将您的具体类强制转换为“接口”实例:
Actually you can hide the method, until the your concrete class is casted to an "Interface" instance by implementing the Interface explicitly:
1) 为什么要首先实现一个受保护的
接口
?它不会增加任何价值。您已经有办法确保所有派生类都遵守给定的契约:2) 接口定义一个
public
契约。通过public
我的意思是它具有与界面本身相同的可访问性。这就是为什么在定义接口成员时不允许使用辅助功能修饰符。3) 您不能降低任何虚拟成员或界面成员的可访问性,否则会违反界面合同。在您的示例中
((IOnCreate)ClsFirst).OnCreate()
是不可能的,因为OnCreate()
不可访问,这意味着ClsFirst
没有实现IOnCreate
接口。1) Why would you want to implement a protected
interface
to begin with? It does not add any value. You already have a way to make sure all derived classes comply with a given contract:2) Interfaces define a
public
contract. Bypublic
I mean that it has the same accesibility as the interface itselft. That is why accesibility modifiers are not permitted when defininginterface
members.3) You can not decrease accesibility of any virtual member or interface members as you would be breaking the interface's contract. In your example
((IOnCreate)ClsFirst).OnCreate()
would not be possible asOnCreate()
is not accesible, which would mean thatClsFirst
does not implementIOnCreate
interface.