简体   繁体   English

实现接口但将成员更改为私有

[英]Implementing an Interface but changing a member to be private

All members of an Interface are public by default. 默认情况下,接口的所有成员都是公共的。 But there are some properties in my interface that I want to be used as private members of some subclasses that implement my interface. 但是我的界面中有一些属性我想用作实现我的界面的一些子类的私有成员。 Is this something that can and is done or am I way off basis here. 这是可以而且已经完成的事情,还是我在这里的基础。 I'm working on using more Interfaces in my architecture these days so I'm not that well versed yet. 我现在正在我的架构中使用更多接口,所以我还不是那么精通。

The point of interfaces is that they provide a contract that other objects can use to communicate with your object. 接口的关键是它们提供了一个其他对象可以用来与对象通信的契约。 If you change a member which is declared as public in an interface to private then you're not fulfilling the contract - another object may need to read that property / call that method, and you must allow them to. 如果您将在接口中声明为public的成员更改为private那么您没有履行合同 - 另一个对象可能需要读取该属性/调用该方法,并且您必须允许它们。

An interface will never have private members as an interface is for "interfacing" between two objects. 接口永远不会有private成员,因为接口用于在两个对象之间“接口”。 Your internal private members don't matter to it as long as you hold up your end of the contract. 只要您坚持合同结束,您的内部private会员就无所谓。

Going on your question, and your use of the word "subclass", I don't think you've fully understood Interfaces yet. 继续你的问题,以及你对“子类”这个词的使用,我认为你还没有完全理解接口。

I know you've probably heard this a million times but, an Interface describes what an object DOES, and a Class is HOW it does it. 我知道你可能已经听过这一百万次但是,一个接口描述了一个对象是什么,一个类是如何做到的。 A Class IMPLEMENTS, an interface, it does not INHERIT from it. 一个类IMPLEMENTS,一个接口,它不会依赖它。

So, if you want, have an Interface for you base Class, or for your SubClasses, but your question makes me think you're thinking about a base Class (Abstract Class), not an Interface. 所以,如果你愿意,可以为你的基类或你的SubClasses设置一个接口,但是你的问题让我觉得你在考虑一个基类(抽象类),而不是一个接口。

Does that make sense? 那有意义吗?

As interface does not has an Access Modifier, if you still want your method private in the class which is implementing that interface, you can Implement that interface EXPLICITLY. 由于接口没有Access Modifier,如果您仍然希望您的方法在实现该接口的类中是私有的,则可以实现该接口EXPLICITLY。

In that way your class methods will be Private. 这样,您的类方法将是Private。

You have to fully understand what interfaces are. 您必须完全了解接口是什么。 In fact there are just descriptions of the expectations that outside world could have about the class members. 事实上,只有外部世界对阶级成员的期望才有描述。 It do not creates the member, it just informs that specified class have specified method to use in public scope. 它不创建成员,它只是通知指定的类具有在公共范围中使用的指定方法。 So, as you can see by interface you could only describe public members. 因此,正如您可以通过界面看到的,您只能描述公共成员。

On the other hand if you want to declare some private members that are fixed or virtual you can use classic inheritance with the abstract base class. 另一方面,如果要声明一些固定或虚拟的私有成员,可以将经典继承与抽象基类一起使用。 In this case you will make all methods that you want to implement in subclasses as abstract, and implement methods that you want to be defined in base class. 在这种情况下,您将要在子类中实现的所有方法都作为抽象,并实现您希望在基类中定义的方法。

Hope this helps.. Regards 希望这会有所帮助..问候

Interfaces are only good for public access. 接口仅适用于公共访问。 Internally, it would be strange for an object to refer to itself through an interface. 在内部,对象通过接口引用自身会很奇怪。

If you want to have private variables that you force an implementation of, you want to use an abstract class, and mark them as protected. 如果要使用强制执行的私有变量,则需要使用抽象类,并将它们标记为受保护。

Think a little about this - and you understand that this can not be done: 想一想 - 你明白这不可能做到:

Interfaces are like a contact. 接口就像一个联系人。 all the public fields of the interface are parts of the contact. 界面的所有公共字段都是联系人的一部分。

So, you can't hide them in a subclass... 所以,你不能将它们隐藏在子类中......
What would happen if someone were to upcast your class object to the interface's type ? 如果有人要将您的类对象向上转换为接口的类型,会发生什么?

You'd probably want to change your design - may be split your interface in to two interfaces? 您可能想要更改您的设计 - 可能将您的界面拆分为两个界面? or and interface and an abstract class? 或者接口和抽象类? we need more details to know... 我们需要更多细节才能知道......

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

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