有人能告诉我以下语法的含义吗?
public ScopeCanvas(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
我指的是
method(argument) : base(argument){}
是什么??附注:这是一个类的构造函数。
有人能告诉我以下语法的含义吗?
public ScopeCanvas(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
method(argument) : base(argument){}
是什么?? :base
语法是派生类型连接到基类上接受指定参数的构造函数的一种方式。如果省略,则编译器会默默地尝试绑定到一个接受0个参数的基类构造函数。
class Parent {
protected Parent(int id) { }
}
class Child1 : Parent {
internal Child1() {
// Doesn't compile. Parent doesn't have a parameterless constructor and
// hence the implicit :base() won't work
}
}
class Child2 : Parent {
internal Child2() : base(42) {
// Works great
}
}
还有:this
语法,它允许在同一类型的构造函数之间使用指定的参数列表进行链式调用
你的类可能是这样定义的:
MyClass : BaseClass
: base(...)
会在运行派生类构造函数中的代码之前调用基类中适当的构造函数。context
和 attrs
传递进去。这是一个抽象的重载类构造函数,允许为派生类和基类初始化参数,并指定是否使用重载构造函数。
链接public class A
{
public A()
{ }
public A(int size)
{ }
};
class B : public A
{
public B()
{// this calls base class constructor A()
}
public B(int size) : base(size)
{ // this calls the overloaded constructor A(size)
}
}
Public class BaseType
{
public BaseType(object something)
{
}
}
public class MyType : BaseType
{
public MyType(object context) : base(context)
{
}
}