1. Đầu tiên ta hãy xem một đoạn code hướng thủ tục về nhập xuất dữ liệu
Sub Main()
Dim hoten As String
Dim diemtoan As Single
Dim diemvan As Single
Dim dtb As Single
Dim hoten As String
Dim diemtoan As Single
Dim diemvan As Single
Dim dtb As Single
Console.WriteLine("Nhap vao ten cua ban ")
hoten = Console.ReadLine()
Console.WriteLine("Nhap vao diem toan")
diemtoan = Console.ReadLine()
Console.WriteLine("nhap vao diem van")
diemvan = Console.ReadLine()
dtb = (diemtoan + diemvan) / 2
Console.WriteLine("ten cua ban {0}, diem toan {1}, diem van {2}, diem trung binh {3}", hoten, diemtoan, diemvan, dtb)
Console.ReadLine()
End Sub
hoten = Console.ReadLine()
Console.WriteLine("Nhap vao diem toan")
diemtoan = Console.ReadLine()
Console.WriteLine("nhap vao diem van")
diemvan = Console.ReadLine()
dtb = (diemtoan + diemvan) / 2
Console.WriteLine("ten cua ban {0}, diem toan {1}, diem van {2}, diem trung binh {3}", hoten, diemtoan, diemvan, dtb)
Console.ReadLine()
End Sub
--------------------------------------------------------------------------------------------------
2. Code Lập trình hướng đơn thể
---------------------------------------------------------------------------
Sub Main()
Dim hs As New CauTrucHocSinh
Nhap(hs)
TinhDiemTB(hs)
Xuat(hs)
Console.ReadKey()
End Sub
----------------------------------------------------------------------------
Structure CauTrucHocSinh
Dim Hoten As String
Dim DiemToan As Double
Dim DiemVan As Double
Dim dtb As Double
End Structure
Sub Nhap(ByRef hs As CauTrucHocSinh)
Console.WriteLine("Nhap vao ten cua ban ")
hs.Hoten = Console.ReadLine()
Console.WriteLine("Nhap vao diem toan")
hs.DiemToan = Console.ReadLine()
Console.WriteLine("nhap vao diem van")
hs.DiemVan = Console.ReadLine()
End Sub
Sub Main()
Dim hs As New CauTrucHocSinh
Nhap(hs)
TinhDiemTB(hs)
Xuat(hs)
Console.ReadKey()
End Sub
----------------------------------------------------------------------------
Structure CauTrucHocSinh
Dim Hoten As String
Dim DiemToan As Double
Dim DiemVan As Double
Dim dtb As Double
End Structure
Sub Nhap(ByRef hs As CauTrucHocSinh)
Console.WriteLine("Nhap vao ten cua ban ")
hs.Hoten = Console.ReadLine()
Console.WriteLine("Nhap vao diem toan")
hs.DiemToan = Console.ReadLine()
Console.WriteLine("nhap vao diem van")
hs.DiemVan = Console.ReadLine()
End Sub
Sub TinhDiemTB(ByRef hs As CauTrucHocSinh)
hs.dtb = (hs.DiemToan + hs.DiemVan) / 2
End Sub
hs.dtb = (hs.DiemToan + hs.DiemVan) / 2
End Sub
Sub Xuat(ByRef hs As CauTrucHocSinh)
Console.WriteLine("ten cua ban {0}, diem toan {1}, diem van {2}, diem trung binh {3}", hs.Hoten, hs.DiemToan, hs.DiemVan, hs.dtb)
End Sub
Console.WriteLine("ten cua ban {0}, diem toan {1}, diem van {2}, diem trung binh {3}", hs.Hoten, hs.DiemToan, hs.DiemVan, hs.dtb)
End Sub
---------------------------------------------------------------------------------------------------
3. Lập trình hướng đối tượng
------------------------------------------
Sub Main()
Dim hs As New HocSinh
hs.Nhap()
hs.TinhDTB()
hs.Xuat()
Console.ReadKey()
End Sub
--------------------------------------------
Public Class HocSinh
Private _Hoten As String
Private _diemtoan As Double
Private _diemvan As Double
Private _dtb As Double
Public Sub Nhap()
Console.WriteLine("Nhap vao ten cua ban ")
_Hoten = Console.ReadLine()
Console.WriteLine("Nhap vao diem toan")
_diemtoan = Console.ReadLine()
Console.WriteLine("nhap vao diem van")
_diemvan = Console.ReadLine()
End Sub
Public Sub TinhDTB()
_dtb = (_diemtoan + _diemvan) / 2
End Sub
Sub Main()
Dim hs As New HocSinh
hs.Nhap()
hs.TinhDTB()
hs.Xuat()
Console.ReadKey()
End Sub
--------------------------------------------
Public Class HocSinh
Private _Hoten As String
Private _diemtoan As Double
Private _diemvan As Double
Private _dtb As Double
Public Sub Nhap()
Console.WriteLine("Nhap vao ten cua ban ")
_Hoten = Console.ReadLine()
Console.WriteLine("Nhap vao diem toan")
_diemtoan = Console.ReadLine()
Console.WriteLine("nhap vao diem van")
_diemvan = Console.ReadLine()
End Sub
Public Sub TinhDTB()
_dtb = (_diemtoan + _diemvan) / 2
End Sub
Public Sub Xuat()
Console.WriteLine("ten cua ban {0}, diem toan {1}, diem van {2}, diem trung binh {3}", _Hoten, _diemtoan, _diemvan, _dtb)
End Sub
End Class
Console.WriteLine("ten cua ban {0}, diem toan {1}, diem van {2}, diem trung binh {3}", _Hoten, _diemtoan, _diemvan, _dtb)
End Sub
End Class
Đăng nhận xét