public, protected, private 有咩用?

我明白佢地有咩分別,但唔明面佢地有咩用,有D 人話security, 睇唔透,求解答。Thxxxx

本帖最後由 mgchat 於 2014-11-6 18:47 編輯

例如你有program 有2個class, class a 同 class b
class b 既private varible 唔可以直接被class a 去access

同理,你有一個program,hacker又有另外一個program
hacker用program hack左你個program
如果你咩都用public,入面既data就會好容易比人access到

如果以memory既形式去解釋請教其他高手

有錯請指教

TOP

我明白佢地有咩分別,但唔明面佢地有咩用,有D 人話security, 睇唔透,求解答。Thxxxx ...
hio0o1 發表於 6/11/2014 18:22

Please Google!  
大大睇得多 examples 就會明白!
可以的話就全 PRIVATE!

TOP

例如你有program 有2個class, class a 同 class b
class b 既private varible 唔可以直接被class a 去acces ...
如果以memory既形式去解釋請教其他高手....
mgchat 發表於 6/11/2014 18:42

Compiler 識 PRIVATE/PUBLIC 不等於 OS 有 memory protection...

TOP

Please Google!   
大大睇得多 examples 就會明白!
可以的話就全 PRIVATE! ...
toylet 發表於 2014-11-6 18:56



    求keywords. 打左public, protected, private 都係講有咩分別

TOP

求keywords. 打左public, protected, private 都係講有咩分別
hio0o1 發表於 6/11/2014 19:24

"oop private public protected" (Object-Oriented Programming)

TOP

Compiler 識 PRIVATE/PUBLIC 不等於 OS 有 memory protection...
toylet 發表於 2014-11-6 18:58

其實最簡單可以用 reflection 開番佢

TOP

"oop private public protected" (Object-Oriented Programming)
toylet 發表於 2014-11-6 19:25



    oop principle

TOP

oop principle
snoopy11hk 發表於 6/11/2014 20:16

小人只想起 Visual Foxpro... 不太記得 C++....

TOP

本帖最後由 Jackass_TMxCK 於 2014-11-6 20:25 編輯

Access-specifier private
- Makes a data member or member function accessible only to member functions of the class
- private is the default access for class members
- Data hiding


As a rule of thumb, data members should be declared private and member functions should be declared public.

Software engineering with set and get functions
- public member functions that allow clients of a class to set or get the values of private data members
- set functions sometimes called mutators and get functions sometimes called accessors
- Allows the creator of the class to control how clients access private data
- Should also be used by other member functions of the same class


Always try to localize the effects of changes to a class’s data members by accessing and manipulating the data members through their get and set functions. Changes to the name of a data member or the data type used to store a data member then affect only the corresponding get and set functions, but not the callers of those functions.


如果任人改public attribute,要debug會難,轉做private再加setter可以set break point係個getter度,亦可以控制到改attribute時behavior如何

TOP