一个case
子类和父类有同名的成员变量,最终输出父类的name kaka
using UnityEngine;
namespace Tests.mapilot
{
public abstract class Father
{
public string name;
public Father(string name)
{
this.name = name;
Debug.Log("father:" + this.name);
}
}
public class Children : Father
{
public string name;
public Children(string name) : base(name)
{
this.name = "chilid-" + name;
Debug.Log("chilid:" + this.name);
}
}
public class UnitTest
{
public static void Start()
{
Father c = new Children("kaka");
test(c);
}
public static void test(Father f)
{
Debug.Log(f.name);
}
}
}
子类不写同名字段name,输出子类的name chile-kaka
using UnityEngine;
namespace Tests.mapilot
{
public abstract class Father
{
public string name;
public Father(string name)
{
this.name = name;
Debug.Log("father:" + this.name);
}
}
public class Children : Father
{
public Children(string name) : base(name)
{
this.name = "chilid-" + name;
Debug.Log("chilid:" + this.name);
}
}
public class UnitTest
{
public static void Start()
{
Father c = new Children("kaka");
test(c);
}
public static void test(Father f)
{
Debug.Log(f.name);
}
}
}