我是 C# 程序员,不熟悉 D 语言。我对 D 编程语言中的 OOP 有点困惑。
假设我有以下课程:
public class A {
protected void foo() {
writefln("A.foo() called.");
}
};
public class B : A {
public override void foo() {
writefln("B.foo() called.");
}
};
protected
修饰符意味着我可以访问继承类上的方法.foo()
,为什么这个D程序编译正常?
这是C#.NET的等价物:
using System;
public class A {
protected virtual void foo() {
Console.WriteLine("a.foo() called.");
}
};
public class B : A {
public override void foo() {
Console.WriteLine("b.foo() called.");
}
};
public class MainClass {
public static void Main(string[] args) {
A a = new A();
B b = new B();
a.foo();
b.foo();
}
};
它不编译并给出以下错误消息(如我所料):
test.cs(10,30): 错误 CS0507:
B.foo()': cannot change access modifiers when overriding
protected' 继承的成员 `A.foo()'
有人可以解释这种 D 行为吗?提前致谢。