C#如何使用接口清除其他类的变量值?

时间:2017-12-21 03:22:34

标签: c# interface

我正在尝试学习接口和抽象类。我有一个基本的Account类,PersonalAccounts的子类,BusinessAccounts的子类,IPayMyBill接口和main。我需要使用帐户输出帐户信息(Names,address,amountDue,invoiceDate,DueDate),输出此信息后,我必须在每个类的实例上调用接口的pay方法,将amountDue设置为0有人可以解释一下如何做到这一点,或者给我一些资源来帮助我解决这个问题吗?

到目前为止,这是我的代码,基本帐户:

abstract class Account : IPayMyBill
{

    private String _address;
    private Decimal _amountDue;
    private DateTime _invoiceDate;

    public Account(String address, Decimal amountDue, DateTime invoiceDate, DateTime DueDate)
    {
        Address = address;
        AmountDue = amountDue;
        InvoiceDate = invoiceDate;

    }
    public String Address
    {
        get { return _address; }
        set { _address = value; }
    }
    public Decimal AmountDue
    {
        get { return _amountDue; }
        set { _amountDue = value; }
    }
    public DateTime InvoiceDate
    {
        get { return _invoiceDate; }
        set { _invoiceDate = value; }
    }
    public abstract DateTime DueDate
    {
        get;
    }
    public void Pay()
    {

    }



}

孩子1:

class PersonalAccounts : Account , IPayMyBill
{
    private String _firstName;
    private String _lastName;

    public PersonalAccounts(String firstName, String lastName, String address, Decimal amountDue, DateTime invoiceDate, DateTime DueDate ) : base(address, amountDue, invoiceDate, DueDate)
    {
        FirstName = firstName;
        LastName = lastName;
    }
    public String FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }
    public String LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }
    public override DateTime DueDate 
    {
        get { return DateTime.Today.AddDays(30);}
    }
    public void Pay()
    {
       //not sure how to code this
    }
}

孩子2:

class BusinessAccounts : Account, IPayMyBill
{
    private String _businessName;

    public BusinessAccounts(String businessName, String address, Decimal amountDue, DateTime invoiceDate, DateTime DueDate) : base(address, amountDue, invoiceDate, DueDate)
    {
        BusinessName = businessName;
    }
    public String BusinessName
    {
        get { return _businessName; }
        set { _businessName = value; }
    }
    public override DateTime DueDate 
    {
        get { return DateTime.Today.AddDays(60); }
    }
    public void Pay()
    {
        //Not sure how to code this
    }
}

界面:

public interface IPayMyBill
{
    void Pay();
}

主:

class Program
{
    static void Main(string[] args)
    {

        PersonalAccounts PA = new PersonalAccounts("Andy", "Smith", "123 Hope St.", 500M, DateTime.Today, DateTime.Today.AddDays(30));

        Console.WriteLine("Name: " + PA.FirstName + " " + PA.LastName + string.Format(" Amount Due: {0:C2}", PA.AmountDue) + " Due: " + PA.DueDate.ToString("d"));

        BusinessAccounts BA = new BusinessAccounts("Sam's Scooters", "456 Lost Ave.", 700M, DateTime.Today, DateTime.Today.AddDays(60));

        Console.WriteLine("Name: " + BA.BusinessName + " " + string.Format("Amount Due: {0:C2}", BA.AmountDue) + " Due: " + BA.DueDate.ToString("d"));


        //I want to reset the Amount Due here, but don't know how.

        Console.ReadLine();

    }
}

4 个答案:

答案 0 :(得分:1)

您不需要在子类中编写Pay方法。

您的抽象Account类继承了IPayMyBill,只需在Account.Pay()方法中写入重置代码。

void Main()
{

    PersonalAccounts PA = new PersonalAccounts("Andy", "Smith", "123 Hope St.", 500M, DateTime.Today, DateTime.Today.AddDays(30));

    Console.WriteLine("Name: " + PA.FirstName + " " + PA.LastName + string.Format(" Amount Due: {0:C2}", PA.AmountDue) + " Due: " + PA.DueDate.ToString("d"));

    BusinessAccounts BA = new BusinessAccounts("Sam's Scooters", "456 Lost Ave.", 700M, DateTime.Today, DateTime.Today.AddDays(60));

    Console.WriteLine("Name: " + BA.BusinessName + " " + string.Format("Amount Due: {0:C2}", BA.AmountDue) + " Due: " + BA.DueDate.ToString("d"));


    //I want to reset the Amount Due here, but don't know how.
    PA.Pay();
    Console.WriteLine("Name: " + PA.FirstName + " " + PA.LastName + string.Format(" Amount Due: {0:C2}", PA.AmountDue) + " Due: " + PA.DueDate.ToString("d"));
    BA.Pay();
    Console.WriteLine("Name: " + BA.BusinessName + " " + string.Format("Amount Due: {0:C2}", BA.AmountDue) + " Due: " + BA.DueDate.ToString("d"));

    Console.ReadLine();
}

// Define other methods and classes here
abstract class Account : IPayMyBill
{

    private String _address;
    private Decimal _amountDue;
    private DateTime _invoiceDate;

    public Account(String address, Decimal amountDue, DateTime invoiceDate, DateTime DueDate)
    {
        Address = address;
        AmountDue = amountDue;
        InvoiceDate = invoiceDate;

    }
    public String Address
    {
        get { return _address; }
        set { _address = value; }
    }
    public Decimal AmountDue
    {
        get { return _amountDue; }
        set { _amountDue = value; }
    }
    public DateTime InvoiceDate
    {
        get { return _invoiceDate; }
        set { _invoiceDate = value; }
    }
    public abstract DateTime DueDate
    {
        get;
    }
    public void Pay()
    {
        this.AmountDue = 0;
    }
}

class PersonalAccounts : Account, IPayMyBill
{
    private String _firstName;
    private String _lastName;

    public PersonalAccounts(String firstName, String lastName, String address, Decimal amountDue, DateTime invoiceDate, DateTime DueDate) : base(address, amountDue, invoiceDate, DueDate)
    {
        FirstName = firstName;
        LastName = lastName;
    }
    public String FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }
    public String LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }
    public override DateTime DueDate
    {
        get { return DateTime.Today.AddDays(30); }
    }
}
class BusinessAccounts : Account, IPayMyBill
{
    private String _businessName;

    public BusinessAccounts(String businessName, String address, Decimal amountDue, DateTime invoiceDate, DateTime DueDate) : base(address, amountDue, invoiceDate, DueDate)
    {
        BusinessName = businessName;
    }
    public String BusinessName
    {
        get { return _businessName; }
        set { _businessName = value; }
    }
    public override DateTime DueDate
    {
        get { return DateTime.Today.AddDays(60); }
    }
}

public interface IPayMyBill
{
    void Pay();
}

答案 1 :(得分:0)

我认为你说的是​​抽象类和pay()抽象方法。

答案 2 :(得分:0)

除了其他答案之外

如果您希望控制派生类

中的付费功能,则无需任何界面
abstract class Account
{
   // you are forcing all Derived classes to implement their own functionality 
   public abstract void Pay();
}

public class Child : Account
{        
    public override void Pay()
    {
        // implement child pay functionality here 
    }
}

public abstract void Pay();只是强制派生类实现此

如果您没有抽象类,或者您想使用基类中不可用的方法和属性,您将使用接口

或者,如果要在pay中实现的功能在基类中都可用,那么您可以创建所有Derived类本身都具有的方法。

最后,您可以使用虚拟方法

abstract class Account
{
   public virtual void Pay()
   {
       // code which may or may not be overridden by the inheritor 
   }
}

// this class just implements the default pay behaviour
public class Child1 : Account
{
}

// this class overrides the default pay behaviour
public class Child2 : Account
{
   public virtual void Pay()
   {
       // Add your own custom pay functionality 
   }
}

// this class calls the default pay behaviour and does other things
public class Child3 : Account
{
   //other stuff
   public virtual void Pay()
   {
       // calls the default pay implementation in base class 
       base.Pay();

       // Add your additional pay functionality 
   }
}

答案 3 :(得分:0)

我知道如果所有子类的功能在基础上都相同,这已经得到了充分的回答,但我没有看到任何关于每个孩子是否需要不同实现功能的事情。在这种情况下,您可能希望将基类中的变量声明为protected而不是private - 来自protected (C# Reference)

  

受保护的成员可以在其类和派生类中访问   实例

这意味着你可以这样做:

abstract class Account : IPayMyBill
{
    private String _address;
    protected Decimal _amountDue;
    private DateTime _invoiceDate;
    ...
}

然后:

class PersonalAccounts : Account , IPayMyBill
{
    ...
    public void Pay()
    {
        // This will work because child classes can access protected members from their base class and _amountDue was declared above as protected
        _amountDue = 0;
    }

    public void ChangeAddress(string newAddress)
    {
        // This won't work because _address is private so can't be accessed by the child class
        _address = newAddress;
    }
}

编辑:对微软文档的引用进行了整理