【问题标题】:C#: What does 'owner' mean when used as argument of a class?C#:当用作类的参数时,“所有者”是什么意思?
【发布时间】:2012-07-30 20:09:24
【问题描述】:

在下面的类中,“所有者”参数对 myClass 和基类有什么作用?

public class Base
{
    public myClass(owner) : base (owner) { }
}

【问题讨论】:

  • 投反对票是非常不必要的!给 op 一个编辑的机会!
  • 这并没有夺走他的机会,并且在问题被改写后,任何投反对票的人都可以收回他们的 -1。到目前为止,我什至不明白。每个人都在猜测 OP 是什么意思,它满足了“不清楚或无用”问题的定义
  • 您的项目(或引用的程序集)中似乎有一个名为 myMethod 的类,并且 myMethod 的一个实例正在作为参数(命名为所有者)传递给这个“新”方法。也许这个(子)类被命名为@new?
  • @arthurmani 是什么让你在你的问题中使用new 作为示例类名?!
  • @Xander 抱歉,我通过 VB.net 的转换(开发人员融合)获得了代码,无法识别语法。感谢 Jason 的回复,我现在明白了。

标签: c# class arguments owner


【解决方案1】:

如果你有两个类,一个是基类,另一个是派生类,当你为派生类创建构造函数时,你可以将参数传递给基类。

public class Base
{
    private string Test = "";

    public Base(string test)
    {
        Test = test;
    }
}

public class Derived : Base
{
    public Derived(string test) : base(test) // - This will call public Base(string test)
    {
    }
}

【讨论】:

  • new 在哪里? public new (myMethod owner) : base (owner) { }
  • new 是保留字,不能在此上下文中使用。我觉得他的代码不太对,看起来像是构造函数。
  • new is a reserved word and cannot be used in this context 谢谢。很高兴知道:)
【解决方案2】:

以下内容将编译并且似乎适合您的场景减去您没有使用 verbatim identifier @ 的事实:

public class Base
{
    public Base(myMethod owner)
    {
    }
}

public class @new : Base
{
    public @new(myMethod owner) : base(owner)
    {
    }
}

前面的示例演示了如何将构造函数参数传递给base 类的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2015-09-13
    • 2017-05-09
    • 2012-04-04
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多