您可以在抽象类中声明私有属性吗? [英] Can you declare an attribute private within an abstract class?

查看:749
本文介绍了您可以在抽象类中声明私有属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个抽象类:

abstract class PersonAbstract
{
    private $name = "Stack Overflow";
}

在抽象类中将属性声明为 private 是否合法?还是应该扩展此类,事实上,受保护的最低可见性?

Is it legal to declare an attribute as private within an abstract class? Or the fact that this class should be extended, the minimum visibility is protected?

推荐答案

是的,您可以在抽象类中拥有一个私有字段.但是,该字段只能由该抽象类中的函数访问.从您的抽象类继承的任何类都将无法访问该字段.

Yes, you can have a private field within an abstract class. This field will only be accessible to functions within that abstract class though. Any classes which inherit from your abstract class will not be able to access the field.

您可以在抽象类中将字段和函数声明为public,protected或private.如果某个字段或功能是公共的,则任何人都可以访问它.如果受保护,则只有该类以及从该类继承的任何类都可以访问它.如果是私有的,则只能由该类访问.

You can declare both fields and functions as public, protected or private within an abstract class. If a field or function is public, it is accessible to anyone. If it is protected, it is accessible only to that class, and any classes which inherit from that class. If it is private, it is only accessible to that class.

抽象函数必须由继承的类实现,因此拥有私有抽象函数是毫无意义的(并且可能行不通).

Abstract functions must be implemented by an inheriting class, so it makes no sense (and probably won't work) to have a private abstract function.

这篇关于您可以在抽象类中声明私有属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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