Recent Posts

Code C#: Ví dụ đơn giản về lớp, thuộc tính, phương thức

-

Yêu cầu
1. Tạo lớp Student có các dữ liệu và phương thức sau: SID (mã sinh viên), tên sinh viên, khoa, điểm TB, 
2. Thêm các property cho các dữ liệu thành viên trên; 
3. Viết các phương thức hiển thị thông tin của sinh viên. 
4. Tạo lớp Tester, trong lớp này chỉ chứa duy nhất hàm Main(). Hàm cho phép người dùng nhập vào số n là số sinh viên, sau đó lần lượt tạo các đối tượng sinh viên và thêm vào danh sách sinh viên theo những thông tin do người dùng nhập vào. Xuất ra danh sách chi tiết thông tin sinh viên.

Mã nguồn:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace clsSinhVien
{
    public class Student
    {        
        private int SID;
        private string Ten;
        private string Khoa;
        private float Diem;
        public Student()
        {           
            SID = 1;
            Ten = "Nguyen Minh Ngoc";
            Khoa = "CNTT";
            Diem = 10;
        }
        public Student(Student st)
        {            
            this.SID = st.SID;
            this.Ten = st.Ten;
            this.Khoa = st.Khoa;
            this.Diem = st.Diem;
        }
        public Student(int id, string ten, string khoa, float diem)
        {
            this.SID = id;
            this.Ten = ten;
            this.Khoa = khoa;
            this.Diem = diem;
        }
        public int _SID
        {
            get { return SID; }
            set { SID = value; }
        }
        public string _Ten {
            get { return Ten; }
            set { Ten = value; }
        }
        public string _Khoa {
            get { return Khoa; }
            set { Khoa = value; }
        }
        public float _Diem{
            get { return Diem; }
            set { Diem = value; }
        }
        public void Show()
        {
            //Console.WriteLine("MSSV: {0}. Ten sinh vien: {1}. Khoa: {2}. Diem: {3}", this.SID, this.Ten,this.Khoa,this.Diem);
            Console.WriteLine("\t{0}\t {1}\t{2}\t{3}", this.SID, this.Ten, this.Khoa, this.Diem);
            //Console.WriteLine("Ten sinh vien: {0}", this.Ten);
            //Console.WriteLine("Khoa: {0}", this.Khoa);
            //Console.WriteLine("Diem: {0}", this.Diem);
            Console.WriteLine("------------------------------");
        }
    }
    public class Tester
    {
        public static void Main()
        {
            Student[] StudentList;
            int n;
            Console.Write("Nhap so luong sinh vien: ");
            n = int.Parse(Console.ReadLine());
            StudentList = new Student[n];    //tao mang co n phan tu
            for (int i = 0; i < n; i++)
            {
                StudentList[i] = new Student();         //Tao doi tuong sinh vien
                Console.Write("Nhap ma sinh vien {0}: ", i + 1);
                StudentList[i]._SID = int.Parse(Console.ReadLine());
                Console.Write("Nhap ten sinh vien {0}: ", i + 1);
                StudentList[i]._Ten = Console.ReadLine();
                Console.Write("Nhap Khoa: ");
                StudentList[i]._Khoa = Console.ReadLine();
                Console.Write("Nhap Diem: ");
                StudentList[i]._Diem = float.Parse(Console.ReadLine());
            }
            Console.WriteLine("-----DANH SACH SINH VIEN-----");

            Console.WriteLine("STT \t MSV \t Ten \t Khoa \t Diem");
            for (int i = 0; i < n;i++ )
            {
                Console.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}",i+1 ,StudentList[i]._SID, StudentList[i]._Ten, StudentList[i]._Khoa, StudentList[i]._Diem);
            }
                //foreach (Student st in StudentList)
                //{
                   
                //    st.Show();
                //}
            Console.ReadLine();
        }
    }
}
Tags: C++, C#, class, sinhvien, property, fields, method, sử dụng lớp trong C#




Klik untuk melihat kode: :) =( :s :D :-D ^:D ^o^ 7:( :Q :p T_T @@, :-a :W *fck* x@