无法使用抽象get定义集合

时间:2016-06-30 13:48:59

标签: c#

我有一个继承访问器的问题,我无法定义set方法。

我的代码:

public abstract class MotherOfDragons
{
    public abstract String DragonsName { get; }
}

继承的类:

public class Drogon : MotherOfDragons
{
    public override String DragonsName { get; set; }
}

public class Viserion : MotherOfDragons
{
    public override String DragonsName { get; }
}

ViserionDrogon CS0546我的错误MotherOfDragons

  

'存取' :无法覆盖,因为'属性'没有可覆盖的集合访问者

如果没有在using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApplication58 { class MultiThreading { static void Main(string[] args) { MultiThreading mul = new MultiThreading(); Thread t1 = new Thread(new ThreadStart(mul.WriteX)); Thread t2 = new Thread(new ThreadStart(mul.WriteO)); t1.Priority = ThreadPriority.Lowest; t2.Priority = ThreadPriority.Highest; t1.Start(); t2.Start(); } private void WriteX() { for (int i = 0; i < 300; i++) { Console.Write("X"); } } private void WriteO() { for (int i = 0; i < 300; i++) { Console.Write("O"); } } } } 类中添加set访问器,可以解决此错误吗?我想保持这个字段只读一个案例。

谢谢

1 个答案:

答案 0 :(得分:0)

谢谢@sircodesalot

我终于做到了:

public class Drogon : MotherOfDragons
{
    private String dragonsName;
    public override String DragonsName { get { return dragonsName; } }

    public void Change_DragonsName(String name)
    {
        dragonsName = name;
    }
}

它有效,不像set,但它有效。