By GokiSoft.com| 15:28 25/09/2021|
C Sharp

[Share Code] Lập trình hướng đối tượng OOP - tính chất đa hình, trừu tượng, interface trong C#



Tính chất lập trình OOP
- Tính chất bao đóng: OK
- Tính chất kế thừa: OK
- Tính chất đa hình
- TÍnh chất trừu tượng

Interface
=========================================================
Mini Project: Thiết kế 1 chương trình quản lý động -> Tiger, Cat, Dog
- Tạo 1 mảng quản lý tất cả các động vật: Tiger, Cat, Dog -> Hiển thị thông tin tất cả động vật
- Đếm số động vật Tiger, Dog, Cat trong mảng trên
- Hiển thị âm thanh tiếng kêu của động vật

- Tiger, Dog, Cat, Student, Car => Tao 1 mang quan ly tat ca cac object tren -> 1 mang -> Quan ly nhom hanh dong running().

Animal:
	Thuộc tính:
		Tên
		Tuổi
		Loại thức ăn
	Phương thức:
		Nhập
		Hiển thị
		Hiên thị tiếng kêu
Tiger, Dog, Cat -> Animal




#Program.cs


using System;
using System.Collections.Generic;
using Lesson03.Models;

namespace Lesson03
{
    class Program
    {
        static void Main_Comment(string[] args)
        {
            //Tim hieu t/c da hinh
            //Animal animal = new Animal();
            //animal.Input();
            //animal.Display();

            Tiger tiger = new Tiger();
            tiger.Input();
            tiger.ShowSound();

            //Ban than cac ban: friends, You <-> Teacher, You <-> Parent, You <-> GrandParent, ...
            Animal animal1 = new Dog();
            animal1.Input();
            animal1.ShowSound();

            //Kiem tra xem object -> khoi tao tu 1 child class
            if(animal1 is Dog)
            {
                ((Dog)animal1).ShowMsg("Hello World!!!");
            }

            //Ung dung da hinh trong TH nao???
            List<Dog> dogsList = new List<Dog>();
            List<Tiger> tigerList = new List<Tiger>();
            List<Cat> catList = new List<Cat>();

            List<Animal> animalList = new List<Animal>();
            animalList.Add(new Tiger());
            animalList.Add(new Dog());
            animalList.Add(new Dog());
            animalList.Add(new Cat());
            animalList.Add(new Cat());
            //animalList.Add(new Animal());

            for(int i=0;i<animalList.Count;i++)
            {
                animalList[i].Input();
            }

            for (int i = 0; i < animalList.Count; i++)
            {
                animalList[i].Display();
            }

            //Đếm số động vật Tiger, Dog, Cat trong mảng trên
            int tigerCount = 0, dogCount = 0, catCount = 0;
            for (int i = 0; i < animalList.Count; i++)
            {
                if(animalList[i] is Tiger)
                {
                    tigerCount++;
                } else if (animalList[i] is Dog)
                {
                    dogCount++;
                } else if (animalList[i] is Cat)
                {
                    catCount++;
                }
            }
            int otherCount = animalList.Count - tigerCount - dogCount - catCount;
            Console.WriteLine("Tiger: {0}, dog: {1}, cat: {2}, other: {2}", tigerCount, dogCount, catCount, otherCount);

            //Hiển thị âm thanh tiếng kêu của động vật
            for (int i = 0; i < animalList.Count; i++)
            {
                animalList[i].ShowSound();
            }

            Bear bear = new Bear();
            bear.ShowSound();
        }
    }
}


#Test.cs


using System;
using System.Collections.Generic;
using Lesson03.Models;

namespace Lesson03
{
    public class Test
    {
        static void Main(string[] args)
        {
            List<IRunning> runnings = new List<IRunning>();

            Dog dog = new Dog();
            runnings.Add(dog);

            Student std = new Student();
            runnings.Add(std);

            Car car = new Car();
            runnings.Add(car);

            for(int i=0;i<runnings.Count;i++)
            {
                runnings[i].onRunning();
            }
        }
    }
}


#Animal.cs


using System;
namespace Lesson03.Models
{
    public abstract class Animal : IRunning
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string FoodType { get; set; }

        public Animal()
        {
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten: ");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap tuoi: ");
            Age = int.Parse(Console.ReadLine());

            Console.WriteLine("Nhap thuc an: ");
            FoodType = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("Ten: {0}, tuoi: {1}, thuc an: {2}", Name, Age, FoodType);
        }

        //Ly do su dung t/c truu tuong
        //TH1: Phuong thuc -> no body -> chuyen no ve abstract method -> bat buoc class -> abtract class
        //TH2: Child Class -> bat buoc ghi de phuong thuc ShowSound -> abstract method ...
        public abstract void ShowSound();

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


#Bear.cs


using System;
namespace Lesson03.Models
{
    public class Bear : Animal
    {
        public Bear()
        {
        }

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


#Car.cs


using System;
namespace Lesson03.Models
{
    public class Car : IRunning
    {
        public Car()
        {
        }

        public void onRunning()
        {
            Console.WriteLine("Car is running");
        }
    }
}


#Cat.cs


using System;
namespace Lesson03.Models
{
    public class Cat : Animal
    {
        public Cat()
        {
        }

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


#Dog.cs


using System;
namespace Lesson03.Models
{
    public class Dog : Animal
    {
        public Dog()
        {
        }

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

        public void ShowMsg(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}


#Student.cs


using System;
namespace Lesson03.Models
{
    public class Student : IRunning
    {
        public Student()
        {
        }

        public void onRunning()
        {
            Console.WriteLine("Student is running");
        }
    }
}


#Tiger.cs


using System;
namespace Lesson03.Models
{
    public class Tiger : Animal
    {
        public Tiger()
        {
        }

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


Tags:

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

5

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