C#-同时继承多个类

面试题:

using UnityEngine;

public class Test : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        IMyInterface my1 = new MyClass1();
        IMyInterface my2 = new MyClass2();
        IMyInterface my3 = new MyClass3();
        IMyInterface my4 = new MyClass4();

        my1.Write();
        my2.Write();
        MyClass2 myClass2 = new MyClass2();
        myClass2.Write();
        my3.Write();
        my4.Write();
        MyClass4 myClass4 = new MyClass4();
        MyClass1 myClass5 = new MyClass4();
        myClass4.Write();
        myClass5.Write();
    }
}


interface IMyInterface
{
    void Write();
}

public class MyClass1 : IMyInterface
{
    public new void Write()
    {
        Debug.Log("1");
    }
}

public class MyClass2 : MyClass1
{
    public new void Write()
    {
        Debug.Log("2");
    }
}

public class MyClass3 : MyClass2, IMyInterface
{
    public new void Write()
    {
        Debug.Log("3");
    }
}

public class MyClass4 : MyClass3, IMyInterface
{
    void IMyInterface.Write()
    {
        Debug.Log("4");
    }

    public new void Write()
    {
        Debug.Log("5");
    }
}

输出结果依次是:1,1,2,3,4,5,1


 

你可能感兴趣的:(Unity_Code)