Can somone please tell me what does the syntax below means?
public ScopeCanvas(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
I mean what is method(argument) : base(argument) {}
??
PS This is a constructor of a class.
The :base<\/code> syntax is a way for a derived type to chain to a constructor on the base class which accepts the specified argument.
If omitted the compiler will silently attempt to bind to a base class constructor which accepts 0 arguments.
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
}
}
Your class is likely defined like this:
MyClass : BaseClass
It derives from some other class. : base(...)
on your constructor calls the appropriate constructor in the base class before running the code in your derived class's constructor.
Here 'sa related question.
EDIT
As noted by Tilak , the MSDN documentation on the base keyword provides a good explanation.
to call named constructor of base class. if base( argument ) is not specified, parameterless constructor is called
它从传递参数context
和attrs
的基类调用构造函数
This is an abstract overloaded class constructor which allows for initilizing of arguments for the derived and the base class and specifying if an overloaded constructor is to be used. LINK
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)
}
}
您的类继承自基类,并且当您初始化 ScopeCanvas 类型的对象时,使用 (context, attrs) 的参数列表调用基构造函数
This means that this constructor takes two arguments, and passes them to the inherited objects constructor. An example below with only one argument.
Public class BaseType
{
public BaseType(object something)
{
}
}
public class MyType : BaseType
{
public MyType(object context) : base(context)
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.