微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

getter setter 和当前上下文中不存在的对象

如何解决getter setter 和当前上下文中不存在的对象

您好,我是 OOP 的新手,我现在正在执行一项任务,但是我无法将客户类和对象链接到我的主程序中。它说它在当前上下文中不存在。这也可能是因为我没有完全理解命名空间,并且文件目录中的类名可能不正确,但老实说我不知道​​。

主程序(文件直接名为 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();
    }
}

客户类(文件目录中的名称为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();

这一行创建了一个 Customer 类的新实例。使用关键字 new 创建实例并将其分配给 variable 后,您可以使用 variable.Propertyvariable.Method() 调用类方法和属性。 这适用于非静态类。

实例参考:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/objects

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。