請考慮下面的設計:密封抽象類的屬性
public interface IBook
{
string Author { get; set; }
string Title { get; set; }
}
abstract class BaseBook : IBook
{
private string _title;
public virtual string Author
{
get
{
Console.WriteLine("Base book GET!");
return _title;
}
set
{
Console.WriteLine("Base book SET!");
_title = value;
}
}
public string Title { get; set; }
}
class Book : BaseBook
{
}
class SuperBook : Book
{
public override string Author
{
get
{
Console.WriteLine("SuperBook GET!");
return base.Author;
}
set
{
Console.WriteLine("SuperBook SET!");
base.Author = value;
}
}
public string Title { get; set; }
}
有什麼辦法使密封防止財產在派生類中被重寫的BaseBook
基類的Title
財產(如Book
和SuperBook
類)?
你爲什麼要這樣的事情?有什麼特別的原因讓用戶不應該被允許覆蓋你的財產? – 2011-05-18 13:11:33