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

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

问题描述

在面向对象编程中什么是公共的、私有的和受保护的?

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.

这假定这些关键字用作类定义中的字段或方法声明的一部分.

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

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