Skip to content

Encapsulation: private fields, getters, setters · ⁨封装:私有字段、getter 和 setter⁩

English

Protect your data

  • Encapsulation means hiding an object's data and only changing it through methods.
  • This keeps the data safe. Other code cannot put a bad value into it.
  • The main tools are the words private and public.

中文

保护你的数据

  • 封装(encapsulation)的意思是隐藏一个对象的数据,只能通过方法来修改它。
  • 这样能让数据保持安全。外部代码不能往里面放进一个错误的值。
  • 主要的工具是 private 和 public 这两个词。

私有的 balance 字段只能通过 getBalance 和 setBalance 访问

Handout

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩