界面中的访问修饰符 [英] access modifiers in interface

查看:161
本文介绍了界面中的访问修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

众所周知,在接口中,所有方法都必须是公共的,我们不能定义私有或受保护的方法.如果我们尝试尝试,则编译器将给出错误,因为它定义了接口.

我的问题是,为什么接口不接受除public之外的访问修饰符,其背后的原因是什么?

在此先感谢:)
--Amit

Hi to all,

As we know in an interface all methods must be public, we can not define private or protected methods. If we will try then compiler will give the error because its definition of the interface.

My question is why interfaces do not accept access modifiers other then public and what are the reasons behind it?

Thanks in advance :)
--Amit

推荐答案

因为这是接口的定义.
在C ++中,仍然可能具有接口的访问说明符,因为接口只不过是一个结构.
Because that is the definition of an interface.
In C++ it is still possible to have access specifiers for an interface because an interface is nothing but a struct.


am .net写道:
am.net wrote:

我的问题是,为什么接口不接受除公共之外的访问修饰符,这是什么原因??

my question is, Why interfaces not accept access modifiers other then public, what is the reason behind this???



非公共接口背后的原因是什么? :)

一个类的所有非公共方法都可以直接访问
(,如果您有适当的此上下文...
...在接口调用中不是这种情况):)



What would be a reason behind non-public interfaces ? :)

All non-public methods of a class could be accessed directly
(, if you have an appropriate this-context...
...that is not a case at an interface call) :)


因为这是接口定义(接口是合约).
接口中私有方法的含义是什么?
:)
Because that''s the interface definition (an interface is a contract).
What would be the sense of a private method in a interface?
:)


这篇关于界面中的访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆