gpt4 book ai didi

c# - 显示不同对象的数组c#(类继承)

转载 作者:行者123 更新时间:2023-11-30 20:28:43 27 4
gpt4 key购买 nike

我的代码有问题。我是继承的新手(到目前为止只有 1 节课),非常感谢这里的任何帮助。我遇到了这个问题,并认为我已经快完成了。创建一个 Book 类,添加两个子类。全部显示。加上其他东西,但这是核心问题。

当我尝试在 for 循环中显示每个对象及其信息时,我不知道如何访问教科书的 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))-Branch 中转换为特定类型的 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 ,但我不想打扰你的老师。

关于c# - 显示不同对象的数组c#(类继承),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928719/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com