Trang Chủ | Diễn Đàn | Thành Viên (Đăng Ký) | Tìm Kiếm | Tutorial Room
C#.NET - thac mac ve method va prooerty trong C# ???
Hỏi đáp, trao đổi về C#. Các câu hỏi về ngôn ngữ C/C++C/C++ for Windows xin hãy gởi vào các box tương ứng!
First page Previous page  (Page 1 )   1     2   Next page Last page
thac mac ve method va prooerty trong C# ???
Member
Member since
13:46 24-05-2008
Posts: 4
Fantasy Points: 20
Rank
question Posted at 02:15 10-03-2010 Move Move Topic   Pin/Unpin Pin Topic   Lock Lock Topic
minh mới tìm hiểu về c# nên có 1 số vấn đề không hiểu hy vọng các pac chỉ giáo.

trong đoạn code này mình không hiểu mấy cái method và property trong class Student này để làm gì, khi mình xóa nó đi thi chương trình vẫn chạy đúng được. xin giải thích dùm mình chức năng của nó. thank

voi lai mình khai báo DiemTB la float nhưng nhập kieu so thuc lai khong được,mình muốn nhập số thực thì làm cách nào.

[code]

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

namespace btth_cua_thay_giang
{
public class Student
{
public string TenSV;
public int MSSV;
public string Khoa;//..,
public float DiemTB;//...

//ham ko co tham so
/* public Student()
{
MSSV = 1;
TenSV = "Balaxanh";
Khoa = "CNTT";
DiemTB = 4;
}

//ham sao chep
public Student(Student stu)
{
MSSV = stu.MSSV;
TenSV = stu.TenSV;
Khoa = stu.Khoa;
DiemTB = stu.DiemTB;
}
//ham co tham so
public Student(int id, string ten, string khoa, float diem)
{
MSSV = id;
TenSV = ten;
Khoa = khoa;
DiemTB = diem;
}
//tao cac property cho tung du lieu thanh vien cua lop
//danh sac property
public int ID
{
get //lay du lieu
{
return MSSV;
}
set//gan du lieu
{
MSSV = value;
}
}
public string NAME
{
get
{
return TenSV;
}
set
{
TenSV = value;
}
}
public string KHOA
{
get
{
return Khoa;//lay gia tri
}
set
{
Khoa = value;
}
}
public float DIEMTB//ten property
{
get
{
return DiemTB;
}
set
{
DiemTB = value;//gan gia tri
}
}
*/
public void show()
{
Console.WriteLine("MSSV: {0,6}", this.MSSV);
Console.WriteLine("Ten SV: {0,6}", this.TenSV);
Console.WriteLine("Khoa :{0,6}", this.Khoa);
Console.WriteLine("Diem TB: {0,6}", this.DiemTB);
}


}
class Teste
{
public static void Main()
{
int n;
Console.WriteLine("Moi ban nhap vao so luong sinh vien: ");
n = int.Parse(Console.ReadLine());
Student[] StudentList = new Student[n];//tao mang sinh vien co n phan tu
for (int i = 0; i < n; i++)
{
StudentList[i] = new Student();//tao doi tuong sinh vien
Console.WriteLine("Nhap ten sinh vien thu {0}", i);
StudentList[i].TenSV = Console.ReadLine();
Console.WriteLine("MSSV: ");
StudentList[i].MSSV = int.Parse(Console.ReadLine());
Console.WriteLine("Khoa: ");
StudentList[i].Khoa = Console.ReadLine();
Console.WriteLine("DiemTB: ");
StudentList[i].DiemTB = int.Parse(Console.ReadLine());

}
//xuat danh sach sinh vien
Console.WriteLine("*********DANH SACH SINH VIEN********");
foreach (Student st in StudentList)

st.show();
Console.ReadLine();
}
}
}


[/code]
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
Re: thắc mắc về method và property trong C# ???
Member
Member since
00:40 28-11-2009
Posts: 757
Fantasy Points: 2,649
Rank
answer Posted at 03:02 10-03-2010
Reply to thac mac ve method va prooerty trong C# ??? (balaxanh27)
1. Mấy cái Get Let Set đó là dùng để truy sát (ý lộn, truy xuất) gián tiếp vào mấy cái private. Đã public rùi thì tha hồ sửa.
2. Nhập float ở chỗ nào bị error vậy???
Trên đời nghìn vạn điều cay đắng
Cay đắng chi bằng học môn văn
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,769 time(s). 1 direct repli(es) and 1 indirect repli(es).
Title Poster
answer Re: thắc mắc về method và property trong C# ???
 
answer Re: thắc mắc về method và property trong C# ???
balaxanh27
Re: thắc mắc về method và property trong C# ???
Member
Member since
13:46 24-05-2008
Posts: 4
Fantasy Points: 20
Rank
answer Posted at 03:16 10-03-2010
Reply to Re: thắc mắc về method và property trong C# ??? (bigbelly)
o DiemTB áh, pạn nói rõ hơn duoc hem, public rồi thì tha hồ sữa ?? pubic chỗ nào ? chỗ class Student hay chỗ mấy cái biến MSSV,TenSV..vay
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,766 time(s). 1 direct repli(es) and 0 indirect repli(es).
Title Poster
answer Re: thắc mắc về method và property trong C# ???
 
answer Re: thắc mắc về method và property trong C# ???
bigbelly
Re: thắc mắc về method và property trong C# ???
Member
Member since
00:40 28-11-2009
Posts: 757
Fantasy Points: 2,649
Rank
answer Posted at 03:27 10-03-2010
Reply to Re: thắc mắc về method và property trong C# ??? (balaxanh27)
balaxanh27:
public Student()
{
MSSV = 1;
TenSV = "Balaxanh";
Khoa = "CNTT";
DiemTB = 4;
}


Chữ j màu ĐỎ thế kia???

balaxanh27:

for (int i = 0; i < n; i++)
{
StudentList[i] = new Student();//tao doi tuong sinh vien
Console.WriteLine("Nhap ten sinh vien thu {0}", i);
StudentList[i].TenSV = Console.ReadLine();
Console.WriteLine("MSSV: ");
StudentList[i].MSSV = int.Parse(Console.ReadLine());
Console.WriteLine("Khoa: ");
StudentList[i].Khoa = Console.ReadLine();
Console.WriteLine("DiemTB: ");
StudentList[i].DiemTB = int.Parse(Console.ReadLine());
}


Muốn code ăn float thì phải float.parse chứ, int.parse chỉ ăn integer thui.
Trên đời nghìn vạn điều cay đắng
Cay đắng chi bằng học môn văn
This post has been edited 1 time(s). Last edited by bigbelly on 03:28 10-03-2010.
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,762 time(s). 0 direct repli(es) and 0 indirect repli(es).
Re: thac mac ve method va prooerty trong C# ???
Member
Member since
23:09 08-03-2010
Posts: 35
Fantasy Points: 123
Rank
answer Posted at 23:49 10-03-2010
Reply to thac mac ve method va prooerty trong C# ??? (balaxanh27)
Đoạn code này bạn lấy ở đâu vậy?
Để giải thich, cần phải đặt ra hai giả thuyết

Giả thuyết thứ nhất:
Người viết đã viết đúng theo nhiệm vụ của property (một ý niệm do Microsoft đặt ra cho C# và VB.NET), tức là che dấu fields/data members (các biến) đi, và dùng properties để truy xuất/nhập.

Đương nhiên theo đúng code trên thì khi bạn loại bỏ các properties đi code của bạn vẫn compile vì nó chỉ đề cập tới public members của lớp thôi chứ đâu có đụng vào private members.

Theo giả thuyết này thì để viết cho được đúng, các biến phải được đặt là private (dấu, không cho truy xuất/nhập trực tiếp)
private string TenSV;
private int MSSV;
private string Khoa;//..,
private float DiemTB;//...
Khi ấy, code của bạn bắt buộc phải dùng các properties, vì các fields đã bị giấu (privatised) rồi

Giả thuyết thứ hai:
Người viết muốn lợi dụng đặc điểm properties của C# để viết một lớp mà members có thể truy xuất/nhập bằng cả tiếng Việt lẫn tiếng Anh. Tức là các fields thì đặt tên tiếng Việt còn các properties thì đặt tên tiếng Anh (đáng lẽ KHOA phải đặt là Faculty và DIEMTB phải là AverageMark). Nếu đúng ý như thế thì các data members public như trên là đúng rồi.

Tuy nhiên, viết như thế là không theo đúng tinh thần data hiding (trị biến ẩn) của lập trình hướng đối tượng (!). Người viết chuyên nghiệp vẫn cho đám fields trên là private
private string _TenSV;
private int _MSSV;
private string _Khoa;//..,
private float _DiemTB;//... (chú ý gạch trước mỗi tên)
Xong, viết mấy cái properties:
public string TenSV { get { return _TenSV; } set { _TenSV = value; } }
public int MSSV { get… set… }
vân vân…
Lưu ý: các properties tiếng Anh, và các constructors có thể để nguyên, không cần phải chỉnh lại để dùng các biến số (có gạch trước tên) vì chúng có thể gọi các properties tiếng Việt để làm việc.

** Update: chữ Khoa là Faculty hoặc School, lúc đầu hiểu lầm là Khoá nên dịch là StudySet.
This post has been edited 1 time(s). Last edited by megaownage on 00:57 11-03-2010.
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,724 time(s). 1 direct repli(es) and 2 indirect repli(es).
Title Poster
answer Re: thac mac ve method va prooerty trong C# ???
 
answer Re: thắc mắc về method va property trong C# ???
bigbelly
Re: thac mac ve method va prooerty trong C# ???
Member
Member since
23:09 08-03-2010
Posts: 35
Fantasy Points: 123
Rank
answer Posted at 00:47 11-03-2010
Reply to thac mac ve method va prooerty trong C# ??? (balaxanh27)
Trả lời câu hỏi các public methods:

Student() là default constructor – tức là hiển thể (object instantiation) bằng trị số mặc định. Thường thì người ta không viết như thế, mà gọi cái full constructor như thế này:
public Student() : this(1, "Balaxanh", "CNTT", 3.4 ) { }

Student(int id, string ten, string khoa, float diem) là full constructor.

Student(Student stu) là copy constructor – dùng để copy nguyên chang thay vì chỉ cái pointer vô cái đối tượng (object). Khác với C++, C# không dùng pointers nên khái niệm deep copy và shallow copy không quan trọng lắm. (Có pointers nhưng chỉ trường hợp rất đặc biệt mới dùng tới)
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,719 time(s). 0 direct repli(es) and 0 indirect repli(es).
Re: thắc mắc về method va property trong C# ???
Member
Member since
00:40 28-11-2009
Posts: 757
Fantasy Points: 2,649
Rank
answer Posted at 06:35 11-03-2010
Reply to Re: thac mac ve method va prooerty trong C# ??? (megaownage)
Tui cũng có học sơ sơ về lập trình hướng đối tượng nhưng cũng chả hju property và method dùng làm j. Tại sao ko để public và sửa trong đó nhỉ?
Trên đời nghìn vạn điều cay đắng
Cay đắng chi bằng học môn văn
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,708 time(s). 1 direct repli(es) and 1 indirect repli(es).
Title Poster
answer Re: thắc mắc về method va property trong C# ???
 
answer Re: thắc mắc về method va property trong C# ???
phuongt
Re: thắc mắc về method va property trong C# ???
Member
Member since
08:30 04-11-2003
Posts: 1,755
Fantasy Points: 18,378
Rank
answer Posted at 10:34 11-03-2010
Reply to Re: thắc mắc về method va property trong C# ??? (bigbelly)
bigbelly:
Tui cũng có học sơ sơ về lập trình hướng đối tượng nhưng cũng chả hju property và method dùng làm j. Tại sao ko để public và sửa trong đó nhỉ?


Nếu tui muốn khi property thay đổi thì phải programme biết để kiểm tra có dc thay đổi kô.
Với property thì OK, nếu mà dùng public vairable thì làm sao mà generate cái event đây.

Nếu cho nó là public member of base class, thì mấy cái inherit class phải làm sao đây?
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 2,693 time(s). 1 direct repli(es) and 0 indirect repli(es).
Title Poster
answer Re: thắc mắc về method va property trong C# ???
 
answer Re: thắc mắc về method va property trong C# ???
megaownage
First page Previous page  (Page 1 )   1     2   Next page Last page

Permissions: Create Topic: No  |  Reply Topic: No  |  Attach File: No  |  Make Poll: No

Vietnamese Keyboard: AUTO TELEX VNI VIQR VIQR* OFF

Go top || Print page ||

All logos, trademarks and graphics artwork in this site are property of their respective owners.
Opinions expressed in articles within this site are those of their owners and may not reflect the opinion of TXBB.

TXBB: Home - Disclaimer - Help - Contact
Copyright (C) 2000-2006 TXBB. All rights reserved.

TreXanh Bulletin Board v2.0 (Build: #332 Nov 21, 2006)

DEBUG INFORMATION
Execution 0.257s - SQL used 6s - Concurrent process(es) 0