简体   繁体   English

我可以对不同的类使用相同的属性吗?

[英]Can i use the same properties for different classes?

I'm supposed to find the classes and their responsibilities from the problem statement.我应该从问题陈述中找到类及其职责。

The bank offers the following type of accounts to its customers: savings account, checking account, and money market account.该银行为其客户提供以下类型的账户:储蓄账户、支票账户和货币市场账户。 customers are allowed to withdraw (decrease) and deposit (increase) money into those accounts.客户可以在这些账户中提取(减少)和存入(增加)资金。

my question is:我的问题是:

Can I create 3 different classes that use the same 2 properties (increase and decrease)?我可以创建 3 个使用相同 2 个属性(增加和减少)的不同类吗? Also, would it work for the above scenario.此外,它是否适用于上述情况。 Thanks in advance.提前致谢。

Why not?为什么不? You could create one parent class with that properties and get inherited by child classes.您可以使用该属性创建一个父类并由子类继承。 Also you can create interface and implement custom logic for each class you want to have such property.您还可以为每个想要拥有此类属性的类创建接口并实现自定义逻辑。

public class Parent
{
    public int Increase { get; set; }
    public int Decrease { get; set; }
}

public class Child1 : Parent
{

}

public class Child2 : Parent
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM