unity中子类怎么访问父类
在Unity中,子类可以通过关键字base来访问父类的成员。使用base关键字可以调用父类中的构造函数、方法和属性。
例如,假设有一个父类ParentClass,其中有一个公共方法ParentMethod():
public class ParentClass
{
public void ParentMethod()
{
Debug.Log("This is parent method.");
}
}
现在有一个子类ChildClass,想要调用父类中的ParentMethod()方法,可以使用base关键字:
public class ChildClass : ParentClass
{
public void ChildMethod()
{
base.ParentMethod(); // 调用父类方法
Debug.Log("This is child method.");
}
}
在子类ChildClass中,ChildMethod()方法通过base关键字调用了父类中的ParentMethod()方法。这样,在调用ChildMethod()方法时,会先输出父类中的信息,再输出子类中的信息。
原文地址: https://www.cveoy.top/t/topic/bf0C 著作权归作者所有。请勿转载和采集!