C# 如何显示列表<;对象>;控制台C中的项目#
有人能帮我吗。。。 我试图在控制台上显示列表中的项目 当前输出显示了我的两个子类:杂货和新鲜农产品。 我还添加了一个显示以下当前结果的图像 非常感谢您的帮助C# 如何显示列表<;对象>;控制台C中的项目#,c#,list,object,console,C#,List,Object,Console,有人能帮我吗。。。 我试图在控制台上显示列表中的项目 当前输出显示了我的两个子类:杂货和新鲜农产品。 我还添加了一个显示以下当前结果的图像 非常感谢您的帮助 namespace ShoppingList { class ShoppingList { static void Main(string[] args) { Grocery myGrocery = new Grocery("Bread", 1);
namespace ShoppingList
{
class ShoppingList
{
static void Main(string[] args)
{
Grocery myGrocery = new Grocery("Bread", 1);
FreshProduce myFreshProduce = new FreshProduce("Orange", 1);
List<object> myShoppingList = new List<object>();
myShoppingList.Add(myGrocery);
myShoppingList.Add(myFreshProduce);
PrintValues(myShoppingList, '\t');
}
public static void PrintValues(IEnumerable myList, char mySeparator)
{
foreach (Object obj in myList)
Console.Write("{0}{1}", mySeparator, obj);
Console.WriteLine();
}
public abstract class Product
{
protected string Name;
protected int Quantity;
}
public class Grocery : Product
{
public Grocery(string groceryName, int groceryQuantity)
{
Name = groceryName;
Quantity = groceryQuantity;
}
}
public class FreshProduce : Product
{
public FreshProduce(string freshProduceName, int freshProduceQuantity)
{
Name = freshProduceName;
Quantity = freshProduceQuantity;
}
}
}
}
namespace ShoppingList
{
班级购物清单
{
静态void Main(字符串[]参数)
{
杂货店我的杂货店=新杂货店(“面包”,1);
FreshProduct MyFreshProduct=新的FreshProduct(“橙色”,1);
List myShoppingList=新列表();
myShoppingList.Add(我的杂货店);
myShoppingList.Add(myfreshproduct);
打印值(myShoppingList,'\t');
}
公共静态void打印值(IEnumerable myList、char mySeparator)
{
foreach(myList中的对象obj)
Write(“{0}{1}”,mySeparator,obj);
Console.WriteLine();
}
公共抽象类产品
{
受保护的字符串名称;
保护整数数量;
}
公共类杂货店:产品
{
公共杂货店(字符串杂货店名称,内部杂货店数量)
{
名称=杂货店名称;
数量=杂货店数量;
}
}
公共类新鲜农产品:产品
{
公共FreshProduct(字符串freshProduceName,int freshProduceQuantity)
{
名称=freshProduceName;
数量=新鲜产品数量;
}
}
}
}
如果需要打印
产品的详细信息,应覆盖ToString()
方法。您可以使用下面的Product
类的实现来实现这一点
public abstract class Product {
protected string Name;
protected int Quantity;
public override string ToString() {
return $"Name = {Name}, Quantity = {Quantity}";
}
}
在我进行这项工作时,您还可以对代码进行一些其他小的改进。请参阅下面的完整代码
namespace ShoppingList {
class ShoppingList {
static void Main(string[] args) {
Grocery myGrocery = new Grocery("Bread", 1);
FreshProduce myFreshProduce = new FreshProduce("Orange", 1);
List<Product> myShoppingList = new List<Product>();
myShoppingList.Add(myGrocery);
myShoppingList.Add(myFreshProduce);
PrintValues(myShoppingList, "\t");
}
// instead of IEnumerable, you should use IEnumerable<Product> for better type checking
public static void PrintValues(IEnumerable<Product> myList, string mySeparator) {
// string.Join does exactly what you are trying to do using a loop
Console.WriteLine(string.Join(mySeparator, myList));
}
public abstract class Product {
protected string Name;
protected int Quantity;
public override string ToString() {
return $"Name = {Name}, Quantity = {Quantity}";
}
}
public class Grocery : Product {
public Grocery(string groceryName, int groceryQuantity) {
Name = groceryName;
Quantity = groceryQuantity;
}
}
public class FreshProduce : Product {
public FreshProduce(string freshProduceName, int freshProduceQuantity) {
Name = freshProduceName;
Quantity = freshProduceQuantity;
}
}
}
}
namespace ShoppingList{
班级购物清单{
静态void Main(字符串[]参数){
杂货店我的杂货店=新杂货店(“面包”,1);
FreshProduct MyFreshProduct=新的FreshProduct(“橙色”,1);
List myShoppingList=新列表();
myShoppingList.Add(我的杂货店);
myShoppingList.Add(myfreshproduct);
打印值(myShoppingList,“\t”);
}
//为了更好地进行类型检查,应该使用IEnumerable而不是IEnumerable
公共静态void打印值(IEnumerable myList、字符串mySeparator){
//Join正是使用循环尝试执行的操作
Console.WriteLine(string.Join(mySeparator,myList));
}
公共抽象类产品{
受保护的字符串名称;
保护整数数量;
公共重写字符串ToString(){
返回$“Name={Name},Quantity={Quantity}”;
}
}
公共类杂货店:产品{
公共杂货店(字符串杂货店名称,内部杂货店数量){
名称=杂货店名称;
数量=杂货店数量;
}
}
公共类新鲜农产品:产品{
公共FreshProduct(字符串freshProduceName,int freshProduceQuantity){
名称=freshProduceName;
数量=新鲜产品数量;
}
}
}
}
不一定要解决这个问题,但在我看来,拥有一个普通的食品类或杂货店
和新鲜食品
都源自并做列表
,而不是做列表
使用string.Join()方法更有意义:Console.WriteLine(string.Join(“,”,myList.Select(x=>x.Name+”:“+x.Quantity.ToString());我收到一些错误:PrintValues(myShoppingList,“\t”)上的参数无效;Console.WriteLine(string.Join(mySeparator,myList))上的参数无效;我也试着打印这部分:杂货店我的杂货店=新杂货店(“面包”,1);FreshProduct MyFreshProduct=新的FreshProduct(“橙色”,1);将其添加到我的列表myShoppingList=new list();myShoppingList.Add(我的杂货店);myShoppingList.Add(myfreshproduct);这不是问题所在,你使用的是什么版本的VS?@user3775220完整的代码逐字复制在VS2017上运行良好