为什么跳过覆盖访问器?

时间:2014-05-04 00:26:22

标签: c# inheritance override

我正在工作,虽然我自己学习C#(不是作业)。不知道为什么我的TextBook和CoffeeTableBook子类中跳过了我的“公共新双倍价格”。我认为这是由于我的构造函数,因为这是在退出类之前执行的最后一行代码。尚未学到任何过于花哨的东西,寻求简洁。谢谢。

namespace BookDemo
{
class Program
{
    static void Main()
    {
        Book book1 = new Book (123, "BOOK: The Blue Whale", "Liam Smith", 15.99);
        Book book2 = new Book(456, "BOOK: The Blue Whale 2", "Liam Smith", 35.00);
        TextBook book3 = new TextBook(789, "TEXTBOOK: Math 101", "Bob Taylor", 1000.00, 10);
        CoffeeTableBook book4 = new CoffeeTableBook(789, "TEXTBOOK: Math 101", "Molly Burns", 0.10);

        Console.WriteLine(book1.ToString());
        Console.WriteLine(book2.ToString());
        Console.WriteLine(book3.ToString());
        Console.WriteLine(book4.ToString());
        Console.ReadLine();
    }

    class Book
    {
        private int Isbn { get; set; }
        private string Title  { get; set; }
        private string Author { get; set; }
        protected double Price { get; set; }

         //Book Constructor
        public Book(int isbn, string title, string author, double price)
        {
            Isbn = isbn;
            Title = title;
            Author = author;
            Price = price;
        }

        public override string ToString()
        {
            return("\n" + GetType() + "\nISBN: " + Isbn + "\nTitle: " + Title + "\nAuthor: " + Author + "\nPrice: " + Price.ToString("C2"));
        }
    }

    class TextBook : Book
    {
        private const int MIN = 20;
        private const int MAX = 80;
        private int GradeLevel { get; set; }

        //TextBook Constructor
        public TextBook(int isbn, string title, string author, double price, int grade) : base (isbn, title, author, price)
        {
            GradeLevel = grade;
        }

        public new double Price
        {
            set
            {
                if (value <= MIN)
                    Price = MIN;
                if (value >= MAX)
                    Price = MAX;
                else
                    Price = value;
            }
        }    
    }

    class CoffeeTableBook : Book
    {
        const int MIN = 35;
        const int MAX = 100;

        public new double Price  // min 35, max 100
        {                
            set
            {
                if (value <= MIN)
                    Price = MIN;
                if (value >= MAX)
                    Price = MAX;
                else
                    Price = value;
            }
        }

        //CoffeeTable Book Constructor
        public CoffeeTableBook(int isbn, string title, string author, double price) : base (isbn, title, author, price)
        {
        }
    }
}
}

1 个答案:

答案 0 :(得分:4)

new关键字隐藏了原始属性。由于您的Book类具有受保护的 Price属性,并且TextBook类具有 public 属性,因此编译器正在识别它们作为2个不同的属性,TextBook类中的属性为全新和无关属性。访问Price时,您仍然会收到基类(Book)的价格

尝试使用virtualoverride代替,同时保持访问修饰符的一致性(它们两者都是公开的)

class Book
{
    ...
    public virtual double Price { get { return price; } set { price = value; } } //Property
    protected double price; //Backing Field
    ...
}
class TextBook : Book
{
    ...
    public override double Price
    {
        set
        {
            if (value <= MIN)
                price = MIN;
            if (value >= MAX)
                price = MAX;
            else
                price = value;
        }
    }    
    ...
}

有关其他问题的详情,请参阅Override/Virtual vs New。特别是从它,答案中的这个图:

https://farm4.static.flickr.com/3291/2906020424_f11f257afa.jpg?v=0