getter setter 和当前上下文中不存在的对象
getter setter and objects not existing in the current context
您好,我是 OOP 的新手,我现在正在做一个任务,但是我不能 link 客户 class 和反对进入我的主程序。它说它在当前上下文中不存在。这也可能是由于我不完全理解名称空间,文件目录中 class 的名称可能不正确,但老实说我不知道。
主程序(文件直接名称为MainProg)
class Program
{
static void Main(string[] args)
{
//New Customer Account Sequence
//Customer customerOne = new Customer();
Console.WriteLine("Customer Details:\n-----------------\n");
Console.Write("Please enter cutomer forename: ");
customerOne.Forename = Console.ReadLine();
customerOne.Surname = Console.ReadLine();
customerOne.Address = Console.ReadLine();
customerOne.Town = Console.ReadLine();
customerOne.Postcode = Console.ReadLine();
CurrentAccount accountCurrOne = new CurrentAccount();
//accountCurrOne.AccountNumber = 1000;
SavingsAccount accountSavOne = new SavingsAccount();
accountSavOne.AccountNumber = 1001;
accountCurrOne.Open(customerOne);
accountSavOne.Open(customerOne);
//Initial deposit
decimal depositAmount;
depositAmount = decimal.Parse(Utility.Console.Ask("Enter initial deposit: "));
accountCurrOne.Deposit(depositAmount);
accountSavOne.Deposit(depositAmount);
// End of New Customer Account Sequence
Console.ReadLine();
}
}
客户Class(文件目录中的名称是CustomerOne)
class CustomerOne : BankAccount
{
protected string _Forename;
protected string _Surname;
protected string _Address;
protected string _Town;
protected string _Postcode;
public string Forename
{
get { return _Forename; }
set { Forename = value; }
}
public string Surname
{
get { return _Surname; }
set { Surname = value; }
}
public string Address
{
get { return _Address; }
set { Address = value; }
}
public string Town
{
get { return _Town; }
set { Town = value; }
}
public string Postcode
{
get { return _Postcode; }
set { Postcode = value; }
}
}
谁能告诉我哪里搞砸了?
为了调用一个对象的属性和方法,您必须实例化它。
问题是您评论的以下行:
//Customer customerOne = new Customer();
此行创建了客户的新实例 class。使用关键字 new
创建实例并将其分配给 variable
后,您可以使用 variable.Property
或 variable.Method()
调用 class 方法和属性。
这适用于非静态的 classes。
实例参考:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/objects
您好,我是 OOP 的新手,我现在正在做一个任务,但是我不能 link 客户 class 和反对进入我的主程序。它说它在当前上下文中不存在。这也可能是由于我不完全理解名称空间,文件目录中 class 的名称可能不正确,但老实说我不知道。
主程序(文件直接名称为MainProg)
class Program
{
static void Main(string[] args)
{
//New Customer Account Sequence
//Customer customerOne = new Customer();
Console.WriteLine("Customer Details:\n-----------------\n");
Console.Write("Please enter cutomer forename: ");
customerOne.Forename = Console.ReadLine();
customerOne.Surname = Console.ReadLine();
customerOne.Address = Console.ReadLine();
customerOne.Town = Console.ReadLine();
customerOne.Postcode = Console.ReadLine();
CurrentAccount accountCurrOne = new CurrentAccount();
//accountCurrOne.AccountNumber = 1000;
SavingsAccount accountSavOne = new SavingsAccount();
accountSavOne.AccountNumber = 1001;
accountCurrOne.Open(customerOne);
accountSavOne.Open(customerOne);
//Initial deposit
decimal depositAmount;
depositAmount = decimal.Parse(Utility.Console.Ask("Enter initial deposit: "));
accountCurrOne.Deposit(depositAmount);
accountSavOne.Deposit(depositAmount);
// End of New Customer Account Sequence
Console.ReadLine();
}
}
客户Class(文件目录中的名称是CustomerOne)
class CustomerOne : BankAccount
{
protected string _Forename;
protected string _Surname;
protected string _Address;
protected string _Town;
protected string _Postcode;
public string Forename
{
get { return _Forename; }
set { Forename = value; }
}
public string Surname
{
get { return _Surname; }
set { Surname = value; }
}
public string Address
{
get { return _Address; }
set { Address = value; }
}
public string Town
{
get { return _Town; }
set { Town = value; }
}
public string Postcode
{
get { return _Postcode; }
set { Postcode = value; }
}
}
谁能告诉我哪里搞砸了?
为了调用一个对象的属性和方法,您必须实例化它。 问题是您评论的以下行:
//Customer customerOne = new Customer();
此行创建了客户的新实例 class。使用关键字 new
创建实例并将其分配给 variable
后,您可以使用 variable.Property
或 variable.Method()
调用 class 方法和属性。
这适用于非静态的 classes。
实例参考:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/objects