C บทเรียนเกี่ยวกับความปลอดภัยทางไซเบอร์เล่ม 1

การผจญภัยแบบห่อหุ้ม

op การผจญภัยแบบห่อหุ้ม

เรียนรู้การเขียนโปรแกรมเชิงวัตถุผ่านเกม!

ประสบการณ์: 0/100

ระดับ: ระดับเริ่มต้น

ระดับ 1 – การห่อหุ้มคืออะไร

การห่อหุ้มหมายความว่า:

  • การรวมตัวแปรและวิธีการเข้าด้วยกันภายในคลาส
  • Protecting data by making variables private.
  • Accessing data through public methods called getters and setters.
class Student{ private String name; public void setName(String n){ name=n; } public String getName(){ return name; } }

Quiz 1

Why do programmers use encapsulation?

To make everything public

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

Coding Challenge

Create a setter method called setName.

Final Boss Quiz

Encapsulation combines…

Variables and methods into one class

Loops and arrays

Internet connections

🏆 Congratulations!

You Mastered Encapsulation!

Enter your name: