By GokiSoft.com|
15:14 23/09/2021|
C Sharp
[Share Code] Tìm hiểu về lập trình OOP tính chất bao đóng & kế thừa trong C# - Khoá học C# - C2009G2
Lập trình hướng đối tượng C#
- Tính chất đóng gói
- Tính chất kế thừa
- Tính chất đa hình
- Tích chất trựu tượng
- Interface
===================================================
Nội dung kiến thức:
Lập trình hướng đối tượng C#
- Tính chất đóng gói
- Tính chất kế thừa
Project Mini:
- Viết chương trình quản lý thông tin công dân, sinh viên. Tạo ra 2 đối tượng công dân và sinh viên -> test.
B1. Phân tích qua dự án
Công dân -> Citizen
Thuộc tính:
- fullname: string
- birthday: string
- address: string
- cmtnd: string
- gender: string
Phuong thuc:
- input -> Nhap du lieu
- display -> Hien thi du lieu
- Ham tao & getter/setter
Sinh vien -> Student
Thuộc tính:
- fullname: string
- birthday: string
- address: string
- cmtnd: string
- gender: string
- rollno: string
- email: string
Phuong thuc:
- input -> Nhap du lieu -> can nhap them 2 truong thong tin rollno & email
- display -> Hien thi du lieu -> hien thi du thong tin.
- Ham tao & getter/setter
Thuoc tinh truy cap:
- public
- protected
- private
- friendly (internal) -> default
Mot so luu y:
- Class Object -> Ko nen truy cap truc tiep thuoc cua class object -> namespace -> truy cap qua getter/setter
#Citizen.cs
using System;
namespace Lesson02
{
public class Citizen
{
/**string fullname;
//Neu hoc qua Java -> getter/setter cua Java -> nhu sau
public void setFullname(string fullname)
{
this.fullname = fullname;
}
public string getFullname()
{
return this.fullname;
}*/
public string Fullname { get; set; }
//Ket thuc overview getter/setter cua Java
//Luu y: Trong C# -> ko dung theo cach cua Java
//Trong C# -> Ten bien + getter/setter -> Su dung chung la 1.
public string Gender { get; set; }
public string Birthday { get; private set; }
public string Cmtnd { get; private set; }
//Birthday <-> Cmtnd => Nhu nhau.
public string Address { private get; set; }
//Diem cong
//Bai toan: yeu cau point >= 0
private int _point;
public int Point
{
get
{
return _point;
}
set
{
if(value <= 0)
{
Console.WriteLine("Point chi nhan gia tri >= 0");
return;
}
this._point = value;
}
}
public Citizen()
{
}
public Citizen(string fullname, string gender, string birthday, string cmtnd, string address, int point)
{
this.Fullname = fullname;
this.Gender = gender;
this.Birthday = birthday;
this.Cmtnd = cmtnd;
this.Address = address;
this.Point = point;
}
public virtual void Input()
{
Console.WriteLine("Nhap ten: ");
Fullname = Console.ReadLine();
Console.WriteLine("Nhap gioi tinh: ");
Gender = Console.ReadLine();
Console.WriteLine("Nhap ngay sinh: ");
Birthday = Console.ReadLine();
Console.WriteLine("Nhap CMTND: ");
Cmtnd = Console.ReadLine();
Console.WriteLine("Nhap dia chi: ");
Address = Console.ReadLine();
Console.WriteLine("Nhap diem: ");
Point = int.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine(ToString());
}
//public new string ToString => "Ho Ten: " + Fullname + ", gioi tinh: " + Gender + ", ngay sinh: " + Birthday + ", " +
//"cmtnd: " + Cmtnd + ", dia chi: " + Address + ", diem: " + Point;
public virtual string ToString()
{
return "Ho Ten: "+Fullname+ ", gioi tinh: " + Gender + ", ngay sinh: " + Birthday + ", " +
"cmtnd: " + Cmtnd + ", dia chi: " + Address + ", diem: " + Point;
}
}
}
#ObjectTest.cs
using System;
namespace Lesson02
{
public class ObjectTest
{
public int x;
private int y;
protected int z;
int k;
public ObjectTest()
{
x = 10;
y = 5;
z = 12;
k = 1;
}
public void ShowMsg(string msg)
{
Console.WriteLine(msg);
}
}
}
#Program.cs
using System;
namespace Lesson02
{
public class ObjectTest02
{
public int x;
private int y;
protected int z;
int k;
public ObjectTest02()
{
x = 10;
y = 5;
z = 12;
k = 1;
}
}
class Program
{
static void Main(string[] args)
{
//Test01();
//Test02();
Test03();
}
static void Test03()
{
Student std = new Student("R001", "a@gmail.com", "Tran Van A",
"Nam", "12/09/1980", "23234", "Ha Noi", 11);
std.Display();
Student std2 = new Student();
std2.Input();
std2.Display();
}
static void Test02()
{
ObjectTest t = new ObjectTest();
t.x = 12;
t.ShowMsg("23123asdasd");
ObjectTest02 t2 = new ObjectTest02();
t2.x = 56;
}
static void Test01()
{
Console.WriteLine("Tim Hieu Lap Trinh OOP Trong C#");
Citizen citizen = new Citizen();
//citizen.birthday = "20/06/1999";
//Console.WriteLine("Ngay sinh: {0}", citizen.birthday);
//citizen.setFullname("12345");
//Console.WriteLine("Ho & Ten: {0}", citizen.getFullname());
citizen.Gender = "Nam";
Console.WriteLine("Gioi Tinh: {0}", citizen.Gender);
//citizen.Birthday = "20/06/1999";
Console.WriteLine("Ngay sinh: {0}", citizen.Birthday);
citizen.Address = "Ha Noi";
//Console.WriteLine("Dia Chi: {0}", citizen.Address);
citizen.Point = -100;
Console.WriteLine("Diem cong dan: {0}", citizen.Point);
Citizen citizen1 = new Citizen("Tran Van A", "Nam", "12/10/1990", "123456789", "Ha Noi", 100);
citizen1.Display();
Citizen c2 = new Citizen();
c2.Input();
c2.Display();
}
}
}
#Student.cs
using System;
namespace Lesson02
{
public class Student : Citizen
{
public string Rollno { get; set; }
public string Email { get; set; }
public Student()
{
}
public Student(string rollno, string email,
string fullname, string gender,
string birthday, string cmtnd,
string address, int point):base(fullname, gender,
birthday, cmtnd, address, point)
{
Rollno = rollno;
Email = email;
}
public override string ToString()
{
return "Rollno: " + Rollno + ", Email: " + Email + ", " + base.ToString();
}
public override void Input()
{
base.Input();
Console.WriteLine("Nhap ma sinh vien: ");
Rollno = Console.ReadLine();
Console.WriteLine("Nhap email: ");
Email = Console.ReadLine();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)