By GokiSoft.com|
20:08 26/10/2021|
C Sharp
[Share Code] Overview kiến thức OOP C#
OOP:
4 T/c trong lập trình hướng đối tượng
Interface
Overloading
Delegate & Event
Exception
#Program.cs
using System;
namespace Lesson11
{
delegate void ShowInfo();
class Program
{
static void Main(string[] args)
{
//Test01();
//Test02();
Test03();
}
static event ShowInfo showEvent;
static void Test03()
{
//Test delegate
ShowInfo show = TestEvent;
show();
//Amator
showEvent = TestEvent;
showEvent();
}
static void TestEvent()
{
Console.WriteLine("ok 1.....");
Console.WriteLine("ok 1.....");
Console.WriteLine("ok 1.....");
Console.WriteLine("ok 1.....");
Console.WriteLine("ok 1.....");
Console.WriteLine("ok 1.....");
Console.WriteLine("ok 1.....");
}
//Test -> Exception
static void Test02()
{
//Tao doi tuong sinh vien yeu cau nhu sau:
//Email -> chua ky tu @ -> khong co -> NotEmailException
//Gender -> Nam, Nu -> ko phai -> NotGenderException
//Test -> NotEmailException -> Sai dia chi email
//Test -> NotGenderException -> sai gioi tinh
//Test -> Nhap du lieu chinh xac.
Student student = new Student();
try
{
student.Fullname = "ABC";
student.Email = "tranvandiep.it@gmail.com";
student.Gender = "NAam";
student.Display();
} catch(NotGenderException e)
{
Console.WriteLine("Sai gioi tinh : " + e.Message);
} catch(NotEmailException e)
{
Console.WriteLine("Sai dia chi email");
}
}
//Test -> partial
static void Test01()
{
Student student = new Student();
student.Fullname = "ABC";
student.Birthday = "1990";
student.Display();
student.Input();
}
}
}
#Student.cs
using System;
namespace Lesson11
{
public partial class Student
{
public string Fullname { get; set; }
private string _email;
public string Email {
get
{
return _email;
}
set
{
if(!value.Contains("@"))
{
//Throw exception
throw new NotEmailException();
}
_email = value;
}
}
public Student()
{
}
public Student(string fullname, string email, string phone, string gender, string birthday)
{
Fullname = fullname;
Email = email;
Phone = phone;
Gender = gender;
Birthday = birthday;
}
public void Input()
{
Console.WriteLine("Nhap du lieu...");
}
private void ShowTest()
{
Console.WriteLine("okok....");
}
}
}
#Student01.cs
using System;
namespace Lesson11
{
public partial class Student
{
public string Phone { get; set; }
private string _gender;
public string Gender
{
get
{
return _gender;
}
set
{
if(value != "Nam" && value != "Nu")
{
//Throw exception
throw new NotGenderException("Nhap sai gioi tinh ...");
}
_gender = value;
}
}
public string Birthday { get; set; }
public virtual void Display()
{
Console.WriteLine("Ten: {0}, email: {1}, gioi tinh: {2}", Fullname, Email, Gender);
}
}
}
#Student02.cs
using System;
namespace Lesson11
{
public class Student02 : Student
{
public Student02()
{
}
public override void Display()
{
base.Display();
}
public override string ToString()
{
return base.ToString();
}
}
}
#NotEmailException.cs
using System;
namespace Lesson11
{
public class NotEmailException : Exception
{
public NotEmailException()
{
}
}
}
#NotGenderException.cs
using System;
namespace Lesson11
{
public class NotGenderException : Exception
{
public NotGenderException()
{
}
public NotGenderException(string msg):base(msg)
{
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)