面向对象编程中什么是公共、私有和受保护?
面向对象编程中什么是公共、私有和受保护?
What are public, private and protected in object oriented programming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
面向对象编程中什么是公共、私有和受保护?
What are public, private and protected in object oriented programming?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
它们是访问修饰符,帮助我们实现封装(或信息隐藏) 。 它们告诉编译器哪些其他类应该有权访问正在定义的字段或方法。
private
- 只有当前类才能访问该字段或方法。protected
- 只有该类的当前类和子类(有时还有同包类)才能访问该字段或方法。public
- 任何类都可以引用该字段或调用该方法。假设这些关键字用作类定义中字段或方法声明的一部分。
They are access modifiers and help us implement Encapsulation (or information hiding). They tell the compiler which other classes should have access to the field or method being defined.
private
- Only the current class will have access to the field or method.protected
- Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method.public
- Any class can refer to the field or call the method.This assumes these keywords are used as part of a field or method declaration within a class definition.
它们并不是真正的概念,而是在 C++ 和 Java 等流行语言中经常出现的特定关键字(语义略有不同)。
本质上,它们旨在允许类限制对成员(字段或函数)的访问。 这个想法是,允许一种类型在另一种类型中访问的次数越少,可以创建的依赖性就越少。 这使得所访问的对象可以更容易地更改,而不会影响引用它的对象。
广义上讲,public表示允许所有人访问,private表示只允许同一个类的成员访问,protected表示也允许子类的成员访问。 然而,每种语言都添加了自己的东西。 例如,C++ 允许您非公开继承。 在Java中,还有默认的(包)访问级别,还有关于内部类的规则等。
They aren't really concepts but rather specific keywords that tend to occur (with slightly different semantics) in popular languages like C++ and Java.
Essentially, they are meant to allow a class to restrict access to members (fields or functions). The idea is that the less one type is allowed to access in another type, the less dependency can be created. This allows the accessed object to be changed more easily without affecting objects that refer to it.
Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed. However, each language adds its own things to this. For example, C++ allows you to inherit non-publicly. In Java, there is also a default (package) access level, and there are rules about internal classes, etc.
这三个都是在类中使用的访问修饰符和关键字。
public 声明的任何变量都可以被类内或类外的任何对象使用,private 变量只能被类内的对象使用,不能通过直接访问更改(因为它可以通过友元函数等函数更改)在受保护部分下定义的任何内容都可以由该类及其派生类使用。
All the three are access modifiers and keywords which are used in a class.
Anything declared in public can be used by any object within the class or outside the class,variables in private can only be used by the objects within the class and could not be changed through direct access(as it can change through functions like friend function).Anything defined under protected section can be used by the class and their just derived class.
公共项目是可以从任何其他类访问的项目。 您只需要知道它是什么对象,就可以使用点运算符来访问它。 受保护意味着一个类及其子类可以访问该变量,但不能访问任何其他类,它们需要使用 getter/setter 对变量执行任何操作。 私有意味着只有该类可以直接访问变量,其他所有内容都需要方法/函数来访问或更改该数据。 希望这可以帮助。
A public item is one that is accessible from any other class. You just have to know what object it is and you can use a dot operator to access it. Protected means that a class and its subclasses have access to the variable, but not any other classes, they need to use a getter/setter to do anything with the variable. A private means that only that class has direct access to the variable, everything else needs a method/function to access or change that data. Hope this helps.
如上所述,但从质量上来说:
您提供的访问权限越少,从对象中泄漏的实现细节就越少。 这种泄漏的减少意味着在不破坏对象的客户端的情况下改变对象的实现方式方面具有更大的灵活性(也称为“松散耦合”)。 这是需要理解的真正基本的事情。
as above, but qualitatively:
the less access you provide the fewer implementation details leak out of your objects. less of this sort of leakage means more flexibility (aka "looser coupling") in terms of changing how an object is implemented without breaking clients of the object. this is a truly fundamental thing to understand.
总而言之,在面向对象编程中,一切都被建模为类和对象。
类包含属性和方法。
public、private 和 protected 关键字用于指定从其他类或其他 .dll 甚至其他应用程序对类的这些成员(属性和方法)的访问。
To sum it up,in object oriented programming, everything is modeled into classes and objects.
Classes contain properties and methods.
Public, private and protected keywords are used to specify access to these members(properties and methods) of a class from other classes or other .dlls or even other applications.
这些是访问修饰符。
所有数据和函数(行为)都被封装或限制在一个称为类的单元中。 为了访问类的属性和行为,我们使用对象。 但决定哪些行为或属性必须公开或应该保持对所有类的可访问性以及哪些行为或属性必须是私有的也很重要。
因此,此时访问修饰符(如 public、private、protected 和 protected inside)可以帮助实现此目的。 这种为类、方法或属性定义特权的行为称为抽象。
These are access modifiers.
All the data and functions(behaviours) are encapsulated or bounded into a single unit called a class. In order to access the properties and behaviour of the class we use objects. But it's also important to decide which behaviour or property has to be exposed or should remain accessible to all the classes and which behaviour or property has to be private.
So, that's when access modifiers like public, private, protected and protected internal help in doing so. This act of defining privilege to the class or method or property is called as abstraction.