Truyền tham số cho phương thức trong OOP

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ì ạ.

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

1 Like
// đâ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é :))

3 Likes
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.

2 Likes

Cái này thì tùy vào method Output của bạn làm gì chứ :))

3 Likes

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).

2 Likes

Đã 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 :))

4 Likes

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.

4 Likes

Đầ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 đó :))

3 Likes

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.

3 Likes

// Đâ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 :smiley: cái đó ko phải là OOP.

6 Likes

Chắc cái “thằng” out đó nó có “tay chân” gì to lắm đấy! :rofl:
Không chừng vừa là Student vừa là con của thầy. :smiling_imp:

3 Likes

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();
}
5 Likes

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?

6 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?