简体   繁体   English

C# 接口和类。 接口中需要的类中使用的变量不能是私有的,或者私有的只能是公共的。 为什么?

[英]C# Interfaces and classes. Variables used in the class needed in the interface can't be private, or private only public. Why?

I have a problem with interfaces in C#.我在 C# 中遇到接口问题。 I made an interface called IPerson that needs a name and age.我制作了一个名为 IPerson 的界面,它需要一个名字和年龄。 After that I created a class that inherits from that interface, so it needs a name and age.之后,我创建了一个继承自该接口的类,因此它需要一个名称和年龄。 When I try to write these variables in the class, they can only be public and I don't understand why.当我尝试在类中编写这些变量时,它们只能是公共的,我不明白为什么。

public interface IPerson
{
    string name { get; set; }
    int age { get; set; }
}

public class Person : IPerson
{
    // If I try to make them private or protected, they don't work. and IPerson will be colored red. Only if they are public it's good, why ?
    private string name { get; set; } 
    private int age { get; set; }
}

Perhaps what you want is to implement the interface explicitly ...也许您想要的是显式实现接口......

eg例如

public interface IPerson
{
    string Name { get; set; }
    int Age { get; set; }
}

public class Person : IPerson
{
    string IPerson.Name { get; set; } 
    int IPerson.Age { get; set; }
}

These members can only be accessible publicly via a reference to the interface.这些成员只能通过对接口的引用公开访问。

ie You can't do the following...即您不能执行以下操作...

Person me = new Person();
Console.WriteLine(me.Name);  // Won't compile

but you can do the following...但您可以执行以下操作...

IPerson me2 = new Person();
Console.WriteLine(me2.Name); // Will compile

An interface is a kind of contract that you state.接口是您声明的一种契约。 Everything declared in the interface needs to be accessible in the type that inherits from said interface.接口中声明的所有内容都需要在继承自所述接口的类型中可访问。 When you now declare a method as private, you can't access it from the other object which tries to enact the contract, resulting in the interface not being implemented correctly.当您现在将一个方法声明为私有方法时,您无法从试图制定契约的其他对象访问它,从而导致接口无法正确实现。

This is also the reason, why interfaces do not have accessor declarations.这也是接口没有访问器声明的原因。 The properties, methods and events defined in the interface are required to be public, because otherwise accessing the class instance through the interface would not work.接口中定义的属性、方法和事件都需要是公共的,否则通过接口访问类实例将不起作用。

In your example, you derive Person from IPerson .在您的示例中,您从IPerson派生Person Now imagine, you use it like this:现在想象一下,你像这样使用它:

IPerson c = GetCustomer();

where GetCustomer is defined like this:其中GetCustomer定义如下:

public IPerson GetCustomer() {
    // internal code
}

All you have access to now, is the given values declared in the interface.您现在可以访问的只是接口中声明的给定值。 This means that name and age are defined in the interface and are accessible.这意味着nameage在界面中定义并且可以访问。
Let's say, Person also declares some other properties, like IPerson parent { get; set; }假设 Person 还声明了一些其他属性,例如IPerson parent { get; set; } IPerson parent { get; set; } IPerson parent { get; set; } . IPerson parent { get; set; } . In this case, the parent may be of type Person or of any other type that inherits from IPerson.在这种情况下,父对象可能是Person类型,也可能是从 IPerson 继承的任何其他类型。 But you can always be sure that the object that gets assigned to this property derives from IPerson and will have the properties name and age defined.但是您始终可以确定分配给此属性的对象来自IPerson并且将定义属性nameage

There is also no reason to declare an interface for private members.也没有理由为私有成员声明接口。 What would be the benefit your code has from that?你的代码会从中得到什么好处? Interfaces exist to connect objects that do not know much about the other type, except the interface.接口的存在是为了连接对其他类型知之甚少的对象,除了接口。 Think of it, as a power plug: You plug it into the socket, but it does not care where the power comes from.把它想象成一个电源插头:你把它插到插座上,但它并不关心电源从哪里来。 And likewise, the socket doesn't care where the power is going.同样,插座也不关心电源的去向。 It is just the case that both use an interface the other can interact with.只是两者都使用另一个可以交互的接口。
Using an interface in a class just for itself would be pointless, because there is no information that is hidden or needs to be abstracted within a class.在类中只为自己使用接口是没有意义的,因为在类中没有隐藏或需要抽象的信息。

As Max Play said:正如 Max Play 所说:

An interface is a kind of contract that you state.接口是您声明的一种契约。 Everything declared in the interface needs to be accessible in the type that inherits from said interface.接口中声明的所有内容都需要在继承自所述接口的类型中可访问。

Everything you place in an interface, must be accessible in any instance of your implemented object, unless it is used within it an explicit implementation.您放置在接口中的所有内容都必须在您实现的对象的任何实例中都可以访问,除非它在显式实现中使用。

But if you still want to use a pattern where you want to choose which property can be private, public or protected, an option is to create an abstract class , instead of the interface但是如果你仍然想使用一种模式来选择哪些属性可以是私有的、公共的或受保护的,一个选项是创建一个抽象类,而不是接口

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

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