English 中文(简体)
为什么成员在超等级建筑商之后投放物体?
原标题:Why are member objects initialized after the super class s constructor?
  • 时间:2012-01-12 23:21:40
  •  标签:
  • java

我昨天陷入了一个令人感兴趣的问题,虽然固定点非常简单,但我仍然对“水”的怀疑。

我有一个在瞬时分配的私人成员变量的类别,但是,如果使用超级类别建筑商要求的抽象功能,则变量没有价值。 问题的解决非常简单,我只得宣布变数为static,并正确地分配。 说明问题的一些法典:

class Foo extends BaseClass
{
    private final String bar = "fooBar!";
    public Foo()
    {
        super();
    }

    @Override 
    public void initialize()
    {
        System.out.println(bar);
    }
}

基地类别:

abstract class BaseClass
{
    public BaseClass()
    {
        initialize();
    }

    public abstract void initialize();
}

In this example, when we call new Foo(); it will output (null) instead of the expected fooBar!

既然我们重新出现一种“Foo”的物体,那么其成员在打造者(及其超级阶级)之前是否没有得到分配和分配? 这一点在 Java语中是否具体指明了什么地方?

Thanks for any insight!

最佳回答

<代码bar = “foo Bar>;>:在编辑期间被指派到施工人员。

http://java.sun.com 二级建筑商因此,事后执行声明是自然的。

Generally though, it s bad practice to call overridable methods from a constructor.

问题回答

我只想补充大家接受的答复,因为我不同意他的结论。

我们这样做。

class Engine {
  public Engine() {
    init();
  }

  void init() {
    lockDoors();
    releasePressure();
    tightenSeatbelts();
    launchRocket();
  }

  ...
}

现在的问题是:,可登陆Landifier。 我们应在<条码>init(功能上添加。 如果是私人的或受保护的。

  • make it private <-- keeps subclasses out
  • make it protected <-- allows subclasses in

Before you make a choice

首先,你应认识到,(几乎)<条码>Engine类别中的所有代码均可由一个子类取代。

  • code in a public function, can easily be overridden
  • code in a protected function, can easily be overridden
  • code in a private function, can be replaced by overriding all methods that call it.

只有一个例外:

  • you can never modify the code of a constructor
  • you can never avoid a private method being called from the constructor of a super class.
  • (and of course, you cannot replace a final method)

Protected init() - the wrong way

请说<代码>init()方法protected 确实存在陷阱。 它试图推翻它,增加以下特征。 这确实是一个错误。

class SubEngine extends Engine {
  int screws = 5;

  void init() {
    tightenScrews();
    super.init();
  }

  void tightenScrews() {
    // this won t print 5, but it will print 0.
    System.out.println("tightening " + screws + " screws"); 
  }
}

Protected init() - the right way

因此,基本上来说,你应当仅仅解除父母的罪责,而是把执行推迟到自己的建筑人。

class SubEngine extends Engine {
  int screws = 5;

  public SubEngine() {
    initSubEngine();
  }

  void init() {
    // disable parent code
  }

  void initSubEngine() {
    tightenScrews();
    super.init();
  }

  void tightenScrews() {
    // this will print 5 as expected
    System.out.println("tightening " + screws + " screws"); 
  }
}

Private init() - you may need a phonecall

现在,如果<代码>init()”方法是 private?

  • Like mentioned above, there is no way to disable the code of a parent constructor. And if init() is private you simply cannot disable it.
  • You ll end up copying the entire Engine class, perhaps just to add 1 line of code.
  • And that may not be the end of it. Even after copying your class, your copied object won t be an Engine meaning that you won t be able to use your EngineUtil#inspectEngine(Engine engine) function.
  • Perhaps somebody knew this in advance and made an IEngine interface. Then you can get away with it.
  • In practice it means you ll have to take your phone, and call to that other department that made the Engine class, and ask them to change their code a little to take away some restrictions.

Intelligent design

还有另一种方式。 组成部分是确定变量。 他们 anything弃任何东西。 每当你们看到一个班级,在其建筑商(或采用私人方法)上,就应当有红色旗帜。

class Engine {
  public Engine() {
  }

  public void init() {
    lockDoors();
    releasePressure();
    tightenSeatbelts();
    launchRocket();
  }

  // and you probably also want one of these
  public void shutdown() { ... }

  ...
}

Intention

当然,你的意图很可能不是开放你的法典。 也许你真的不想让他人延长你的班子。 确实可以有这样的情况,即你想要把人锁住。

了解这也会使你法的测试更加困难。

任何情况都不同。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签