🎮 Cuộc phiêu lưu đóng gói
Học lập trình hướng đối tượng thông qua một trò chơi!
XP: 0/100
Mức độ:
Người mới bắt đầu
Cấp độ 1 – Đóng gói là gì
Đóng gói có nghĩa là:
- Nhóm các biến và phương thức lại với nhau trong một lớp.
- Bảo vệ dữ liệu bằng cách đặt các biến ở chế độ riêng tư.
- Truy cập dữ liệu thông qua các phương thức công khai được gọi là getters và setters.
lớp học sinh{
tên chuỗi riêng tư;
public void setName(String n){
tên=n;
}
Chuỗi công khai getName(){
trả lại tên;
}
}
Quiz 1
Why do programmers use encapsulation?
To make everything public
To protect data and control access
To slow programs down
To protect data and control access
To slow programs down
Level 2 – Private Variables
Private variables cannot be accessed directly outside the class. Instead:
- Setter methods update them.
- Getter methods retrieve them.
private int age;
public void setAge(int a){
age=a;
}
public int getAge(){
return age;
}
Quiz 2
Which keyword hides variables?
public
private
static
private
static
Coding Challenge
Create a setter method called setName.
Final Boss Quiz
Encapsulation combines…
Variables and methods into one class
Loops and arrays
Internet connections
Loops and arrays
Internet connections
🏆 Congratulations!
You Mastered Encapsulation!
Enter your name:
