Thứ Bảy, 16 tháng 1, 2016

Ví dụ đơn giản về kế thừa và đa hình trong C# [C# console, Visual Studio 2013]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CSharpCoBan
{
    public class Program
    {

        static void Main(string[] args)
        {
            /*
            #region KHỞI ĐỘNG
            Console.Title = "Header"; // đặt tiêu đề cho Màn hình console
            Console.OutputEncoding = UnicodeEncoding.Unicode; // Output trên màn hình có thể hiển thị Unicode
            Console.WriteLine("Fist");

            //Thread.Sleep(5000); // sẽ đóng màn hình sau 5000 mili giây = 5 giây.

            float f = 5.6f; // khai báo kiểu float phải có f ở sau cùng.
            double x = 5.5;
            #endregion

            #region Kiểu enum
            //Console.Write(DayOfWeek.Monday);
            const int cc = 4;
            //cc = 10;

            #endregion
             * */

            Console.InputEncoding = UnicodeEncoding.Unicode;
            Console.OutputEncoding = UnicodeEncoding.Unicode;

            CollegeStudent cs = new CollegeStudent();
            cs.EnterInformation();
            cs.DisplayInformation();

            UniversityStudent us = new UniversityStudent();
            us.EnterInformation();
            us.DisplayInformation();


            //thu kiem tra ve ham tao voi tinh ke thua

            //Student sv01 = new CollegeStudent();
            //Student sv03 = new UniversityStudent();
            //CollegeStudent x = new CollegeStudent();
            //UniversityStudent y = new UniversityStudent();
            //y._ToString();


            Console.Read(); // dừng màn hình
        }

    }

    #region Ke thua da hinh
    /*
        Ví dụ về kế thừa và đa hình trong C#
        - Xây dựng lớp SV
          + thuộc tính: họ tên, năm sinh
          + phương thức: Khởi tạo, nhập thông tin
        - Xây dựng lớp SVCD kế thừa lớp SV
          Bổ sung:
          + thuộc tính: điểm năm 1, điểm năm 2, điểm năm 3
          + phương thức: tính điểm tổng kết, in thông tin
        - Xây dựng lớp SVDH kế thừa lớp SVCD
          Bổ sung:
          + thuộc tính: điểm năm 4

        Yêu cầu:
        - Khởi tạo mỗi lớp 1 đối tượng, thực hiện nhập thông tin, in thông tin.

        */

    public abstract class Student
    {
        protected string fullName;
        protected int yearOfBirth;

        /// <summary>
        /// Ham khoi dung doi tuong
        /// </summary>`
        /// <param name="fullName"></param>
        /// <param name="yearOfBirth"></param>

        public Student() { }
        public Student(string fullName, int yearOfBirth)
        {
            this.fullName = fullName;
            this.yearOfBirth = yearOfBirth;
        }
        /// <summary>
        /// Ham nhap thong tin Sinh vien
        /// </summary>
        public virtual void EnterInformation()
        {
         
            Console.Write("Input FullName: ");
            this.fullName = Console.ReadLine();
            Console.Write("Input Year of birth: ");
            this.yearOfBirth = int.Parse(Console.ReadLine());
        }
        public abstract void _ToString();
        public abstract double SummaryPoint();
        public abstract void DisplayInformation();
    }


    public class CollegeStudent : Student
    {
        private double pointOfYear01;
        private double pointOfYear02;
        private double pointOfYear03;

        public double PointOfYear01
        {
            get { return pointOfYear01; }
            set { pointOfYear01 = value; }
        }

        public double PointOfYear02
        {
            get { return pointOfYear02; }
            set { pointOfYear02 = value; }
        }

        public double PointOfYear03
        {
            get { return pointOfYear03; }
            set { pointOfYear03 = value; }
        }

        public CollegeStudent() { }

        /// <summary>
        /// Ham tao sinh vien cao dang
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="yearOfBirth"></param>
        /// <param name="pointOfYear01"></param>
        /// <param name="pointOfYear02"></param>
        /// <param name="pointOfYear03"></param>
        public CollegeStudent(string fullName, int yearOfBirth, double pointOfYear01, double pointOfYear02, double pointOfYear03)
            : base(fullName, yearOfBirth)
        {
            this.pointOfYear01 = pointOfYear01;
            this.pointOfYear02 = pointOfYear02;
            this.pointOfYear03 = pointOfYear03;
        }

        public override void EnterInformation()
        {
            base.EnterInformation();
            Console.Write("Input point of year 01: ");
            this.pointOfYear01 = double.Parse(Console.ReadLine());
            Console.Write("Input point of year 02: ");
            this.pointOfYear02 = double.Parse(Console.ReadLine());
            Console.Write("Input point of year 03: ");
            this.pointOfYear03 = double.Parse(Console.ReadLine());
        }

        /// <summary>
        /// Tinh diem tong ket
        /// </summary>
        /// <returns></returns>
        public override double SummaryPoint()
        {
            return (pointOfYear01 + pointOfYear02 + pointOfYear03) / 3;
        }

        /// <summary>
        /// Ham xuat thong tin sinh vien cao dang
        /// </summary>
        public override void DisplayInformation()
        {
            Console.WriteLine(" College Student: {0}, YearOfBirth: {1}, Point: {2}", this.fullName, this.yearOfBirth, this.SummaryPoint());
        }

        /// <summary>
        /// Ham hien thi loai sinh vien
        /// </summary>
        public override void _ToString()
        {
            Console.WriteLine("This is College Student");
        }
    }

    public class UniversityStudent : CollegeStudent
    {
        private double pointOfYear04;

        public double PointOfYear04
        {
            get { return pointOfYear04; }
            set { pointOfYear04 = value; }
        }

        public UniversityStudent() { }

        public UniversityStudent(string fullName, int yearOfBirth, double pointOfYear01, double pointOfYear02, double pointOfYear03, double pointOfYear04)
            : base(fullName, yearOfBirth, pointOfYear01, pointOfYear02, pointOfYear03)
        {
            this.pointOfYear04 = pointOfYear04;
        }

        public override void EnterInformation()
        {
            base.EnterInformation();
            Console.Write("Input point of year 04: ");
            this.pointOfYear04 = double.Parse(Console.ReadLine());

        }

        public override double SummaryPoint()
        {
            return (this.PointOfYear01 + this.PointOfYear02 + this.PointOfYear03 + this.PointOfYear04) / 4;
        }

        public override void DisplayInformation()
        {
            Console.WriteLine(" University Student: {0}, YearOfBirth: {1}, Point: {2}", this.fullName, this.yearOfBirth, this.SummaryPoint());
        }

        public override void _ToString()
        {
            Console.WriteLine("This is University Student");
        }
    }
    #endregion
}