C# 如何在方法中引用对象
C# how to refer to objects inside a method
所以我需要在这个 class 中创建一个名为“store”的 class 我需要一个名为“start”的方法,现在在上述方法中我需要从不同的对象创建 9 个对象classes。然后我需要在主程序中调用所述方法,并能够在调用消息后引用所述创建的对象。
class Store
{
public void start()
{
Pizza vesuvio = new Pizza("Vesuvio", 75, "Tomato", "Cheese", "Ham");
Pizza vegetarian = new Pizza("Vegetarian", 80, "Tomato", "cheese", "vegetables");
Pizza contadina = new Pizza("Containda", 75, "Tomato", "cheese", "mushrooms", "olives");
Customer victor = new Customer("Victor", "Hansen");
Customer jacob = new Customer("Jacob", "Pedersen");
Customer maghrete = new Customer("Maghrete", "Ingrid");
Order victorOrdre = new Order(vesuvio.PizzaPrice, 1.25, vesuvio.PizzaPrice * 1.25);
Order jacobOrdre = new Order(vegetarian.PizzaPrice, 1.25, vegetarian.PizzaPrice * 1.25);
Order maghreteOrdre = new Order(contadina.PizzaPrice, 1.25, contadina.PizzaPrice * 1.25);
return;
}
}
这是我创建的方法,但无论我 return 或将类型更改为什么,我似乎都无法在主程序中调用该方法。
这就是我被指示要做的事情
- 要测试您的应用程序,您应该使用 Start 方法创建一个 class 商店。
- 从 class 程序中的 main 方法调用 Start 方法。
- 在开始方法中你应该:
- 创建 3 个 Pizza 对象、3 个 Customer 对象和 3 个 Order 对象,每个对象都有不同的披萨。
- 打印订单信息
- 使用每个订单对象的对象引用,您应该打印出每个订单的披萨名称、顾客姓名和总价。
public class Order
{
// instance fields
private double _tax;
private int _priceBeforeTaxes;
private double _totalPrice;
//properties
public double Tax
{
get { return _tax; }
set { _tax = 0.25; }
}
public int PriceBeforeTaxes
{
get { return _priceBeforeTaxes; }
}
public double TotalPrice
{
get { return _totalPrice; }
}
//constructor
public Order(int priceBeforeTax, double tax, double totalPrice)
{
_priceBeforeTaxes = priceBeforeTax;
_tax = tax;
_totalPrice = totalPrice;
}
//methods
public override string ToString()
{
string obj = $"order total before taxes is {PriceBeforeTaxes} with taxes of 25% is equal to {TotalPrice} ";
return obj;
}
public double CalculateTotalPrice(int PriceBeforeTaxes, double Tax)
{
double CalculatedTotalPrice = PriceBeforeTaxes * Tax;
return CalculatedTotalPrice;
}
}
public class Pizza
{
private string _pizzaName;
public int _pizzaPrice;
private string _pizzaToppings1;
private string _pizzaToppings2;
private string _pizzaToppings3;
private string _pizzaToppings4;
//Properties
public string PizzaName
{
get { return _pizzaName; }
}
public int PizzaPrice
{
get { return _pizzaPrice; }
}
public string PizzaToppings1
{
get { return _pizzaToppings1; }
}
public string PizzaToppings2
{
get { return _pizzaToppings2; }
}
public string PizzaToppings3
{
get { return _pizzaToppings3; }
}
public string PizzaToppings4
{
get { return _pizzaToppings4; }
}
//constructor
public Pizza(string pizzaName, int pizzaPrice, string pizzaToppings1, string pizzaToppings2, string pizzaToppings3)
{
_pizzaName = pizzaName;
_pizzaPrice = pizzaPrice;
_pizzaToppings1 = pizzaToppings1;
_pizzaToppings2 = pizzaToppings2;
_pizzaToppings3 = pizzaToppings3;
}
public Pizza(string pizzaName, int pizzaPrice, string pizzaToppings1, string pizzaToppings2, string pizzaToppings3, string pizzaToppings4)
{
_pizzaName = pizzaName;
_pizzaPrice = pizzaPrice;
_pizzaToppings1 = pizzaToppings1;
_pizzaToppings2 = pizzaToppings2;
_pizzaToppings3 = pizzaToppings3;
_pizzaToppings4 = pizzaToppings4;
}
//method
public override string ToString()
{
string obj = $"{PizzaName} with {PizzaToppings1}, {PizzaToppings2}, {PizzaToppings3}, {PizzaToppings4} costs {PizzaPrice}kr";
return obj;
}
}
public class Customer
{
private string _customerFirstName;
private int _customerNumber;
private string _customerAddress;
private string _customerLastName;
//properties
public string CustomerFirstName
{
get { return _customerFirstName; }
set { _customerFirstName = value; }
}
public string CustomerLastName
{
get { return _customerLastName; }
set { _customerLastName = value; }
}
public int CustomerNumber
{
get { return _customerNumber; }
set { _customerNumber = value; }
}
public string CustomerAddress
{
get { return _customerAddress; }
set { _customerAddress = value; }
}
//constructor
public Customer(string CustomerFirstName, string CustomerLastName, int CustomerNumber, string CustomerAddress)
{
_customerFirstName = CustomerFirstName;
_customerNumber = CustomerNumber;
_customerAddress = CustomerAddress;
_customerLastName = CustomerLastName;
if (CustomerNumber < 100000000 && CustomerNumber > 9999999)
{
}
else
{
Console.WriteLine("this is not a valid phone number, valid phone number must be 8 digits. Please try again.");
}
}
//methods
public override string ToString()
{
string obj = $"{CustomerFirstName}, {CustomerLastName}, \n{CustomerNumber}, \n{CustomerAddress}";
return obj;
}
}
class Program
{
static void Main(string[] args)
{
Store store = new Store();
store.start();
}
}
那是我应该调用该方法并引用我在该方法中创建的对象的地方。
如果您使用 visual studio,则创建 class 非常简单。这篇 MSDN 文章(似乎正是您要查找的内容)更深入地介绍了如何创建和使用 classes。查看示例部分:https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes
下面我更深入地介绍了您的每项要求。
- To test your application you should create a class Store with a method Start.
namespace PizzaStore
{
public class Store
{
public void Start()
{
// Create 3 Pizza objects
// Create 3 Customer objects
// Create 3 Order objects each with a different pizza.
}
}
}
- Call the Start method from the main method in the class Program.
找到名为 Program.cs
的文件,如果您使用的是新的 .NET 6 框架,则此文件看起来会有所不同。否则,如果您使用的是 .Net Framework 4.x,那么它应该看起来像传统的 class。有关详细信息,请参阅此 MSDN 文档:https://docs.microsoft.com/en-gb/dotnet/core/tutorials/top-level-templates
要调用 Start()
函数,您需要创建 Store
对象的新实例,然后通过引用该实例调用该函数。
Store store = new Store();
store.Start();
如果您决定使用此代码,则需要确保在文件顶部包含一个 using 语句。您可以在此处找到有关名称空间的更多信息:https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces
命名空间:
using PizzaStore;
否则您将不得不参考 class 包括命名空间。
PizzaStore.Store store = new PizzaStore.Store();
store.Start();
- and 4. In the Start method you should: Create 3 Pizza objects, 3 Customer objects and 3 Order objects each with a different pizza.
您似乎已经创建了对象。据我所知,您在创建对象方面所做的是正确的。您可以以不同的方式设置变量(例如使用属性:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties),但是您通过在构造函数中初始化值所做的一切都很好。
我建议的唯一改进是删除函数末尾的 return;
。这没有任何意义,可能会造成混淆。
- and 6. Print out order information: Using the object reference to each Order object, you should print out the pizza name, the customer name and the total price for each order.
我假设您使用的是控制台项目。在这种情况下写入控制台很简单。
要写一行,您可以使用:
Console.WriteLine("Hello, World!");
要在不换行的情况下写一些东西,您可以使用:
Console.Write("Hello, World!");
为了引用您创建的每个对象,只需使用您为它们创建的已声明变量名即可,例如:
Console.WriteLine(vesuvio.PizzaName);
您的任务的最终要求将需要您修改当前拥有的代码。创建订单时,您需要输入有助于识别订单的数据。
现在,你有披萨的价格,我假设的是税率和最终总价。
不幸的是,这并不能帮助我们识别订单,因为我们无法弄清楚订购的是什么类型的比萨饼以及谁订购了比萨饼。因此,您需要在订单中添加这两个关键部分的数据。
我建议在 Order
class 中为您创建的对象添加一个 属性 调用:
- 披萨
- 客户
您可以按照创建要传递给构造函数的字符串和数字的相同方式来执行此操作。但是请确保它存储在 class 中,而不仅仅是传递给构造函数。
例如,您的客户 class 看起来像这样:
public class Custumer
{
public Custumer(string name, string surname)
{
CustomerName = name;
CustomerSurname = surname;
}
public string CustomerName { get; set; }
public string CustomerSurname { get; set; }
}
您的订单 class 看起来像这样:
public class Order
{
public Order(double price, Customer customer)
{
OrderPrice = price;
OrderCustomer = customer;
}
public double OrderPrice { get; set; }
public Customer OrderCustomer { get; set; }
}
您可以通过执行以下操作来引用订单客户:
victorOrdre.OrderCustomer.CustomerName;
您似乎已经用代码更新了答案。这对于确定如何帮助您非常有帮助。关于您的更新,很明显您对 classes 和属性有很好的理解。
对于比萨 class,我建议您改用包含一系列比萨配料的列表。这样你就不需要为每个浇头创建一个新变量。
public class Pizza
{
public Pizza(string name, double price, params string[] args)
{
PizzaName = name;
PizzaPrice = price;
PizzaIngredients = new List<string>();
foreach (var item in args)
{
PizzaIngredients.Add(item);
}
}
public string PizzaName { get; set; }
public double PizzaPrice { get; set; }
public List<string> PizzaIngredients { get; set; }
}
根据您发布的说明,在我看来,获得满分所需的全部内容是:
class Program
{
static void Main(string[] args)
{
Store store = new Store();
store.Start();
}
}
public class Store
{
public void Start()
{
Order[] orders = new []
{
new Order(new Customer("Victor Hansen"), new Pizza("Vesuvio", 75m)),
new Order(new Customer("Jacob Pedersen"), new Pizza("Vegetarian", 80m)),
new Order(new Customer("Maghrete Ingrid"), new Pizza("Containda", 75m)),
};
foreach (Order order in orders)
{
Console.WriteLine($"{order.Customer.Name} has ordered {order.Pizza.Name} costing {order.Pizza.Price}.");
}
}
}
public class Order
{
public Customer Customer { get; private set; }
public Pizza Pizza { get; private set; }
public Order(Customer customer, Pizza pizza)
{
this.Customer = customer;
this.Pizza = pizza;
}
}
public class Pizza
{
public string Name { get; private set; }
public decimal Price { get; private set; }
public Pizza(string name, decimal price)
{
this.Name = name;
this.Price = price;
}
}
public class Customer
{
public string Name { get; private set; }
public Customer(string name)
{
this.Name = name;
}
}
所以我需要在这个 class 中创建一个名为“store”的 class 我需要一个名为“start”的方法,现在在上述方法中我需要从不同的对象创建 9 个对象classes。然后我需要在主程序中调用所述方法,并能够在调用消息后引用所述创建的对象。
class Store
{
public void start()
{
Pizza vesuvio = new Pizza("Vesuvio", 75, "Tomato", "Cheese", "Ham");
Pizza vegetarian = new Pizza("Vegetarian", 80, "Tomato", "cheese", "vegetables");
Pizza contadina = new Pizza("Containda", 75, "Tomato", "cheese", "mushrooms", "olives");
Customer victor = new Customer("Victor", "Hansen");
Customer jacob = new Customer("Jacob", "Pedersen");
Customer maghrete = new Customer("Maghrete", "Ingrid");
Order victorOrdre = new Order(vesuvio.PizzaPrice, 1.25, vesuvio.PizzaPrice * 1.25);
Order jacobOrdre = new Order(vegetarian.PizzaPrice, 1.25, vegetarian.PizzaPrice * 1.25);
Order maghreteOrdre = new Order(contadina.PizzaPrice, 1.25, contadina.PizzaPrice * 1.25);
return;
}
}
这是我创建的方法,但无论我 return 或将类型更改为什么,我似乎都无法在主程序中调用该方法。 这就是我被指示要做的事情
- 要测试您的应用程序,您应该使用 Start 方法创建一个 class 商店。
- 从 class 程序中的 main 方法调用 Start 方法。
- 在开始方法中你应该:
- 创建 3 个 Pizza 对象、3 个 Customer 对象和 3 个 Order 对象,每个对象都有不同的披萨。
- 打印订单信息
- 使用每个订单对象的对象引用,您应该打印出每个订单的披萨名称、顾客姓名和总价。
public class Order
{
// instance fields
private double _tax;
private int _priceBeforeTaxes;
private double _totalPrice;
//properties
public double Tax
{
get { return _tax; }
set { _tax = 0.25; }
}
public int PriceBeforeTaxes
{
get { return _priceBeforeTaxes; }
}
public double TotalPrice
{
get { return _totalPrice; }
}
//constructor
public Order(int priceBeforeTax, double tax, double totalPrice)
{
_priceBeforeTaxes = priceBeforeTax;
_tax = tax;
_totalPrice = totalPrice;
}
//methods
public override string ToString()
{
string obj = $"order total before taxes is {PriceBeforeTaxes} with taxes of 25% is equal to {TotalPrice} ";
return obj;
}
public double CalculateTotalPrice(int PriceBeforeTaxes, double Tax)
{
double CalculatedTotalPrice = PriceBeforeTaxes * Tax;
return CalculatedTotalPrice;
}
}
public class Pizza
{
private string _pizzaName;
public int _pizzaPrice;
private string _pizzaToppings1;
private string _pizzaToppings2;
private string _pizzaToppings3;
private string _pizzaToppings4;
//Properties
public string PizzaName
{
get { return _pizzaName; }
}
public int PizzaPrice
{
get { return _pizzaPrice; }
}
public string PizzaToppings1
{
get { return _pizzaToppings1; }
}
public string PizzaToppings2
{
get { return _pizzaToppings2; }
}
public string PizzaToppings3
{
get { return _pizzaToppings3; }
}
public string PizzaToppings4
{
get { return _pizzaToppings4; }
}
//constructor
public Pizza(string pizzaName, int pizzaPrice, string pizzaToppings1, string pizzaToppings2, string pizzaToppings3)
{
_pizzaName = pizzaName;
_pizzaPrice = pizzaPrice;
_pizzaToppings1 = pizzaToppings1;
_pizzaToppings2 = pizzaToppings2;
_pizzaToppings3 = pizzaToppings3;
}
public Pizza(string pizzaName, int pizzaPrice, string pizzaToppings1, string pizzaToppings2, string pizzaToppings3, string pizzaToppings4)
{
_pizzaName = pizzaName;
_pizzaPrice = pizzaPrice;
_pizzaToppings1 = pizzaToppings1;
_pizzaToppings2 = pizzaToppings2;
_pizzaToppings3 = pizzaToppings3;
_pizzaToppings4 = pizzaToppings4;
}
//method
public override string ToString()
{
string obj = $"{PizzaName} with {PizzaToppings1}, {PizzaToppings2}, {PizzaToppings3}, {PizzaToppings4} costs {PizzaPrice}kr";
return obj;
}
}
public class Customer
{
private string _customerFirstName;
private int _customerNumber;
private string _customerAddress;
private string _customerLastName;
//properties
public string CustomerFirstName
{
get { return _customerFirstName; }
set { _customerFirstName = value; }
}
public string CustomerLastName
{
get { return _customerLastName; }
set { _customerLastName = value; }
}
public int CustomerNumber
{
get { return _customerNumber; }
set { _customerNumber = value; }
}
public string CustomerAddress
{
get { return _customerAddress; }
set { _customerAddress = value; }
}
//constructor
public Customer(string CustomerFirstName, string CustomerLastName, int CustomerNumber, string CustomerAddress)
{
_customerFirstName = CustomerFirstName;
_customerNumber = CustomerNumber;
_customerAddress = CustomerAddress;
_customerLastName = CustomerLastName;
if (CustomerNumber < 100000000 && CustomerNumber > 9999999)
{
}
else
{
Console.WriteLine("this is not a valid phone number, valid phone number must be 8 digits. Please try again.");
}
}
//methods
public override string ToString()
{
string obj = $"{CustomerFirstName}, {CustomerLastName}, \n{CustomerNumber}, \n{CustomerAddress}";
return obj;
}
}
class Program
{
static void Main(string[] args)
{
Store store = new Store();
store.start();
}
}
那是我应该调用该方法并引用我在该方法中创建的对象的地方。
如果您使用 visual studio,则创建 class 非常简单。这篇 MSDN 文章(似乎正是您要查找的内容)更深入地介绍了如何创建和使用 classes。查看示例部分:https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes
下面我更深入地介绍了您的每项要求。
- To test your application you should create a class Store with a method Start.
namespace PizzaStore
{
public class Store
{
public void Start()
{
// Create 3 Pizza objects
// Create 3 Customer objects
// Create 3 Order objects each with a different pizza.
}
}
}
- Call the Start method from the main method in the class Program.
找到名为 Program.cs
的文件,如果您使用的是新的 .NET 6 框架,则此文件看起来会有所不同。否则,如果您使用的是 .Net Framework 4.x,那么它应该看起来像传统的 class。有关详细信息,请参阅此 MSDN 文档:https://docs.microsoft.com/en-gb/dotnet/core/tutorials/top-level-templates
要调用 Start()
函数,您需要创建 Store
对象的新实例,然后通过引用该实例调用该函数。
Store store = new Store();
store.Start();
如果您决定使用此代码,则需要确保在文件顶部包含一个 using 语句。您可以在此处找到有关名称空间的更多信息:https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces
命名空间:
using PizzaStore;
否则您将不得不参考 class 包括命名空间。
PizzaStore.Store store = new PizzaStore.Store();
store.Start();
- and 4. In the Start method you should: Create 3 Pizza objects, 3 Customer objects and 3 Order objects each with a different pizza.
您似乎已经创建了对象。据我所知,您在创建对象方面所做的是正确的。您可以以不同的方式设置变量(例如使用属性:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties),但是您通过在构造函数中初始化值所做的一切都很好。
我建议的唯一改进是删除函数末尾的 return;
。这没有任何意义,可能会造成混淆。
- and 6. Print out order information: Using the object reference to each Order object, you should print out the pizza name, the customer name and the total price for each order.
我假设您使用的是控制台项目。在这种情况下写入控制台很简单。
要写一行,您可以使用:
Console.WriteLine("Hello, World!");
要在不换行的情况下写一些东西,您可以使用:
Console.Write("Hello, World!");
为了引用您创建的每个对象,只需使用您为它们创建的已声明变量名即可,例如:
Console.WriteLine(vesuvio.PizzaName);
您的任务的最终要求将需要您修改当前拥有的代码。创建订单时,您需要输入有助于识别订单的数据。
现在,你有披萨的价格,我假设的是税率和最终总价。
不幸的是,这并不能帮助我们识别订单,因为我们无法弄清楚订购的是什么类型的比萨饼以及谁订购了比萨饼。因此,您需要在订单中添加这两个关键部分的数据。
我建议在 Order
class 中为您创建的对象添加一个 属性 调用:
- 披萨
- 客户
您可以按照创建要传递给构造函数的字符串和数字的相同方式来执行此操作。但是请确保它存储在 class 中,而不仅仅是传递给构造函数。
例如,您的客户 class 看起来像这样:
public class Custumer
{
public Custumer(string name, string surname)
{
CustomerName = name;
CustomerSurname = surname;
}
public string CustomerName { get; set; }
public string CustomerSurname { get; set; }
}
您的订单 class 看起来像这样:
public class Order
{
public Order(double price, Customer customer)
{
OrderPrice = price;
OrderCustomer = customer;
}
public double OrderPrice { get; set; }
public Customer OrderCustomer { get; set; }
}
您可以通过执行以下操作来引用订单客户:
victorOrdre.OrderCustomer.CustomerName;
您似乎已经用代码更新了答案。这对于确定如何帮助您非常有帮助。关于您的更新,很明显您对 classes 和属性有很好的理解。
对于比萨 class,我建议您改用包含一系列比萨配料的列表。这样你就不需要为每个浇头创建一个新变量。
public class Pizza
{
public Pizza(string name, double price, params string[] args)
{
PizzaName = name;
PizzaPrice = price;
PizzaIngredients = new List<string>();
foreach (var item in args)
{
PizzaIngredients.Add(item);
}
}
public string PizzaName { get; set; }
public double PizzaPrice { get; set; }
public List<string> PizzaIngredients { get; set; }
}
根据您发布的说明,在我看来,获得满分所需的全部内容是:
class Program
{
static void Main(string[] args)
{
Store store = new Store();
store.Start();
}
}
public class Store
{
public void Start()
{
Order[] orders = new []
{
new Order(new Customer("Victor Hansen"), new Pizza("Vesuvio", 75m)),
new Order(new Customer("Jacob Pedersen"), new Pizza("Vegetarian", 80m)),
new Order(new Customer("Maghrete Ingrid"), new Pizza("Containda", 75m)),
};
foreach (Order order in orders)
{
Console.WriteLine($"{order.Customer.Name} has ordered {order.Pizza.Name} costing {order.Pizza.Price}.");
}
}
}
public class Order
{
public Customer Customer { get; private set; }
public Pizza Pizza { get; private set; }
public Order(Customer customer, Pizza pizza)
{
this.Customer = customer;
this.Pizza = pizza;
}
}
public class Pizza
{
public string Name { get; private set; }
public decimal Price { get; private set; }
public Pizza(string name, decimal price)
{
this.Name = name;
this.Price = price;
}
}
public class Customer
{
public string Name { get; private set; }
public Customer(string name)
{
this.Name = name;
}
}