访问子静态属性 [英] access child static property

查看:48
本文介绍了访问子静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i需要一些帮助。

如何访问子类的静态属性。


i有一个抽象类将被扩展...

每个扩展类将有一个静态属性,它将存储一个如下数组:


i need some help here.
how can i access a static property of a subclass.

i have an abstract class which will be extended...

each extending class will have a static property that will store an array like this:

展开 | 选择 | Wrap | 行号

推荐答案

@bilibytes


没有。

@bilibytes


我认为这种方法在OOP方面没有意义(为什么父类应该处理动作,只发生在孩子班?)。这两个属性甚至都不为父类所知。
@bilibytes
no.

@bilibytes
I think that approach does not make sense in terms of OOP (why should a parent class handle actions, that only occur in the child class?). both properties are not even known to the parent class.


这是为了避免方法重复,但是如果它没有好的做法......

谢谢期待你的答复。我将在每个子类中创建一个方法。
It was to avoid method duplication, but if it''s no good practice...
thanks for your reply. I will create a method in each subclass.


这是一个棘手的问题,因为子类不会继承静态方法和成员。他们只是将呼叫转发给他们的父母。

而且,就像你说的那样,没有什么比可以使用child关键字了。 (这不是一个很好的解决方案)


所以在父类中创建一个静态方法,子类可以使用它来编辑儿童班的成员实际上并非实际可能性(至少不是我能看到的方式)

任何对继承的调用子类上的静态方法只会被转发回父级,它将编辑父级成员。



但是...

为此;有一种方法可以做到。

你不会想要使用它,但确实存在。


使用反射,您可以按名称操纵静态成员和方法。 (这将在5.3中被淘汰,当他们允许静态成员被他们的类名引用时)

Magic Constants 为我们提供了一种从其方法中动态检测类名的方法。 br />

所以,结合这两个,你可以这样做:
This is a tricky question, because static methods and members aren''t inherited by child classes. They simply forward calls to them back to their parents.
And, like you say, there is nothing like the "child" keyword that can be used. (Not that that would be a good solution to this)

So to create a static method in a parent class that could be used by child classes to edit the members of the child class isn''t really a practical possibility (At least not in a way I can see).
Any calls to the "inherited" static method on the child class would simply be forwarded back to the parent, where it would edit the parent''s members.


But...
For the sake of it; there is a way to do it.
You are not going to want to use it, but it does exists.

Using reflection, you can manipulate static members and methods by their name. (Which will be made obsolete in 5.3, when they allow static members to be referenced by their class names)
And he Magic Constants give us a way to dynamically detect a class name from within it''s methods.

So, combining those two, you can do:
展开 | 选择 | Wrap | 行号


这篇关于访问子静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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