By GokiSoft.com| 14:41 28/10/2021|
C Sharp

[Video] Tìm hiểu về lập trình OOP Tính chất đa hình - tính chất trừu tượng - interface trong C# - Lập trình C#

Link Video Bài Giảng


Nội dung kiến thức video bao gồm cả phần chữa bài tập tại link:

Chương trình quản lý sách - Develop Book Project - Lập Trình C# - OOP trong C# - C Sharp - C2010G


Nội dung bài học:

Tính chất trong lập trình OOP:
	- T/c đóng gói -> OK
	- T/c kế thừa -> OK
	- T/c đa hình
	- T/c trừu trương
	- Interface

- Thiết kế chương trình quản lý khối hình học:
	- HinhHoc
		-> TinhChuVi
		-> TinhDienTich
	- HinhTron
	- HinhCN




#Program.cs


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

namespace Lesson03
{
    class Program
    {
        static void Main(string[] args)
        {
            //Tich chat da hinh
            //HinhHoc hinhHoc = new HinhHoc(); -> Error -> convert class -> abtract class
            //Console.WriteLine("Chu vi: " + hinhHoc.TinhChuVi());

            HinhTron hinhTron = new HinhTron(2.18F);
            Console.WriteLine("Chu vi: " + hinhTron.TinhChuVi());

            HinhHoc hinhHoc1 = new HinhCN(2.6F, 8.5F);
            Console.WriteLine("Chu vi: " + hinhHoc1.TinhChuVi());

            if(hinhHoc1 is HinhCN)
            {
                ((HinhCN)hinhHoc1).Display();
            }

            //Tich truu tuong -> Xem va thiet ke lai class object

            //Khai bao mang dong
            List<HinhHoc> hinhList = new List<HinhHoc>();
            //Them 1 phan tu vao mang
            hinhList.Add(new HinhCN(2, 6));
            hinhList.Add(new HinhCN(6, 8));
            hinhList.Add(new HinhTron(2.6));

            for(int i=0;i<hinhList.Count;i++)
            {
                double cv = hinhList[i].TinhChuVi();
                Console.WriteLine("CV: " + cv);
            }

            //Test interface
            List<IInput> inputList = new List<IInput>();
            HinhTron h1 = new HinhTron();
            inputList.Add(h1);
            HinhTron h2 = new HinhTron();
            inputList.Add(h2);
            HinhCN h3 = new HinhCN();
            inputList.Add(h3);

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


#IInput.cs


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


#HinhTron.cs


using System;
namespace Lesson03.Models
{
    public class HinhTron : HinhHoc, IInput
    {
        public double Radius { get; set; }

        public HinhTron()
        {
        }

        public HinhTron(double radius)
        {
            Radius = radius;
        }

        public override double TinhChuVi()
        {
            return 2 * Math.PI * Radius;
        }

        public override double TinhDienTich()
        {
            return Math.PI * Radius * Radius;
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin hinh tron");
        }
    }
}


#HinhHoc.cs


using System;
namespace Lesson03.Models
{
    public abstract class HinhHoc
    {
        public HinhHoc()
        {
        }

        public abstract double TinhChuVi();

        public abstract double TinhDienTich();
    }
}


#HinhCN.cs


using System;
namespace Lesson03.Models
{
    public class HinhCN : HinhHoc, IInput
    {
        public double Width { get; set; }
        public double Height { get; set; }

        public HinhCN()
        {
        }

        public HinhCN(double width, double height)
        {
            Width = width;
            Height = height;
        }

        public override double TinhChuVi()
        {
            return 2 * (Width + Height);
        }

        public override double TinhDienTich()
        {
            return Width * Height;
        }

        public void Display()
        {
            Console.WriteLine("Width: {0}, height: {1}", Width, Height);
        }

        public void Input()
        {
            Console.WriteLine("Nhap thong tin hinh CN");
        }
    }
}




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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó