简体   繁体   English

为什么接口变量是公开的

[英]Why are interface variables public

I know that Java interface variables are by default public static and final.我知道 Java 接口变量默认是 public static 和 final 的。

I understood why they are static and final by seeing this discussion通过看到这个讨论,我明白了为什么它们是静态的和最终的

But I didn't reason behind why interface variables are public by default.但我没有解释为什么默认情况下接口变量是公开的。

If any one knows the reason behind why interface variables are always public and why can not we have a protected or default variables in an interface, kindly explain me.如果有人知道为什么接口变量总是公开的以及为什么我们不能在接口中使用受保护或默认变量的原因,请解释一下。

Kindly don't ask me to google for it because I have been googling for it for more than two hours and there is no clear explanation from any site about why the variables are public by default.请不要让我用谷歌搜索它,因为我已经用谷歌搜索了两个多小时,任何网站都没有明确解释为什么默认情况下变量是公开的。

I have seen the below blog also but there is no clear info about my query.我也看过下面的博客,但没有关于我的查询的明确信息。

Interfaces are "behaviour blueprints".接口是“行为蓝图”。 Unlike classes, they shouldn't (and do not) have states.与类不同,它们不应该(也不应该)有状态。

Since instance variables represents the state of an object, interfaces do not have instance variables (being them private or public).由于实例变量表示对象的状态,接口没有实例变量(它们是私有的或公共的)。 And since private static variables represent the state of a class, they don't have private static variables either.由于私有静态变量代表类的状态,因此它们也没有私有静态变量。

an interface has functions which the using class needs to communicate with the outside.接口具有使用类需要与外部通信的功能。 Through that you can have a few different classes which all have the same functions for the calling classes.通过它,您可以拥有几个不同的类,它们都具有相同的调用类功能。

Now if you make a variable in an interface, what would it bring, if the variable is not public?现在,如果您在接口中创建一个变量,如果该变量不是公共的,它会带来什么? the interface is there to generalizes the communication with other classes and if the variable or the function is not public it can not be used from other classes.接口用于概括与其他类的通信,如果变量或函数不是公共的,则不能从其他类中使用。 i hop this will help you.我希望这会帮助你。

Interfaces where designed to be a means of communication between objects.接口设计为对象之间的通信方式。 This is why all methods have to be public.这就是为什么所有方法都必须是公开的。 It's merely a design choice.这只是一种设计选择。

EDIT: As suggested by @jwenting, it's better to say that this is a core architecture choice instead of a design choice.编辑:正如@jwenting 所建议的,最好说这是一个核心架构选择,而不是一个设计选择。

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

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