是否有可能改变一个继承的访问修饰符在ActionScript 3? [英] Is it possible to change an inherited access modifier in ActionScript 3?

查看:101
本文介绍了是否有可能改变一个继承的访问修饰符在ActionScript 3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个项目,我有一吨重的类从其他类继承等其他类继承。这也可能是更复杂的比它应该是,但我是一个吸盘抽象。

I'm currently working on a project where I have a ton of classes inheriting from other classes which inherit from other classes and so on. It's probably more complex than it should be, but I am a sucker for abstraction.

反正,有时我需要从被公私营改变的getter / setter。我想这是不是一个真正的需要,而是一种渴望切断事情是preSET的子类,但仍需要在父类向公众开放。

Anyway, at times I need to change a getter/setter from being public to private. I suppose it's not really a need, but a desire to cut off things that are preset in child classes, but still need to be publicly accessible in the parent classes.

因此​​,一个例子是:

So an example would be:

Class Base {
    public function set label( value:String ):void{};
}

Class A extends Base {}

Class B extends A {
    public function B() {
        super();
        this.label = "stuff";
    }

    override public function set label( value:String ):void {
        //this setter should not be publicly available since the label should not be possible to change in this class
    }
}

目前,我做了两件事情在这种情况下:

Currently, I am doing one of two things in these cases:

  1. 覆盖setter什么都不做,或将其设置为默认值,以便它仍可以更新/渲染/不管
  2. 在抛出一个错误,称这是该班
  3. 不可用

我已经做了一些搜索和一切似乎都指向这个是不可能的,但我从来没有发现它明确地说,它是不可能的。因此,它是可以改变访问修饰符在继承的属性/功能?

I've done some searching and everything seems to point to this being impossible, but I've never found it explicitly stated that it is impossible. So is it possible to change the access modifier on an inherited property/function?

推荐答案

这是不可能的,而且它确实不应该,因为它会导致混乱和联合国predictable类层次结构。对于初学者来说,如果你不喜欢的东西,你会打破里氏替换原则:在一个超类应该在任何时候都可以更换它的派生类。更改API会清醒地prevent的 - 并由此可能导致运行错误和/或莫名其妙的故障,如果另一个程序员意外交换类型

It is not possible, and it really should not be, because it leads to confusing and unpredictable class hierarchies. For starters, if you did something like that, you would break the Liskov Substitution Principle: A super class should at all times be replaceable by its derived classes. Changing the API would clearly prevent that - and thus possibly lead to runtime errors and/or inexplicable glitches, if another programmer accidentally exchanged types.

如果您正在建模的类都以这样的方式,将让你隐藏的,否则公共API方法不同的行为,你可能不应该使用继承这一点 - 或者以不同的方式。从你的描述,我猜想,在层次结构的较大部分,你或许应该使用组成,而不是继承,反正。

If the classes you are modeling have different behavior in such a way that would make you "hide" an otherwise public API method, you should probably not use inheritance for this - or perhaps in a different way. From what you are describing, I would guess that in a larger part of your hierarchy, you should probably be using composition instead of inheritance, anyway.

这篇关于是否有可能改变一个继承的访问修饰符在ActionScript 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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