說明

每天都有XD的事!!

[公告]

『本站提供各類 {整合式教學 + 短評}』

2012年8月19日 星期日

A good way to remember accessibility for OOP


A good way to remember accessibility for Object Oriented Programming

There are 3 levels of accessibility to choose in a class: Public, Private, Protected.

The following explains the details for each accessibility:

Public - Anything can access.
Private - Only objects of the same class can access.
Protected - Only objects of the same class and its children (inherited class) can access.

I believe Public is very obvious, but it may be quite confusing for some people to remember what's the difference between Private and Protected. Now, here's the trick: add "toilet" behind every level, such that they become "public toilet", and so on. Get it now?

Public toilet - anyone can use.
Private toilet - only the owner can use; it's a very personal one.
Protected toilet - only the owner, and the owner's family, for example, can use. Imagine putting a combination lock on the door, and only the close people know what is the correct combination.

It's a bit gross, but it's very effective :)

All credits go to Dr. Soo Yuen Jien at NUS (http://www.comp.nus.edu.sg/~sooyj/), whom I learned this trick from. Many thanks to him as well.

沒有留言:

張貼留言