Chào mọi người, cho mình hỏi là : Khi Truyền tham số cho Phương Thức là 1 Tên của Class, Vậy mục đích và ý nghĩa của việc này là gì ạ.
Truyền tham số cho phương thức trong OOP
Bạn nên nêu rõ ngôn ngữ và code mẫu.
Động đến Type name thì chắc là Generic programming rồi :V
// đây bạn
public class Student {
private int mCode;
private String mName;
private int mAge;
private boolean mSex;
public Student() {}
public void Output(Student st) {
}
}
Cái này là constructor
, bạn tìm hiểu thêm về phần này nhé :))
public void Output(Student st) {
}
bạn ơi, cái mình hỏi là truyền tham số Student st cho phương thức Output kìa. Mình có hỏi phương thức khởi tạo đâu ?
method Output có gì bên trong? nhưng mình ngĩ là để change properties của parameter st.
Cái này thì tùy vào method Output
của bạn làm gì chứ :))
Nhưng cái lớp này không ổn đấy, đáng lẽ phương thức Output()
không nên có tham số Student st
. Nó nên dùng đối tượng hiện tại (this
).
Đã biết Output() làm gì đâu mà bảo là không ổn.
Nhỡ đâu đó là tính năng “làm bài tập hộ” thì sao :))
Bạn đọc tên hàm là Output thì mình nghĩ bạn cũng đoán ra nó làm gì rồi chứ :)). Hàm này xuất các thông tin của Student đấy. Vd như xuất tên, tuổi, giới tính của học sinh. Đầu vào là 1 list student nhé.
Thế thì có 2 cách:
public void Output()//st.Output(), this.Output()...
hoặc
public static void Output(Student st)//Student.Output(st)
Chứ không khai báo như bạn.
Đầu vào của bạn đang 1 Student :))
Vậy thì mục đích là truyền vào tham số Student st để in thông tin ra đó :))
Nếu là danh sách của sinh viên thì nên có lớp riêng quản lý danh sách đó. Tự nhiên cho 1 sinh viên bất kì quản lý 1 danh sách sinh viên.
Cho biết kiểu của tham số cần truyền.
// Đây mình gửi full code của thầy nha.
public class Student {
private int mStudentCode;
private String mStudentName;
private int mStudentAge;
private boolean mStudentSex;
public Student() {}
public Student(int mStudentCode, String mStudentName, int mStudentAge, boolean mStudentSex) {
super();
this.mStudentCode = mStudentCode;
this.mStudentName = mStudentName;
this.mStudentAge = mStudentAge;
this.mStudentSex = mStudentSex;
}
public int getmStudentCode() {
return mStudentCode;
}
public void setmStudentCode(int mStudentCode) {
this.mStudentCode = mStudentCode;
}
public String getmStudentName() {
return mStudentName;
}
public void setmStudentName(String mStudentName) {
this.mStudentName = mStudentName;
}
public int getmStudentAge() {
return mStudentAge;
}
public void setmStudentAge(int mStudentAge) {
this.mStudentAge = mStudentAge;
}
public boolean ismStudentSex() {
return mStudentSex;
}
public void setmStudentSex(boolean mStudentSex) {
this.mStudentSex = mStudentSex;
}
public void outPut() {
System.out.println("Student Information ");
System.out.println("FullName "+ mStudentName);
System.out.println("AGE "+ mStudentAge);
System.out.println("SEX "+ (this.mStudentSex ? "BOY" : "Girl"));
}
public void outPut(Student st) {
System.out.println("Student Information ");
System.out.println("FullName "+ st.getmStudentName());
System.out.println("AGE "+ st.getmStudentAge());
System.out.println("SEX "+ (st.mStudentSex ? "BOY" : "Girl"));
}
public void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Please input for the Information Student ");
System.out.println("Code ");
this.mStudentCode = sc.nextInt();
System.out.println("FullName ");
this.mStudentName = sc.next();
sc.nextLine();
System.out.println("Age ");
this.mStudentAge = sc.nextInt();
System.out.println("Sex");
int sex = sc.nextInt();
if(sex == 1) {
this.mStudentSex = true;
} else {
this.mStudentSex = false;
}
}
public class RunMain {
private static List<Student> mList = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Please value n students ");
int n = sc.nextInt();
Student out = new Student();
for(int i = 0; i <n ;i++) {
Student st = new Student();
st.input();
mList.add(st);
}
// for(int i = 0 ; i< mList.size() ; i++) {
// out.outPut(mList.get(i));
// }
for (Student st : mList) {
out.outPut(st);
}
}
Sao lại có chuyện output dùm vậy cái đó ko phải là OOP.
Chắc cái “thằng” out
đó nó có “tay chân” gì to lắm đấy!
Không chừng vừa là Student
vừa là con của thầy.
Mình nghĩ code thầy thiếu chữ “static”.
À không. Kể cả thế, thầy giáo cũng không thể code sida thế được.
public static void outPut(Student st)
{
if(st == null)
System.out.println("null");
else
st.outPut();
}
hành vi output theo OOP thì nên là của bản thân class student,
nghĩa là thisStudent.output() ổn hơn là thisStudent.output(otherStudent).
sử dụng theo code của bạn thì giống như việc học sinh A có quyền nhấn chuông điểm danh dùm học sinh B,
dẫn tới việc khi có tín hiệu điểm danh của B thì thầy giáo không biết A hay chính B đã nhấn cái chuông đó, hay C, E, F hay một em Z nào đó đã táy máy cái chuông của B?