public、private、protected、friendly区别

Before explaining these four keywords, we first  make a simple definition of the relationship between classes:

For inheriting your own  class,  base class can be regarded as their own children, and for classes under a directory with themselves  , they are all their own friends.  

 

1. public: public indicates that the data members and member functions are open to all users, and all users can directly call

2. Private: private means private. Private means that no one can use it directly except the class itself. Private property is sacred and inviolable, even children and friends cannot use it.

3. Protected: protected is public for children and friends, and can be used freely without any restrictions, while for other external classes, protected becomes private . 


4. friendly : The default is friendly when not writing , friendly is friendly, for friends, it can be used freely, without any restrictions, and for other external classes , even children, can not be used.

                The current scope is the same package, the descendants are other packages
                public      √         √            √         √
                protected   √         √            √         ×

friendly √ √ × ×

private √ × × ×


Guess you like

Origin www.cnblogs.com/xxhxs-21/p/12706399.html