一次调用适合多个重载方法现象研究

博客围绕编译器选择重载方法展开。通过具体程序示例,展示了在实参有多个适用重载方法时的不同情况,包括实参引用对象类型不同、基类和接口情况等。最后总结出编译器选择重载方法的四条原则,如优先匹配类型、选最近基类等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用重载的方法时,编译器会根据实参的数量和类型的不同选择调用适合的方法。那么如果一次调用使用的实参有多个重载方法可以适合时,编译器会怎么选择呢?见下面的程序。

class Base{}

class
Derive : Base{}

class
DDerive : Derive{}

public
class TestMain
{
    [STAThread]
    static void Main(string[] args)
    {
        DDerive dd = new DDerive();
        Base b = dd;
        Derive d = dd;

        TestMothed(b);
        TestMothed(d);
        TestMothed(dd);
    }
    static void TestMothed(Base b)
    {
        Console.WriteLine("Base");
    }
    static void TestMothed(Derive d)
    {
        Console.WriteLine("Derive");
    }
}

Base{}

class
Derive : Base{}

class
DDerive : Derive{}

public
class TestMain
{
    [STAThread]
    static void Main(string[] args)
    {
        DDerive dd = new DDerive();
        Base b = dd;
        Derive d = dd;

        TestMothed(b);
        TestMothed(d);
        TestMothed(dd);
    }
    static void TestMothed(Base b)
    {
        Console.WriteLine("Base");
    }
    static void TestMothed(Derive d)
    {
        Console.WriteLine("Derive");
    }
}

上面程序可以看到,第2次和第3次的TestMothed方法调用显然两个TestMothed重载方法都适用。而第1次TestMothed的调用虽然实参b的类型是Base类,但b引用的对象则是DDerive类。这三次方法调用会调用哪个重载方法呢?运行结果如下:

    Base
    Derive
    Derive

再把代码修改一下:

class Base{}

interface IInterface{}

class
Derive : Base, IInterface{}

public
class TestMain
{
    [STAThread]
    static void Main(string[] args)
    {
        Derive d = new Derive();

        TestMothed(d);
    }
    static void TestMothed(Base b)
    {
        Console.WriteLine("Base");
    }
    static void TestMothed(IInterface i)
    {
        Console.WriteLine("IInterface");
    }
}

这段代码根本不能编译,因为Base类和IInterface接口都是Derive类的基类(接口)且离Derive类一样近。

再把代码修改一下:

class Base{}

class
Derive : Base{}

class
DDerive : Derive{}

public
class TestMain
{
    [STAThread]
    static void Main(string[] args)
    {
        DDerive dd = new DDerive();
        TestMothed(dd, dd);
    }
    static void TestMothed(Base b, Derive d)
    {
        Console.WriteLine("Base, Derive");
    }
    static void TestMothed(Derive d, Base b)
    {
        Console.WriteLine("Derive, Base");
    }
}

Base{}

class
Derive : Base{}

class
DDerive : Derive{}

public
class TestMain
{
    [STAThread]
    static void Main(string[] args)
    {
        DDerive dd = new DDerive();
        TestMothed(dd, dd);
    }
    static void TestMothed(Base b, Derive d)
    {
        Console.WriteLine("Base, Derive");
    }
    static void TestMothed(Derive d, Base b)
    {
        Console.WriteLine("Derive, Base");
    }
}

这段代码同样不能编译,因为两个TestMothed重载方法的形参离实参都是一近一远。

从结果我们可以发现,如果一次调用使用的参数有多个重载方法可以适合时,编译器将根据以下原则选择重载方法:

1、优先选择形参与实参类型严格匹配的重载方法,而实参所引用的对象类型不以考虑;
2、当有多个重载方法的形参都是实参的基类(接口)时,则选择形参是离实参最近的基类(接口)的重载方法。
3、当有多个重载方法的形参都是实参的基类(接口)时,不能同时存在两个形参是离实参最近的基类(接口)的重载方法,否则不能编译。
4、若存在两个拥有两个以上形参的重载方法,它们有两个形参分别离实参一近一远,则不能编译。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值