【问题标题】:What does the "base" syntax mean?“基本”语法是什么意思?
【发布时间】:2012-05-17 05:03:28
【问题描述】:

有人能告诉我下面的语法是什么意思吗?

public ScopeCanvas(Context context, IAttributeSet attrs) : base(context, attrs)
{
}

我的意思是method(argument) : base(argument) {} ??

P.S 这是一个类的构造函数。

【问题讨论】:

  • 这是一个构造函数,而不是一个方法。
  • 我认为,如果一个问题在不到两分钟的时间内有超过 5 个答案,就不应该被问到。 @Sean87 你可以很容易地用谷歌搜索“C# 基本关键字”
  • Google would give you much faster answer 然后是 StackOverflow。 (@Yorye Nathan - 正是我的观点。)
  • 我知道这是旧的,但我用谷歌搜索'C# :base keyword' 并来到这里......当然 MSDN 链接是第一个,但 SO 答案通常更简洁。

标签: c# syntax


【解决方案1】:

: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 语法允许链接到具有指定参数列表的相同类型的构造函数

【讨论】:

  • “如果省略,编译器将静默尝试绑定到接受 0 个参数的基类构造函数。” 这是解释中最关键的部分 - 谢谢。跨度>
【解决方案2】:

你的类可能是这样定义的:

MyClass : BaseClass

它派生自其他类。构造函数上的: base(...) 在运行派生类构造函数中的代码之前调用基类中适当的构造函数。

Here 是一个相关问题。

编辑

正如 Tilak 所述,MSDN documentation on the base keyword 提供了很好的解释。

【讨论】:

    【解决方案3】:

    调用基类的命名构造函数。如果未指定 base(argument),则调用无参数构造函数

    What really is the purpose of "base" keyword in c#?

    Base keyword

    【讨论】:

      【解决方案4】:

      它从传递参数contextattrs 的基类调用构造函数

      【讨论】:

        【解决方案5】:

        这是一个抽象的重载类构造函数,它允许初始化派生类和基类的参数,并指定是否使用重载构造函数。 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)
            }
        }
        

        【讨论】:

          【解决方案6】:

          您的类继承自基类,当您初始化 ScopeCanvas 类型的对象时,会使用 (context, attrs) 的参数列表调用基构造函数

          【讨论】:

            【解决方案7】:

            这意味着这个构造函数接受两个参数,并将它们传递给继承对象的构造函数。下面的例子只有一个参数。

            Public class BaseType
            {
                public BaseType(object something)
                {
            
                }
            }
            
            public class MyType : BaseType
            {
                 public MyType(object context) : base(context)
                 {
            
                 } 
            }
            

            【讨论】:

              【解决方案8】:

              在上面的例子中,所有人都在谈论 :base 没有人在谈论 base. 是的,base 用于访问父级的成员,但不仅限于构造函数,我们可以直接使用 base._parentVariable 或 base._parentMethod()。

              base. example

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-11-02
                • 2018-05-12
                • 2014-04-21
                • 2021-03-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多