Tạo bởi Trắc Nghiệm Việt|
[Share Code] Tìm hiểu về mảng trong C# - Array in C# - Class Object in C# - Lớp đối tượng trong C# - Lập trình C#
C Sharp
[Share Code] Tìm hiểu về mảng trong C# - Array in C# - Class Object in C# - Lớp đối tượng trong C# - Lập trình C#
[Share Code] Tìm hiểu về mảng trong C# - Array in C# - Class Object in C# - Lớp đối tượng trong C# - Lập trình C#
Viết chương trình quản thư viên
- Book:
Thuộc tính
- BookCode
- Title
- AuthorName
- Price
- Nxb
Hành động (Method)
- Hàm nhập & hiển thị
- Account
Thuộc tính
- UserName
- Email
- Password
Hành động (Method)
- Hàm nhập & hiển thị
#Program.cs
using System;
using Lesson02.Modals;
using System.Collections.Generic;
namespace Lesson02
{
class MainClass
{
public static void Main(string[] args)
{
//learnArray();
Book book = new Book();
//book.BookCode = "Xin Chao";
book.SetBookCode("OKOKOK");
book.Title = "Xin chao";
book.Price = -1000;
book.Display();
Book book2 = new Book("B01", "Lap Trinh C", "ABC", 100000, "Aptech");
book2.Display();
}
static void learnArray()
{
Console.WriteLine("Learn Array in C#!");
// Phan 1: Tim hieu mang index datatype[] vars -> fix length cua array.
// Khai bao mang rong -> gom 5 so nguyen trong C#
int[] t = new int[5];
float[] f = new float[5];
string[] s = new string[5];
String[] ss = new String[5];
// Them gia tri vao trong mang
t[0] = 5;
Console.WriteLine("Nhap gia tri t[1] = ");
t[1] = Convert.ToInt32(Console.ReadLine());
//t[1] = Int32.Parse(Console.ReadLine())
for (int i = 2; i < 5; i++)
{
Console.WriteLine("Nhap gia tri t[{0}] = ", i);
t[i] = Convert.ToInt32(Console.ReadLine());
}
//Hien thi du lieu ra
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Nhap gia tri t[{0}] = {1}", i, t[i]);
}
// Phan 2: Khai bao mang gom cac phan tu co san
int[] k = { 3, 5, 9, 10, 2 };
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Nhap gia tri k[{0}] = {1}", i, k[i]);
}
// Phan 3: Khai bang mang dynamic trong C#
//int -> float, double, string, String, Class Object, ...
List<int> t1 = new List<int>();
//length = t1.Count
//Them phan tu vao trong mang
t1.Add(32);
t1.Add(7);
t1.Add(7);
t1.Add(7);
t1.Add(88);
//Lay cac phan tu trong mang
for (int i = 0; i < t1.Count; i++)
{
Console.WriteLine("Nhap gia tri t1[{0}] = {1}", i, t1[i]);
}
foreach (int v in t1)
{
Console.WriteLine("Nhap gia tri: {0}", v);
}
//Sua du lieu
t1[0] = 111;
for (int i = 0; i < t1.Count; i++)
{
Console.WriteLine("Nhap gia tri t1[{0}] = {1}", i, t1[i]);
}
//Xoa 1 phan tu trong mang
//t1.RemoveAt(0);
t1.Remove(7);
for (int i = 0; i < t1.Count; i++)
{
Console.WriteLine("Nhap gia tri t1[{0}] = {1}", i, t1[i]);
}
}
}
}
#Book.cs
using System;
namespace Lesson02.Modals
{
public class Book
{
//private, protected, public, frienly
//Phan 1: Tim hieu getter/setter
string BookCode;
//Viet theo style cua java -> trong C# ko su dung cach nay.
public void SetBookCode(string BookCode)
{
this.BookCode = BookCode;
}
public string GetBookCode()
{
return this.BookCode;
}
//Viet tuong minh
private string __Title2;
public string Title2
{
get
{
return __Title2;
}
set
{
__Title2 = value;
}
}
//Rut gon -> get/set -> public
public string Title { get; set; }
//get -> public, set -> private
public string AuthorName { get; private set; }
//get -> private, set -> public
public string Nxb { private get; set; }
//price > 0 -> khong co gia am.
private float _Price;
public float Price {
//private get
get
{
return _Price;
}
//private set
set
{
if(value <= 0)
{
Console.WriteLine("Price khong duoc phep am!!!");
} else
{
_Price = value;
}
}
}
public Book()
{
}
public Book(string BookCode, string Title, string AuthorName, float Price, string Nxb)
{
this.BookCode = BookCode;
this.AuthorName = AuthorName;
this.Title = Title;
this.Price = Price;
this.Nxb = Nxb;
}
public void Input()
{
Console.WriteLine("Input...");
}
public void Display()
{
Console.WriteLine("Display... {0} - {1} - Price: {2}", Title, BookCode, Price);
}
}
}
#Account.cs
using System;
namespace Lesson02.Modals
{
public class Account
{
public Account()
{
}
}
}