By GokiSoft.com| 14:47 22/05/2020|
C Sharp

[Share Code] Hướng dẫn tìm hiểu lập trình OOP trong C# - Lập Trình C# - Lập Trình C Sharp

[Share Code] Hướng dẫn tìm hiểu lập trình OOP trong C# - Lập Trình C# - Lập Trình C Sharp


#Animal.cs


using System;
namespace Lession5
{
    public abstract class Animal
    {
        public string Name { get; set; }

        public Animal()
        {
        }

        public void Running() {
            Console.WriteLine("Animal is running");
        }

        public abstract void ShowSound();
    }
}


#Cat.cs


using System;
namespace Lession5
{
    public class Cat : Animal
    {
        public Cat()
        {
        }

        public override void ShowSound()
        {
            Console.WriteLine("Meo .. meo ..");
        }
    }
}


#Dog.cs


using System;
namespace Lession5
{
    public class Dog : Animal
    {
        public Dog()
        {
        }

        public override void ShowSound()
        {
            Console.WriteLine("Go .. go ..");
        }
    }
}


#People.cs


using System;
namespace Lession5
{
    public class People
    {
        public string Fullname { get; set; }
        public string Gender { get; set; }
        public string Birthday { get; set; }

        public People()
        {
        }

        public virtual void Input() {
            Console.WriteLine("Nhap Ten: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Nhap Gioi Tinh: ");
            Gender = Console.ReadLine();

            Console.WriteLine("Nhap Ngay Sinh: ");
            Birthday = Console.ReadLine();
        }

        public virtual void Display() {
            Console.Write("Ten: {0}, Gioi Tinh: {1}, Ngay Sinh: {2}", Fullname, Gender, Birthday);
        }

        //overloading...
        public void Running() {
            Console.WriteLine("{0} dang chay", Fullname);
        }

        //overloading...
        public void Running(string msg)
        {
            Console.WriteLine("{0} dang chay >> {1}", Fullname, msg);
        }
    }
}


#Student.cs


using System;
namespace Lession5
{
    public class Student : People
    {
        public string Rollno { get; set; }

        public Student()
        {
        }

        public override void Input() {
            base.Input();

            Console.WriteLine("Nhap MSV: ");
            Rollno = Console.ReadLine();
        }

        public override void Display()
        {
            base.Display();
            Console.Write(", MSV: {0}", Rollno);
            Console.WriteLine("");
        }

        public void ShowMessage() {
            Console.WriteLine("Hello Msg");
        }
    }
}


#Program.cs


using System;

namespace Lession5
{
    class Program
    {
        static void Main(string[] args)
        {
            //Tinh ke thua + bao dong.
            Student std = new Student();
            std.Fullname = "Tran Van A";
            std.Input();
            std.Display();

            //Tinh da hinh
            People people = new Student();
            if(people is Student) {
                ((Student)people).ShowMessage();
            }

            //Tinh truu tuong
            Cat cat = new Cat();
            Dog dog = new Dog();
            cat.ShowSound();
            dog.ShowSound();
        }
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)