Home > front end >  Realising of the function
Realising of the function

Time:12-15

can anybode pls tell me, how to realise my function in main body ? All works, but i want to do a catalog of employers, so how to write employe to list or massive ? Thank you !

class Catalog : Employe
{
    Employe[] employes = new Employe[10];

    Employe p1 = new Employe(14, "Mark", "James", 124151, "Coder", 4000);

    public Catalog(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
    {
        employes[1] = p1;
    }

    public void CatalogLog()
    {
        for(int i = 0; i < employes.Length; i  )
            Console.WriteLine(employes[i]);
    } 
}

class TestInheritence
{
    public static void Main(string[] args)
    {
        Employe[] employes = new Employe[10];
        
    }
    
}

CodePudding user response:

If you want to add the employees to the catalog you can have an 'add' function that will add the employee to the catalog:

class Catalog : Employe
{
    List<Employe> employes = new List<Employe>();
    
    public void AddEmployee(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
    {
        Employe p1 = new Employe(_age, _firstName, _lastName, _id, _job, _salary);

        employes.Add(p1);
    }

    public void CatalogLog()
    {
        for(int i = 0; i < employes.Count(); i  )
            Console.WriteLine(employes[i]);
    } 
}

class TestInheritence
{
    public static void Main(string[] args)
    {
        Catalog catalog = new Catalog();
        catalog.AddEmployee(14, "Mark", "James", 124151, "Coder", 4000);
        // Add more employees.
        
    }
    
}

But I think that this is the wrong use case for inheritance. Usually you want to inherit when there is a 'Is-a' relationship between the types. But 'Catalog' is not type of 'Employee'

CodePudding user response:

I think you didn't set up the inheritance hierarchy with the right logic. The base class Employee is extensible and contains base methods:

public class Employee
{
    private int _id;
    private string _firstName; 
    
    public Employee(int id, string firstName)
    {
        _id = id;
        _firstName = firstName;
    }
    
    public int GetID()
    {
        return _id;
    }
    
    public void SetID(int id)
    {
        if(id > 0)
            _id = id;
    }   
    
    public void Print()
    {
        Console.WriteLine("ID: {0}\tFirst Name: {1}", this._id, this._firstName);
    }
}

The derived class allows the object to expand by adding new methods and properties to the properties of the base class:

public class Manager : Employee 
{
    private string _city;
    
    public Manager(int id, string firstName, string city) : base(id, firstName)
    {
        _city = city;
    }
    
    public string GetCity()
    {
        return _city;
    }
}

To test how these two classes work, you can review the application code below:

                
public class Program
{
    public static void Main()
    {           
        Employee[] employees = new[]
        {
            new Employee(1, "Thomas"),
            new Employee(2, "John"),
            new Employee(3, "Erick"),
            new Employee(4, "Ahmet"),
            new Employee(5, "Sun")
        };
        
        employees[0].Print();
        
        Manager manager = new Manager(6, "Johnson", "London");
        manager.Print();
        Console.WriteLine("City: {0}", manager.GetCity());
    }
}
  • Related