如何创建可以存储多个 inherited/derived 对象的动态对象列表
How can I create a dynamic List of objects that can store multiple inherited/derived objects
正在尝试为销售人员创建一个可以增加到任意规模的客户列表。我是 C# 中的 OOP 新手,我不确定我在这里做错了什么。是不是因为我试图将派生对象添加到我的基础列表 class?如果是这样,替代方案是什么?
class Program
{
List<Customer> customers = new List<Customer>();
static void Main(string[] args)
{
GetCustomerInfo();
}
static void GetCustomerInfo()
{
int prod1, prod2;
Console.WriteLine("Please enter the following info.");
Console.WriteLine("Type '1' for pickup, '2' for delivery: ");
string option = Console.ReadLine();
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("10 digit Phone Number: ");
string number = Console.ReadLine();
Console.Write("Quantity of product 1: ");
bool success = false;
while (!success)
{
success = int.TryParse(Console.ReadLine(), out prod1);
}
bool success2 = false;
while (!success2)
{
success2 = int.TryParse(Console.ReadLine(), out prod2);
}
if(option == "1")
{
Console.WriteLine("Address: ");
string address = Console.ReadLine();
customers.Add(new DeliveryCustomer() { Name = name, PhoneNumber = number, Address = address });
}
else if (option == "2")
{
Console.WriteLine("Pick up time: ");
string time = Console.ReadLine();
customers.Add(new PickUpCustomer() { Name = name, PhoneNumber = number, PickUpTime = time });
}
customers.Add(new PickUpCustomer() { Name = name, PhoneNumber = number, PickUpTime = time });
出现以下错误。非静态字段、方法或 属性 'member
需要对象引用
}
}
class Customer
{
static int productOnePrice = 5;
static int productTwoPrice = 10;
private string name;
private string phoneNumber;
private int quantityOfProductOne;
private int quantityOfProductTwo;
public Customer()
{
name = "";
phoneNumber = "";
quantityOfProductOne = 0;
quantityOfProductTwo = 0;
}
public string Name
{
get => name;
set => name = value;
}
public string PhoneNumber
{
get => phoneNumber;
set
{
if(value.Length != 10)
{
Console.WriteLine("Invalid number. Must be 10 digits.");
}
else
{
phoneNumber = value;
}
}
}
public int QuantityOfProductOne
{
get => quantityOfProductOne;
set => quantityOfProductOne = value;
}
public int QuantityOfProductTwo
{
get => quantityOfProductTwo;
set => quantityOfProductTwo = value;
}
}
class DeliveryCustomer : Customer
{
private string address;
public string Address
{
get => address;
set => address = value;
}
public DeliveryCustomer()
{
address = "";
}
}
class PickUpCustomer : Customer
{
private string pickUpTime;
public string PickUpTime
{
get => pickUpTime;
set => pickUpTime = value;
}
public PickUpCustomer()
{
pickUpTime = "";
}
}
如果这是一个愚蠢的问题,我深表歉意,我只是无法理解一些 OOP 概念。
GetCustomerInfo()
是一个静态方法,而List<Customer> customers
是一个实例字段,你可能无法从静态方法访问实例字段,所以希望让客户成为静态字段应该解决问题
正在尝试为销售人员创建一个可以增加到任意规模的客户列表。我是 C# 中的 OOP 新手,我不确定我在这里做错了什么。是不是因为我试图将派生对象添加到我的基础列表 class?如果是这样,替代方案是什么?
class Program
{
List<Customer> customers = new List<Customer>();
static void Main(string[] args)
{
GetCustomerInfo();
}
static void GetCustomerInfo()
{
int prod1, prod2;
Console.WriteLine("Please enter the following info.");
Console.WriteLine("Type '1' for pickup, '2' for delivery: ");
string option = Console.ReadLine();
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("10 digit Phone Number: ");
string number = Console.ReadLine();
Console.Write("Quantity of product 1: ");
bool success = false;
while (!success)
{
success = int.TryParse(Console.ReadLine(), out prod1);
}
bool success2 = false;
while (!success2)
{
success2 = int.TryParse(Console.ReadLine(), out prod2);
}
if(option == "1")
{
Console.WriteLine("Address: ");
string address = Console.ReadLine();
customers.Add(new DeliveryCustomer() { Name = name, PhoneNumber = number, Address = address });
}
else if (option == "2")
{
Console.WriteLine("Pick up time: ");
string time = Console.ReadLine();
customers.Add(new PickUpCustomer() { Name = name, PhoneNumber = number, PickUpTime = time });
}
customers.Add(new PickUpCustomer() { Name = name, PhoneNumber = number, PickUpTime = time });
出现以下错误。非静态字段、方法或 属性 'member
需要对象引用 }
}
class Customer
{
static int productOnePrice = 5;
static int productTwoPrice = 10;
private string name;
private string phoneNumber;
private int quantityOfProductOne;
private int quantityOfProductTwo;
public Customer()
{
name = "";
phoneNumber = "";
quantityOfProductOne = 0;
quantityOfProductTwo = 0;
}
public string Name
{
get => name;
set => name = value;
}
public string PhoneNumber
{
get => phoneNumber;
set
{
if(value.Length != 10)
{
Console.WriteLine("Invalid number. Must be 10 digits.");
}
else
{
phoneNumber = value;
}
}
}
public int QuantityOfProductOne
{
get => quantityOfProductOne;
set => quantityOfProductOne = value;
}
public int QuantityOfProductTwo
{
get => quantityOfProductTwo;
set => quantityOfProductTwo = value;
}
}
class DeliveryCustomer : Customer
{
private string address;
public string Address
{
get => address;
set => address = value;
}
public DeliveryCustomer()
{
address = "";
}
}
class PickUpCustomer : Customer
{
private string pickUpTime;
public string PickUpTime
{
get => pickUpTime;
set => pickUpTime = value;
}
public PickUpCustomer()
{
pickUpTime = "";
}
}
如果这是一个愚蠢的问题,我深表歉意,我只是无法理解一些 OOP 概念。
GetCustomerInfo()
是一个静态方法,而List<Customer> customers
是一个实例字段,你可能无法从静态方法访问实例字段,所以希望让客户成为静态字段应该解决问题