By GokiSoft.com| 15:26 28/10/2021|
C Sharp

[Share Code] Tìm hiểu lập trình OOP + Abstract class + Interface trong C# - Lập trình C# - Lập trình C Sharp

Tìm hiểu lập trình OOP + Abstract class + Interface trong C# - Lập trình C# - Lập trình C Sharp

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

Xây dựng 1 dự án quản lý động vật (Cat, Dog)
	- Animal:
		Thuộc tính: tên động vât, tuổi, loại thức ăn
		Hành đồng: chạy, ngủ, phát âm thanh
	- Cat
		-> Tính chất tương tự động vật
	- Dog
		-> Tính chất tương tự động vật
	- People
		-> Tinh chat: ten, tuoi, dia chi, sdt
		-> Hanh dong: chay, hoc
	- Car
		-> Tinh chat: ten, age, color
		-> Hanh dong: Chay, nghi
Yêu cầu:
	Viết chương quản lý tất cả động vật
	Tạo ra được 1 List => quản lý toàn bộ hành động running của tât ca các đối tượng : cat, dog, people, car.




#Program.cs


using System;
using System.Collections.Generic;

namespace Lession3
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat();
            cat.ShowSound();

            Animal animal = new Dog();//tinh chat da hinh
            //Khi khai kieu du lieu tu class parent => khoi tao doi tuong tu class parent hoac class child
            animal.ShowSound();

            //Tao 1 mang => quan ly danh sach dong vat
            List<Animal> animals = new List<Animal>();
            animals.Add(new Dog());
            animals.Add(new Cat());
            animals.Add(new Cat());
            animals.Add(new Cat());
            animals.Add(new Dog());

            foreach(Animal a in animals) {
                a.ShowSound();
            }

            //Interface
            List<IAction> actions = new List<IAction>();
            actions.Add(new Dog());
            actions.Add(new Cat());
            actions.Add(new Dog());
            actions.Add(new People());
            actions.Add(new Car());

            foreach (Animal o in animals)
            {
                o.ShowSound();
            }
        }

        static void testRunning() {
            
        }
    }
}


#People.cs


using System;
namespace Lession3
{
    public class People : IAction
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public string PhoneNumber { get; set; }

        public People()
        {
        }

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


#Animal.cs


using System;
namespace Lession3
{
    public abstract class Animal : IAction
    {
        public string Name { get; set; }
        public string FoodType { get; set; }
        public int Age { get; set; }

        public Animal()
        {
        }

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

        public void Sleeping() {
            Console.WriteLine("Animal is sleeping");
        }

        public abstract void ShowSound();
    }
}


#Car.cs


using System;
namespace Lession3
{
    public class Car : IAction
    {
        public string Name { get; set; }
        public string Color { get; set; }
        
        public Car()
        {
        }

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


#Cat.cs


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

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


#Dog.cs


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

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


#IAction.cs


using System;
namespace Lession3
{
    public interface IAction
    {
        void Running();
    }
}


#Lession3.csproj


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

</Project>


Tags:

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

5

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