我的代码有问题。我是继承的新手(到目前为止仅进行过1次讲座),在此对您的帮助将不胜感激。我已经遇到了这个问题,认为我已经完成了。制作一个Book类,添加两个子类。显示全部。加上其他东西,但这是核心问题。

当我尝试在for循环中显示每个对象及其信息时,我无法弄清楚如何访问TextBook的BookGrade。我做了一系列的书,把所有东西都放在那里。我知道这是不对的,但我不知道从这里去哪里。我试图制作这样的教科书数组

TextBook[] bookArray = new TextBook[3] { Book1, TBook1, CBook1};


但是然后我需要向Book1添加一个强制转换,它可以正常工作,但是我仍然不能将类型'CoffeeTableBook'隐式转换为'TextBook'。我明白了,但不知道如何解决。我收到此错误““ Book”不包含“ BookGrade”的定义。”

我有3个课,即Book,TextBook和CoffeeTableBook。 TextBook和CoffeeTableBook类均从Book类派生。

在此先感谢您抽出宝贵时间来帮助我。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookDemo
{
class Program
{
    static void Main(string[] args)
    {
        //Book
        Book Book1 = new Book();
        Book1.BookNum = 123456;
        Book1.BookTitle = "Harry Potter 1";
        Book1.BookAuthor = "JK Rowling";
        Book1.BookPrice = 5.95;

        //Text Book
        TextBook TBook1 = new TextBook();
        TBook1.BookNum = 123436;
        TBook1.BookTitle = "Harry Potter 2";
        TBook1.BookAuthor = "JK Rowling";
        TBook1.BookPrice = 5.95;
        TBook1.BookGrade = "A";


        //Coffee Table Book
        CoffeeTableBook CBook1 = new CoffeeTableBook();
        CBook1.BookNum = 123136;
        CBook1.BookTitle = "Harry Potter 7";
        CBook1.BookAuthor = "JK Rowling";
        CBook1.BookPrice = 95.95;

        Book[] bookArray = new Book[3] { Book1, TBook1, CBook1 };


        for (int i = 0; i < bookArray.Length; i++)
        {
            if (bookArray[i].GetType() == typeof(TextBook))
            {
                Console.WriteLine("{0} {1} {2} {3} {4}", bookArray[i].BookNum, bookArray[i].BookTitle, bookArray[i].BookAuthor, bookArray[i].BookPrice, bookArray[i].BookGrade);//'Book' does not contain a defination for 'BookGrade'..
            }
            else
            {
                Console.WriteLine("{0} {1} {2} {3} {4}", bookArray[i].BookNum, bookArray[i].BookTitle, bookArray[i].BookAuthor, bookArray[i].BookPrice, "N/A");
            }
        }
    }

}


}

class Book
{
public int BookNum { get; set; }
public string BookTitle { get; set; }
public string BookAuthor { get; set; }
public double BookPrice { get; set; }

}

class TextBook : Book // must be priced between $20.00 and $80.00
{
const double MIN_PRICE = 20;
const double MAX_PRICE = 80;
public string BookGrade { get; set; }

new public double BookPrice
{
    set
    {
        if (value < MIN_PRICE)
        {
            base.BookPrice = MIN_PRICE;
        }
        else if (value > MAX_PRICE)
        {
            base.BookPrice = MAX_PRICE;
        }
        else
        {
            base.BookPrice = value;
        }
    }
    get
    {
        return base.BookPrice;
    }
}
}

class CoffeeTableBook : Book //must be priced between $35.00 and $100.00
{
const double MIN_PRICE = 35;
const double MAX_PRICE = 100;
new public double BookPrice
{
    set
    {
        if (value < MIN_PRICE)
        {
            base.BookPrice = MIN_PRICE;
        }
        else if (value > MAX_PRICE)
        {
            base.BookPrice = MAX_PRICE;
        }
        else
        {
            base.BookPrice = value;
        }
    }
    get
    {
        return base.BookPrice;
    }
}
}

最佳答案

您需要在if(bookArray [i] .GetType()== typeof(TextBook))-分支中强制转换为特定类型的Textbook

例如。:

if (bookArray[i].GetType() == typeof(TextBook))
{
    Console.WriteLine("{0} {1} {2} {3} {4}", bookArray[i].BookNum, bookArray[i].BookTitle, bookArray[i].BookAuthor, bookArray[i].BookPrice, ((TextBook)bookArray[i]).BookGrade); // Works now, because we view bookArray[i] as TextBook and not it's underlaying type
}
else
{
    Console.WriteLine("{0} {1} {2} {3} {4}", bookArray[i].BookNum, bookArray[i].BookTitle, bookArray[i].BookAuthor, bookArray[i].BookPrice, "N/A");
}


我通常建议使用here中所述的新开关功能(模式),但我不想干扰您的老师。

10-06 01:51