[ C# ] Một số ví dụ khai báo class và object trong C#

Một số ví dụ khai báo class và object trong C#


/*
Bài 1:  Xây dựng lớp
- nhập vào thông tin sinh viên
 + Họ tên
 + Năm sinh
 + Địa chỉ
 + Điểm kỳ 1, kỳ 2, kỳ 3
- Tính điểm tổng kết 
- In thông tin sinh viên

*/
// Code C#

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

namespace classSV
{
    // clas SV
    class SV
    {
        // thuoc tinh
        string ht,dc;
        int namSinh;
        float dk1, dk2, dk3;
        const float soTinChiKy1 = 26, soTinChiKy2 = 27, soTinChiKy3 = 30; // khai bao hang
        // Phuong thuc khoi tao
        public SV(string ht, string dc, int namSinh, float dk1, float dk2, float dk3)
        {
            this.ht = ht;
            this.dc = dc;
            this.namSinh = namSinh;
            this.dk1 = dk1;
            this.dk2 = dk2;
            this.dk3 = dk3;
        }
        // Nhap thong tin sv
        public void NhapTT()
        {
            Console.Write("\n Ho ten: ");
            ht = Console.ReadLine();
            Console.Write("\n Dia chi: ");
            dc = Console.ReadLine();
            Console.Write("\n Nam sinh: ");
            namSinh = Int32.Parse(Console.ReadLine());
            do
            {
                Console.Write("\n Diem hoc ky 1: ");
                dk1 = float.Parse(Console.ReadLine());
                if (dk1 < 0 || dk1 > 10)
                    Console.Write("\n Nhap lai diem hoc ky 1: ");
            } while (dk1 < 0 || dk1 > 10);
            do
            {
                Console.Write("\n Diem hoc ky 2: ");
                dk2 = float.Parse(Console.ReadLine());
                if (dk2 < 0 || dk2 > 10)
                    Console.Write("\n Nhap lai diem hoc ky 2: ");
            } while (dk2 < 0 || dk2 > 10);
            do
            {
                Console.Write("\n Diem hoc ky 3: ");
                dk3 = float.Parse(Console.ReadLine());
                if (dk3 < 0 || dk3 > 10)
                    Console.Write("\n Nhap lai diem hoc ky 3: ");
            } while (dk3 < 0 || dk3 > 10);
        }
        // Tinh diem
        public float TinhDiem()
        {
            return (dk1*soTinChiKy1+dk2*soTinChiKy2+dk3*soTinChiKy3)/(soTinChiKy1+soTinChiKy2+soTinChiKy3);
        }
        // In thong tin sinh vien
        public void InTT()
        {
            // tim diem chu
            char diemChu;
            float dtk=TinhDiem();
            if (dtk < 4) diemChu = 'F';
            else if (dtk < 5.5) diemChu = 'D';
            else if (dtk < 6.5) diemChu = 'C';
            else if (dtk < 8.5) diemChu = 'B';
            else diemChu = 'A';
            // in thong tin
            Console.Write("\n THONG TIN CUA SINH VIEN:");
            Console.Write("\n - Ho ten:{0}\n - Nam sinh: {1}\n - Dia chi: {2}\n - Diem tong ket: {3}\n - Diem chu: {4}",ht,namSinh,dc,dtk,diemChu);
        }
    }

// class program
 class Program
    {
        static void Main(string[] args)
        {
            SV t = new SV("", "",0, 0, 0, 0);
            t.NhapTT();
            t.InTT();
            Console.ReadLine();
        }
    }
}



//------------------------------------------------------------------------------------------------

/*
Bài 2:  Xây dựng lớp giải phương trình bậc 2 một ẩn (ax^2 + bx + c = 0)

*/
// code C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PTB2
{
    // clas PhuongTrinh
    class PhuongTrinh
    {
        // thuoc tinh a,b,c
        float a, b, c;
        //phuong thuc khoi tao
        public PhuongTrinh(float a,float b,float c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        // phuong thuc nhap
        public void Nhap()
        {
            Console.Write(" a= ");
            a = float.Parse(Console.ReadLine());
            Console.Write(" b= ");
            b = float.Parse(Console.ReadLine());
            Console.Write(" c= ");
            c = float.Parse(Console.ReadLine());
        }
        // Phuong thuc bien luan
        public void BienLuan()
        {
            if (a == 0)
                if (b == 0)
                    if (c == 0)
                        Console.Write("\n Phuong trinh vo so nghiem");
                    else
                        Console.Write("\n Phuong trinh vo nghiem");
                else
                    Console.Write("\n Phuong trinh co nghiem x={0}", -c / b);
            else
            {
                float d = b * b - 4 * a * c;
                if(d<0)
                    Console.Write("\n Phuong trinh vo nghiem");
                if (d==0)
                    Console.Write("\n Phuong trinh co nghiem kep x1=x2= {0}",-b/(2*a));
                if(d>0)
                    Console.Write("\n Phuong trinh co 2 nghiem x1= {0}; x2= {1}", (-b - Math.Sqrt(d)) / (2 * a), (-b + Math.Sqrt(d)) / (2 * a));
            }
        }
    }

// class program
class Program
    {
        static void Main(string[] args)
        {
            // khai bao va khoi tao doi tuong t
            PhuongTrinh t = new PhuongTrinh(0, 0, 0);
            t.Nhap();
            t.BienLuan();
            Console.ReadLine();
        }
    }
}

//---------------------------------------------------------------------------------------

Chú ý: Để chạy chương trình, bạn tạo Project C# (console Application), đặt tên là trùng với tên namespace sau đó copy code paste vào program.cs.

Tìm kiếm nội dung khác: