By GokiSoft.com| 14:59 20/05/2020|
C Sharp

[Share Code] Hướng dẫn tìm hiểu OOP căn bản trong C#, Lập Trình C#, Lập Trình C Sharp - Phần 1

[Share Code] Hướng dẫn tìm hiểu OOP căn bản trong C#, Lập Trình C#, Lập Trình C Sharp - Phần 1



#People.cs


using System;
namespace Lession4
{
    public class People
    {
        //tao getter & setter => public cho thuoc tinh fullname
        public string Fullname { get; set; }

        //Bieu dien cach code theo phong cach cua Java
        private string _gender;

        public string getGender() {
            return _gender;
        }

        public void setGender(string gender) {
            _gender = gender;
        }

        //thuoc tinh address => getter => public, setter => private
        public string Address { get; private set; }

        //CMTND => dieu kien => do dai tu 10 -> 20 ky tu
        private string _cmtnd;

        public string Cmtnd {
            get {
                return _cmtnd;
            }

            set {
                if(value.Length >= 10 && value.Length <= 20) {
                    this._cmtnd = value;
                } else {
                    Console.WriteLine("Du lieu dau vao sai >> yeu cau length >= 10 & length <= 20");
                }
            }
        }

        //private get; public set;
        public string Birthday { private get; set; }

        public People()
        {
        }

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

        public void Input() {
            //nhap du lieu people
            Console.WriteLine("Nhap ten: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();
        }

        public void Display() {
            //Hien thi du lieu
            Console.WriteLine("Peopel >> {0}, {1}", Fullname, Address);
        }
    }
}


#Program.cs


using System;
using System.Collections.Generic;

namespace Lession4
{
    class Program
    {
        static void Main(string[] args)
        {
            testOOP();
        }

        static void testOOP() {
            People people = new People();
            people.Fullname = "Tran Van Diep";

            //people.Address = "Ha Noi";
            people.Cmtnd = "123";

            Console.WriteLine(people.Fullname);
            Console.WriteLine(people.Cmtnd);

            people.Input();
            people.Display();
        }
    }
}


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

5

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