Sunday, 23 May 2021

Object Oriented review - Abstract, inherent, Virtual

 


Abstract class:

using System;

namespace App

{

    public abstract class Saleman

    {

        private string _firstname;

        private string _lastname;


        public string Fullname { get {

                return string.Format("{0} {1}", this._firstname, this._lastname);

            } }

        public Saleman(string firstname, string lastname)

        {

            this._firstname = firstname;

            this._lastname = lastname;

        }


        public abstract void Sell();

    }

}

----------------------------------

using System;

namespace App

{

    public class CarSalePerson : Saleman

    {

        public CarSalePerson(string firstname, string lastname) : base(firstname, lastname)

        {

        }

        public override void Sell()

        {

            Console.WriteLine("This is {0}. I live in Canada", this.Fullname);

        }

    }

}

------------------------------

using System;

namespace App

{

    public class RetailSalePerson : Saleman

    {

        public RetailSalePerson(string firstname, string lastname) : base(firstname, lastname)

        {

        }

        public override void Sell()

        {

            Console.WriteLine("This is {0}. I live in VN", this.Fullname);

        }

    }

}


Main class:

static void Main(string[] args)
        {

            CarSalePerson TanLESaleMan = new CarSalePerson("Tan", "LE");
            Console.WriteLine(TanLESaleMan.Fullname);
            TanLESaleMan.Sell();

            RetailSalePerson TestLEESaleMan = new RetailSalePerson("Test", "LEE");
            Console.WriteLine(TestLEESaleMan.Fullname);
            TestLEESaleMan.Sell();
        }

OUTPUT:
Tan LE
This is Tan LE. I live in Canada
Test LEE
This is Test LEE. I live in VN



-----------------------------

 

Abstract class + Virtual

using System;

namespace App

{

    public abstract class Saleman

    {

        private string _firstname;

        private string _lastname;


        public string Fullname { get {

                return string.Format("{0} {1}", this._firstname, this._lastname);

            } }

        public Saleman(string firstname, string lastname)

        {

            this._firstname = firstname;

            this._lastname = lastname;

        }


       public virtual void Sell()

        {

            Console.WriteLine("This is {0}. I live in VN. MASTER", this.Fullname);

        }

    }

}

----------------------------------

using System;

namespace App

{

    public class CarSalePerson : Saleman

    {

        public CarSalePerson(string firstname, string lastname) : base(firstname, lastname)

        {

        }

        public override void Sell()

        {

            Console.WriteLine("This is {0}. I live in Canada", this.Fullname);

        }

    }

}

------------------------------

using System;

namespace App

{

    public class RetailSalePerson : Saleman

    {

        public RetailSalePerson(string firstname, string lastname) : base(firstname, lastname)

        {

        }

      

    }

}


Main class:

static void Main(string[] args)
        {

            CarSalePerson TanLESaleMan = new CarSalePerson("Tan", "LE");
            Console.WriteLine(TanLESaleMan.Fullname);
            TanLESaleMan.Sell();

            RetailSalePerson TestLEESaleMan = new RetailSalePerson("Test", "LEE");
            Console.WriteLine(TestLEESaleMan.Fullname);
            TestLEESaleMan.Sell();
        }

OUTPUT:
Tan LE
This is Tan LE. I live in Canada
Test LEE
This is Test LEE. I live in VN. MASTER

No comments:

Post a Comment

Implemented a Service to receive real time message from an API Webhook

 What is the webhook?  Ask Google :) 😅  This is my experience to build a Azure Function HTTP trigger to receive an event from SkyBox API W...