受保护和私有的区别是什么?(复制)

时间:2021-09-27 15:51:45

Possible Duplicate:
Private and Protected Members : C++

可能的副本:私有和受保护成员:c++

I don't understand the difference between protected and private members or methods, as I assumed both will hide the member or the function to access from outside the class.

我不理解受保护成员和私有成员或方法之间的区别,因为我假设它们都将隐藏成员或从类外部访问的函数。

What is the difference between the protected and the private keywords?

受保护的关键字和私有关键字的区别是什么?

5 个解决方案

#1


52  

private - only available to be accessed within the class that defines them.

私有的——只能在定义它们的类中访问。

protected - accessible in the class that defines them and in other classes which inherit from that class.

受保护-在定义它们的类中可访问,在从该类继承的其他类中可访问。

#2


6  

Things that are private are only visible within the class itself.

私有的东西只在类本身中可见。

Things that are protected are visible in the class itself and in subclasses.

受保护的对象在类本身和子类中都是可见的。

#3


2  

Private methods are usually visible to class instances (internal implementations), protected methods are visible to subclasses and classes in the same package (inheritance and restricted usage).

私有方法通常对类实例可见(内部实现),受保护方法对同一包中的子类和类可见(继承和限制使用)。

#4


2  

Private members can only be used by that classes members and its friends; protected members can be inherited by other classes, and can be used by the classes members and friends.

私有成员只能被该类成员及其朋友使用;受保护成员可以被其他类继承,并且可以被类成员和朋友使用。

#5


1  

The difference is who can access those functions.

不同之处在于谁可以访问这些函数。

  • Private = only members of the same class can access the function.

    Private =只有同一个类的成员才能访问这个函数。

  • Protected = Same as private but derived classes can also access.

    Protected = private但派生类也可以访问。

#1


52  

private - only available to be accessed within the class that defines them.

私有的——只能在定义它们的类中访问。

protected - accessible in the class that defines them and in other classes which inherit from that class.

受保护-在定义它们的类中可访问,在从该类继承的其他类中可访问。

#2


6  

Things that are private are only visible within the class itself.

私有的东西只在类本身中可见。

Things that are protected are visible in the class itself and in subclasses.

受保护的对象在类本身和子类中都是可见的。

#3


2  

Private methods are usually visible to class instances (internal implementations), protected methods are visible to subclasses and classes in the same package (inheritance and restricted usage).

私有方法通常对类实例可见(内部实现),受保护方法对同一包中的子类和类可见(继承和限制使用)。

#4


2  

Private members can only be used by that classes members and its friends; protected members can be inherited by other classes, and can be used by the classes members and friends.

私有成员只能被该类成员及其朋友使用;受保护成员可以被其他类继承,并且可以被类成员和朋友使用。

#5


1  

The difference is who can access those functions.

不同之处在于谁可以访问这些函数。

  • Private = only members of the same class can access the function.

    Private =只有同一个类的成员才能访问这个函数。

  • Protected = Same as private but derived classes can also access.

    Protected = private但派生类也可以访问。