我有一个继承访问器的问题,我无法定义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; }
}
Viserion
但Drogon
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访问器,可以解决此错误吗?我想保持这个字段只读一个案例。
谢谢
答案 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
,但它有效。