Java私有方法运用场景_java – 一个类可以同时拥有公共和私有构造函数吗?

Can a class have both public and private constructor?

对的,这是可能的.

A private constructor is needed to set the private field whose type is a private inner class. Is this encouraged or discouraged ?

这取决于实际情况.是否希望其他类初始化对象的状态.在这里,我认为你已经创建了类CopyTree来返回一个私有类的Tree of Tree.因此TreeNode类将被封装,因此它使您可以选择使用私有构造函数捕获习惯用法.

what is a better solution for a scenario listed below ?

在我看来,私有构造函数捕获成语是更好的解决方案.

欲获得更多信息:

在Java Puzzlers的解决方案53中给出了一个例子:

益智53:做你的事

Now it’s your turn to write some code. Suppose that you have a library class called Thing whose sole constructor takes an int parameter:

public class Thing {

public Thing(int i) { ... }

...

}

A Thing instance provides no way to get the value of its constructor parameter. Because Thing is a library class, you have no access to its internals, and you can’t modify it.

Suppose that you want to write a subclass called MyThing, with a constructor that computes the parameter to the superclass constructor by invoking the method SomeOtherClass.func(). The value returned by this method changes unpredictably from call to call. Finally, suppose that you want to store the value that was passed to the superclass constructor in a final instance field of the subclass for future use. This is the code that you’d naturally write:

public class MyThing extends Thing {

private final int arg;

public MyThing() {

super(arg = SomeOtherClass.func());

...

}

...

}

不幸的是,这不合法.如果您尝试编译它,您将收到如下所示的错误消息:

MyThing.java:

can't reference arg before supertype constructor has been called

super(arg = SomeOtherClass.func());

^

How can you rewrite MyThing to achieve the desired effect? The MyThing() constructor must be thread-safe: Multiple threads may invoke it concurrently.

解决方案53:做你的事

You could try to stash the result of the invocation SomeOtherClass.func() in a static field prior to invoking the Thing constructor. This solution is workable but awkward. In order to achieve thread-safety, you must synchronize access to the stashed value, which requires unimaginable contortions. Some of these contortions can be avoided by using a thread-local static field (java.util.ThreadLocal), but a much better solution exists.

The preferred solution is inherently thread-safe as well as elegant. It involves the use of second, private constructor in MyThing:

public class MyThing extends Thing {

private final int arg;

public MyThing() {

this(SomeOtherClass.func());

}

private MyThing(int i) {

super(i);

arg = i;

}

}

This solution uses an alternate constructor invocation. This feature allows one constructor in a class to chain to another constructor in the same class. In this case, MyThing() chains to private constructor MyThing(int), which performs the required instance initialization. Within the private constructor, the value of expression SomeOtherClass.func() has been captured in the parameter i and can be stored in the final field param after the superclass constructor returns.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值