By GokiSoft.com| 08:35 12/10/2021|
C Sharp

[Video] Tìm hiểu tích chất đa hình C# - Tính chất trừu tượng C# - Interface C#

Link Video Bai Giang


Nội dung kiến thức:
- T/c đa hình
- T/c trừu tượng

- Interface

Mini Project: Xây dựng ứng dụng quản lý sở thú: Tiger, Bear, Cat
	- Aminal -> Parent class
		Thuộc tính:
			Name
			Age
			FoodType
		Phương thức/hạnh động
			Input
			Display
			ShowSound
	- Tiger, Bear, Cat : Animal
	




#Program.cs


using System;
using System.Collections.Generic;

namespace Lesson03
{
    class Program
    {
        static void Main(string[] args)
        {
            //T/c truu tuong
            Tiger tiger = new Tiger();
            tiger.ShowSound();

            //T/c da hinh
            Animal animal = new Animal();
            animal.Display();

            Tiger tiger1 = new Tiger();
            tiger1.ShowSound();
            tiger1.ShowMsg();

            Animal animal1 = new Tiger();
            //animal1 -> Animal trong TH -> typing code & compile
            //animal1 -> Tiger trong TH -> runtime
            //animal1 -> ban chat -> doi tuong that -> Tiger
            if(animal1 is Tiger)
            {
                ((Tiger)animal1).ShowMsg();
            }

            //Ung dung T/c da hinh -> Su dung Mang quan ly danh sach nhieu loai dong vat khac: Tiger, Dog, Cat, Bear, ...
            Object obj = new Bear();
            if(obj is Bear)
            {
                ((Bear)obj).ShowSound();
            }

            var o = new Tiger();
            o.ShowMsg();

            //Mang dong
            List<Animal> animalList = new List<Animal>();

            //Them phan tu
            animalList.Add(new Tiger());
            animalList.Add(new Tiger());
            animalList.Add(new Cat());
            animalList.Add(new Bear());
            //Xoa phan tu trong
            animalList.RemoveAt(1);
            //Duyet qua cac phan tu trong mang
            for(int i =0;i<animalList.Count;i++)
            {
                animalList[i].ShowSound();
            }

            //Interface
            IInput input = new Student();
            input.Input();

            IInput input1 = new Cat();
            input1.Input();

            //Vi du: Su dung 1 List -> Quan ly nhap du lieu cua nhieu loai doi tuong khac trong chuong
            //Tiger, Bear, Cat, Student
            List<IInput> inputList = new List<IInput>();
            inputList.Add(input);
            inputList.Add(input1);
            //.v.v
        }
    }
}


#Animal.cs


using System;
namespace Lesson03
{
    public class Animal : IInput
    {
        public string Name { get; set; }
        public string FoodType { get; set; }
        public int Age { get; set; }

        public Animal()
        {
        }

        public Animal(string name, string foodType, int age)
        {
            Name = name;
            FoodType = foodType;
            Age = age;
        }

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

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

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

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

        //Khong the trien khai dc phan than cho phuong thuc
        //Khi children class -> ke thua -> Animal class -> Trien khai dc phan than
        //Dog -> 1 am thanh, cat -> am thanh rieng, Tiger,Bear,..v.v
        public virtual void ShowSound() { }
    }
}


#Bear.cs


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

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


#Cat.cs


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

        public override void ShowSound()
        {
            Console.WriteLine("Am thanh cat ...");
        }
    }
}


#Tiger.cs


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

        public override void ShowSound()
        {
            Console.WriteLine("Am thanh Tiger ...");
        }

        public void ShowMsg()
        {
            Console.WriteLine("Hello World!!!");
        }
    }
}


#Student.cs


using System;
namespace Lesson03
{
    public class Student : IInput
    {
        public Student()
        {
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin sinh vien");
        }
    }
}


#IInput.cs


using System;
namespace Lesson03
{
    public interface IInput
    {
        void Input();
    }
}


Tags:

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

5

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