Skip to main content
 首页 » 编程设计

c#之为什么要跳过覆盖访问器

2024年02月13日10虾米姐

我正在工作,但自学 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) 
        { 
        } 
    } 
} 
} 

请您参考如下方法:

new 关键字隐藏了原来的属性。因为您的 Book 类有一个 protected Price 属性,而 TextBook 类有一个 public 属性,编译器将它们识别为 2 个不同的属性,TextBook 类中的一个是一个全新且不相关 的属性。当访问 Price 时,您仍然会得到基类 (Book) 的价格

尝试使用 virtual 和 override 来代替,并保持访问修饰符一致(它们应该都是公开的)

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在另一个问题上。具体来说,答案中的这张图: