C#如何使用接口清除其他类的变量值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#如何使用接口清除其他类的变量值?相关的知识,希望对你有一定的参考价值。
我正在尝试学习接口和抽象类。我有一个基本的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();
}
}
您不需要在Child类中编写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();
}
我认为你说的是抽象类与pay()抽象方法。
除了其他答案
如果您希望控制Derived类的付费功能,则无需任何界面
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();
只是强制Derived Class实现这一点
如果您没有抽象类,或者想要使用基类中不可用的方法和属性,则可以使用接口
或者,如果要在pay中实现的功能在基类中都可用,则可以创建所有Derived类本身具有的方法。
最后,您可以使用Virtual方法
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
}
}
我知道如果所有子类的功能都是相同的,通过将它放入基础,已经得到了充分的回答,但我没有看到任何关于每个孩子是否需要不同实现功能的事情。在这种情况下,您可能希望将基类中的变量声明为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;
}
}
编辑:预测微软文档的引用
以上是关于C#如何使用接口清除其他类的变量值?的主要内容,如果未能解决你的问题,请参考以下文章