C# 在c中重写并添加新的类成员#

C# 在c中重写并添加新的类成员#,c#,overriding,C#,Overriding,如何这样做(不创建中间类): 编译器说:类型“B”已包含“文本”的定义 说明:如何操作(但不包括“中级”课程): 在同一个类中定义属性文本两次。您正在覆盖它并使用关键字new。删除文本的第二个副本 class B : A { private string text; public override string Text{ get; set;} } 在同一个类中定义属性文本两次。您正在覆盖它并使用关键字new。删除文本的第二个副本 class B : A {

如何这样做(不创建中间类):

编译器说:类型“B”已包含“文本”的定义

说明:如何操作(但不包括“中级”课程):


在同一个类中定义属性文本两次。您正在覆盖它并使用关键字new。删除文本的第二个副本

class B : A
{
    private string text;
    public override string Text{ get; set;}      

}

在同一个类中定义属性文本两次。您正在覆盖它并使用关键字new。删除文本的第二个副本

class B : A
{
    private string text;
    public override string Text{ get; set;}      

}

如果希望在派生类中对属性进行读写,那么这是不可能的。
属性是
PropertyType get\u PropertyName()
(当属性可读时)和
void set\u PropertyName(PropertyType值)
(当属性可写时)方法的语法糖。这一行:

public abstract string Text { get; }
指:

public abstract string get_Text();
public override string get_Text()
{
   // ...
}

public override void set_Text(string value)
{
   // ...
}
这是:

public override string Text{ get; set;}
指:

public abstract string get_Text();
public override string get_Text()
{
   // ...
}

public override void set_Text(string value)
{
   // ...
}

由于基类中没有抽象的
set\u Text
方法,所以不能重写它。

如果希望在派生类中读写该属性,那么这是不可能的。
属性是
PropertyType get\u PropertyName()
(当属性可读时)和
void set\u PropertyName(PropertyType值)
(当属性可写时)方法的语法糖。这一行:

public abstract string Text { get; }
指:

public abstract string get_Text();
public override string get_Text()
{
   // ...
}

public override void set_Text(string value)
{
   // ...
}
这是:

public override string Text{ get; set;}
指:

public abstract string get_Text();
public override string get_Text()
{
   // ...
}

public override void set_Text(string value)
{
   // ...
}

由于基类中没有抽象的
set\u Text
方法,因此您不能重写它。

您可以通过一个接口来实现这一点:

interface A
{
    // The interface requires that Text is *at least* read only
    string Text { get; }
}

class B : A
{
    string text = "";

    // Implement Text as read-write
    public string Text
    {
        get { return text; }
        set { text = value; }
    }
}
如果这对您不起作用,您可以简单地添加一个读写的
TextEx
属性:

public string TextEx
{
    get { return text; }
    set { text = value;
}

public string Text
{
    get { return text; }
}

您可以通过一个界面来实现这一点:

interface A
{
    // The interface requires that Text is *at least* read only
    string Text { get; }
}

class B : A
{
    string text = "";

    // Implement Text as read-write
    public string Text
    {
        get { return text; }
        set { text = value; }
    }
}
如果这对您不起作用,您可以简单地添加一个读写的
TextEx
属性:

public string TextEx
{
    get { return text; }
    set { text = value;
}

public string Text
{
    get { return text; }
}

为什么要这样做?我想要A(和子类)中的只读属性和B(和子类)中的读/写属性。为什么要这样做?我想要A(和子类)中的只读属性和B(和子类)中的读/写属性编译器说:无法重写,因为“A.Text”没有可重写集访问权限。编译器说:无法重写,因为“A.Text”没有可重写集访问权限。另一个问题-如何在一个类中重写和添加具有相同名称(先隐藏)的新成员。它可能在两种情况下发生classes@Michael:是否可以修改A?是的,您可以修改A,但希望A。文本为abstract@Michael:然后将其设置为
公共抽象字符串文本{get;set;}
。这个属性仍然是抽象的,但它也是可写的。我理解。另一个问题-如何在一个类中重写和添加具有相同名称(先隐藏)的新成员。它可能在两种情况下发生classes@Michael:是否可以修改A?是的,您可以修改A,但希望A。文本为abstract@Michael:然后将其设置为
公共抽象字符串文本{get;set;}
。该属性仍将是抽象的,但也将是可写的。