[入门] 谁能帮解释一下这是为什么

purple_jade 2007-12-10
import std.stdio;

interface mix
{
void print(char[]);
}

template mixtem()
{
void print(char[] t)
{
writefln(t);
}
}

class A
{
void print()
{
writefln("A");
}
void print(int a)
{
writefln(a);
}
}

class B:A,mix
{
mixin mixtem;
void pr()
{
super.print();
super.print(18);
print("OK");
writefln("B");
}
}

public void main(char[][] args)
{
B b=new B();
//b.print();
//b.print(27);
b.print("mix");
b.pr();
A a=new B();
a.print();
a.print(56);
(cast(mix)a).print("interface");
getch();
}

谁能告诉我为什么去掉注释后不能通过编译?混入不是通过签名访问的吗?
qiezi 2007-12-10
file:///C:/tools/DMD/dmd/html/d/function.html

AliasDeclaration
Colorful 2007-12-10
请参见http://www.digitalmars.com/d/function.html关于Function Inheritance and Overriding和Function Overloading的规则。
引用

Function Inheritance and Overriding
A functions in a derived class with the same name and parameter types as a function in a base class overrides that function:
class A
{
    int foo(int x) { ... }
}

class B : A
{
    override int foo(int x) { ... }
}

void test()
{
    B b = new B();
    bar(b);
}

void bar(A a)
{
    a.foo(1); // calls B.foo(int)
}
However, when doing overload resolution, the functions in the base class are not considered:

class A
{
    int foo(int x) { ... }
    int foo(long y) { ... }
}

class B : A
{
    override int foo(long x) { ... }
}

void test()
{
    B b = new B();
    b.foo(1); // calls B.foo(long), since A.foo(int) not considered
    A a = b;
    a.foo(1); // issues runtime error (instead of calling A.foo(int))
}
To consider the base class's functions in the overload resolution process, use an AliasDeclaration:

class A
{
    int foo(int x) { ... }
    int foo(long y) { ... }
}

class B : A
{
    alias A.foo foo;
    override int foo(long x) { ... }
}

void test()
{
    B b = new B();
    bar(b);
}

void bar(A a)
{
    a.foo(1); // calls A.foo(int)
    B b = new B();
    b.foo(1); // calls A.foo(int)
}
If such an AliasDeclaration is not used, the derived class's functions completely override all the functions of the same name in the base class, even if the types of the parameters in the base class functions are different. If, through implicit conversions to the base class, those other functions do get called, an std.HiddenFuncError exception is raised:

import std.hiddenfunc;

class A
{
     void set(long i) { }
     void set(int i)  { }
}
class B : A
{
     void set(long i) { }
}

void foo(A a)
{   int i;
    try
    {
        a.set(3);   // error, throws runtime exception since
                    // A.set(int) should not be available from B
    }
    catch (HiddenFuncError o)
    {
i = 1;
    }
    assert(i == 1);
}

void main()
{
    foo(new B);
}
If an HiddenFuncError exception is thrown in your program, the use of overloads and overrides needs to be reexamined in the relevant classes.

A function parameter's default value is not inherited:

class A
{
    void foo(int x = 5) { ... }
}

class B : A
{
    void foo(int x = 7) { ... }
}

class C : B
{
    void foo(int x) { ... }
}


void test()
{
    A a = new A();
    a.foo(); // calls A.foo(5)

    B b = new B();
    b.foo(); // calls B.foo(7)

    C c = new C();
    c.foo(); // error, need an argument for C.foo
}


Function Overloading
In C++, there are many complex levels of function overloading, with some defined as "better" matches than others. If the code designer takes advantage of the more subtle behaviors of overload function selection, the code can become difficult to maintain. Not only will it take a C++ expert to understand why one function is selected over another, but different C++ compilers can implement this tricky feature differently, producing subtly disastrous results.

In D, function overloading is simple. It matches exactly, it matches with implicit conversions, or it does not match. If there is more than one match, it is an error.

Functions defined with non-D linkage cannot be overloaded.

purple_jade 2007-12-11
谢谢两位老兄的指点,D中的overload和C++、C#还是不同的:
If such an AliasDeclaration is not used, the derived class's functions completely override all the functions of the same name in the base class, even if the types of the parameters in the base class functions are different. If, through implicit conversions to the base class, those other functions do get called, an std.HiddenFuncError exception is raised
受益了谢谢
sw2wolf 2007-12-12
代码不能缩进吗? 这样看起太乱
lifaming15 2007-12-14
必须用override关键字才能override吗?
purple_jade 2007-12-14
我个人感觉在D中带不带override他都会给override,要是带了在基类中没有相同签名的方法就会报错,不带就不会。感觉上和java的不一样。不知道这是为什么。
oldrev 2007-12-14
1.0 和 2.0 行为不一样,参阅文档和 changelog
Global site tag (gtag.js) - Google Analytics