jeudi 30 juin 2016

class designing - inheritance or abstract or interface

I have multiple classes having one common property and I am setting that property in constructor of that class.

class Expense1
{
    int _costval;

    public Expense1(int cost)
        {
            _costval = cost;
        }
    ///other properties and methods..
} 
class Expense2
{
    int _costval;

    public Expense1(int cost)
        {
            _costval = cost;
        }
    ///other properties and methods..
} 
class Expense3
{
    public int Costval;

    public Expense1(int cost)
        {
            Costval = cost;
        }
    ///other properties and methods..
} 

at some point I need to access "Costval". something like

Console.WriteLine(@object.CostVal)

That object can be of any type expense1 or expense2 or expense3..

how can I do that? should I create basic abstract class and move costval and constructor to it? please advice.

Aucun commentaire:

Enregistrer un commentaire