【发布时间】:2019-06-01 20:28:13
【问题描述】:
我有这个继承自 Loan 的 LoanWithClient 模型:
public class LoanWithClient : Loan
{
public Client Client { get; set; }
}
如何访问整个继承的Loan 对象,而无需显式编写其属性?
LoanWithClient 不包含 Loan 的定义
return new LoanWithClient
{
**Loan** = loan, //The Loan is erroring: LoanWithClient does not contain a definition for Loan
Client = client
};
班级贷款:
public class Loan
{
public int ID { get; set; }
public string Address { get; set; }
public string City { get; set; }
//etc..
}
【问题讨论】:
-
类
Loan的帖子定义 -
我想你可能误解了“继承”的含义。这意味着派生类 (
LoanWithClient) 具有其父类 (Loan) 的特性(方法、属性等)。如果LoanWithClient和Loan都没有属性Loan,那么在构造实例时将无法使用它。如果Loan具有Amount属性,您可以像使用Client一样直接使用它。 -
创建一个构造函数并从中调用基类构造函数。参见例如stackoverflow.com/questions/12051/…
-
另外,您的对象模型看起来有点可疑。地址真的是贷款的财产吗?
-
不要混淆 is-a 和 has-a。
LoanWithClient是Loan。它没有Loan。
标签: c#