面向对象编程中的公共,私有和受保护的是什么? [英] What are public, private and protected in object oriented programming?

查看:323
本文介绍了面向对象编程中的公共,私有和受保护的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面向对象编程中的公开,私有和受保护的是什么?

What are public, private and protected in object oriented programming?

推荐答案

>并帮助我们实施封装(或信息隐藏)。它们告诉编译器哪些其他类应该访问被定义的字段或方法。

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 - 只有当前类才能访问该字段或方法。

private - Only the current class will have access to the field or method.

protected - 只有当前类和子类)

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 - 任何类都可以引用字段或调用方法。

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.

这篇关于面向对象编程中的公共,私有和受保护的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆