protected
, internal
, 除了private
),那不是更好吗?或者我们应该使用抽象类替代?如果一个接口是internal的,那么它的所有成员将对该程序集内部可见。如果一个嵌套接口是protected的,那么只有外部类的子类可以访问该接口。
对于一个接口而言,其在声明程序集之外的internal成员是没有意义的,同样声明在外部类中的protected成员也是如此。
接口的目的是描述实现类型和接口用户之间的协定。外部调用者不会关心,并且不应该强制关注实现细节,这正是internal和protected成员派上用场的地方。
对于被基类调用的protected成员,抽象类是指定基类和从中继承的类之间协定的理想方式。但在这种情况下,实现细节通常非常重要,除非它是一个退化的纯抽象类(其中所有成员都是抽象的),在这种情况下,protected成员是无用的。在这种情况下,使用接口,为实现类型提供单一的基类选择。
IImmutableWoozle
不会成为一些古怪的可变实现。 - supercatIInternalWoozle
,该接口实现了IWoozle
,并且Foo中使用任何IWoozle
的代码都可以检查是否还实现了IInternalWoozle
以便访问内部成员。.NET框架在某些地方就是这样做的。 - Mark Cidade通过在方法名前明确声明接口名称,可以隐藏接口实现:
public interface IInterface {
public void Method();
}
public class A : IInterface {
public void IInterface.Method() {
// Do something
}
}
public class Program {
public static void Main() {
A o = new A();
o.Method(); // Will not compile
((IInterface)o).Method(); // Will compile
}
}
public
修饰符。例如,为了使你的代码能够在 .NET 4.5 中工作,只需删除两个 public
即可。 - Andrewo.Method()
不能编译?有人能解释一下吗?为什么声明接口名称会隐藏接口实现? - Kyle Delaney这样做没有意义。接口是与公众签订的合同,您支持这些方法和属性。最好使用抽象类。
作为最受讨论的主题,让我发表两个我在SO上发现的优秀答案,当我想起这个问题时。
这个答案举了一个例子,说明派生类中非统一访问限定符的接口成员可能是无意义的。代码总比技术描述好。
对我来说,强制公开接口成员最糟糕的事情是接口本身可以是程序集内部的,但它所暴露的成员必须是公共的。 Jon Skeet在这里解释了这是设计上的遗憾。
这引出了一个问题,为什么接口没有被设计成具有成员的非公共定义。这可以使合同更加灵活。在编写不希望将类的特定成员暴露给程序集外部的程序集时,这非常有用。我不知道为什么。
interface IVehicle
{
void Drive();
void Steer();
void UseHook();
}
abstract class Vehicle // :IVehicle // Try it and see!
{
/// <summary>
/// Consuming classes are not required to implement this method.
/// </summary>
protected virtual void Hook()
{
return;
}
}
class Car : Vehicle, IVehicle
{
protected override void Hook() // you must use keyword "override"
{
Console.WriteLine(" Car.Hook(): Uses abstracted method.");
}
#region IVehicle Members
public void Drive()
{
Console.WriteLine(" Car.Drive(): Uses a tires and a motor.");
}
public void Steer()
{
Console.WriteLine(" Car.Steer(): Uses a steering wheel.");
}
/// <summary>
/// This code is duplicated in implementing classes. Hmm.
/// </summary>
void IVehicle.UseHook()
{
this.Hook();
}
#endregion
}
class Airplane : Vehicle, IVehicle
{
protected override void Hook() // you must use keyword "override"
{
Console.WriteLine(" Airplane.Hook(): Uses abstracted method.");
}
#region IVehicle Members
public void Drive()
{
Console.WriteLine(" Airplane.Drive(): Uses wings and a motor.");
}
public void Steer()
{
Console.WriteLine(" Airplane.Steer(): Uses a control stick.");
}
/// <summary>
/// This code is duplicated in implementing classes. Hmm.
/// </summary>
void IVehicle.UseHook()
{
this.Hook();
}
#endregion
}
这将测试代码。
class Program
{
static void Main(string[] args)
{
Car car = new Car();
IVehicle contract = (IVehicle)car;
UseContract(contract); // This line is identical...
Airplane airplane = new Airplane();
contract = (IVehicle)airplane;
UseContract(contract); // ...to the line above!
}
private static void UseContract(IVehicle contract)
{
// Try typing these 3 lines yourself, watch IDE behavior.
contract.Drive();
contract.Steer();
contract.UseHook();
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
接口在其方法中没有访问修饰符,使它们开放给适当的访问修饰符。这是有目的的:它允许其他类型推断出遵循接口的对象可用的方法和属性。给它们受保护/内部访问器会破坏接口的目的。
如果您坚持认为需要为方法提供访问修饰符,则要么将其从接口中省略,要么像您所说的那样使用抽象类。
我比较熟悉Java而不是C#,但是为什么你想在接口中使用私有成员变量呢?它没有任何实现并且对于实现类来说是不可见的,所以是无用的。接口存在的目的是为了指定行为。如果你需要默认行为,那么请使用抽象类。