为什么我们不能在接口中声明访问修饰符? [英] Why Can't we declare access modifier in interface?

查看:224
本文介绍了为什么我们不能在接口中声明访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口中成员的默认访问修饰符是public,因为我们知道每一个。但是有一个人问我,如果我们在接口中声明访问修饰符public会发生什么。

我说它抛出编译时间错误。然后他问我



为什么我们不能在接口中声明访问修饰符?

是否有任何编译器规范未声明接口中的访问修饰符

The default access modifier of member in interface is public as we know every one.But one guy ask me that what will happen if we declare access modifier public in interface.
I said it throws compile time error.then he asked me

Why Can't we declare access modifier in interface?
is there any compiler specifications to not declare access modifier in interface

推荐答案

将接口视为外部用户的定义,如果我调用它,对象中的功能是什么样的。接口描述了方法和属性的签名,正如其他人所看到的那样。



来自访问修饰符(C#编程指南) [ ^ ]

直接在命名空间内声明的接口可以声明为public或internal,就像类和结构一样,接口默认为内部访问。接口成员总是公共的,因为接口的目的是允许其他类型访问类或结构。没有访问修饰符可以应用于接口成员。



现在什么接口没有定义是它没有定义如何使用什么方法构建功能。因此,它不是一个定义,如何实现,而是一旦实现如何从外部使用。



话虽如此,定义约束是没有意义的,必须在界面内实现什么,因为这不是它的目的



一个例子:

想想一个数字温度计。它显示温度,它可能具有记住最高和最低值的功能,这些值可以使用按钮重置,也许您可​​以更改华氏温度和摄氏温度之间的比例。这是你的界面,如何使用它。



你看不到温度计是如何在内部构建的,使用什么元件,如何完成接线等等。更重要的是,这些并不重要,因为如果制造商年复一年地制造相同型号,他们可能会找到更多适用的组件,而内部设计可能会发生变化。也许一些接线被带走并被更先进的芯片等取代。



内部零件的所有这些变化对如何使用温度计没有影响。即使内部实现可能发生显着变化,界面也保持不变。



从用户的角度来看,你对温度计的构建方式不感兴趣,只对你如何使用它感兴趣。因为内部逻辑可能没有在界面的定义中定义:)
Consider interface as a definition for the outside users, what does functionality in an object look like if I'd call it. Interface describes the 'signature' of methods and properties as others see it.

From Access Modifiers (C# Programming Guide)[^]
"Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members."

Now what interface doesn't define is that it doesn't define how, using what methods, a functionality is built. So it's not a definition, how to implement but how to be used from outside once implemented.

Having that said, it wouldn't make sense to define constraints, what must be implemented inside an interface since that's not it's purpose

An example:
Think about a digital thermometer. It shows you the temperature, it may have a functionality to remember the highest and the lowest values, those values may be reset using a button and perhaps you can change the scale between Fahrenheit and Celsius. This is you interface, how to use it.

What you don't see is how the thermometer is built internally what components are used, how is the wiring done and so on. More importantly those do not matter since if the manufacturer makes the same model year after year, they may find more applicable components and the internal design may change. Perhaps some wiring is taken away and replaced by more advanced chip etc.

All these changes in inner parts have no effect on how to use the thermometer. The interface stays the same even if the internal implementation may change dramatically.

From the user point of view, you're not interested how the thermometer is built, only how you use it. Because of that the inner logic may not be defined in the definition of the interface :)


这篇关于为什么我们不能在接口中声明访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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