Java中有虚拟构造函数吗?

发布于 2024-10-29 03:04:11 字数 39 浏览 3 评论 0原文

Java中有虚拟构造函数这个术语吗?那么我们需要在哪里使用它呢?

is there any term call Virtual Constructor in Java?then where we need to use this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小梨窩很甜 2024-11-05 03:04:11

虚拟构造函数不是 Java 语言的一部分,但该术语可能适用于某些设计模式。

例如,在支持它的对象上调用 object.clone() 将生成一个新对象(多于就像 new ClassName(object) 如果你有一个复制构造函数),因此类似于构造函数,但是是多态的。 Joshua Bloch 在《Effective Java》中主张使用静态工厂方法作为在某些情况下实现多态对象创建的另一种方式。

有关该术语在 C++ 上下文中的使用,请参阅: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8

Virtual Constructors are not a part of the Java language, but the term might be applied to some design patterns

For example, calling object.clone() on an object that supports it will produce a new object (much like new ClassName(object) if you have a copy constructor) and thus resembles a constructor, but is polymorphic. In "Effective Java" Joshua Bloch advocates the use of Static Factory methods as another way of achieving polymorphic object creation in certain circumstances.

For the use of the term in the C++ context look at: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8

等风来 2024-11-05 03:04:11

我不确定“虚拟构造函数”是什么意思。构造函数在类层次结构中递归调用。每个构造函数必须首先调用其超类构造函数。 (如果是对无参数构造函数的调用,则可以省略,在这种情况下,编译器会自动插入它。)

顺便说一句,Java 没有虚拟方法。或者,更准确地说,每个实例方法都是虚拟的(在 C++ 意义上)。

I'm not sure what you mean by "virtual constructor." Constructors are called recursively up the class hierarchy. Every constructor must call its super-class constructor as the first thing. (The call can be omitted if it is to the no-arg constructor, in which case the compiler will automatically insert it.)

As an aside, Java doesn't have virtual methods. Or, more precisely, every instance method is virtual (in the C++ sense).

贪了杯 2024-11-05 03:04:11

虚拟构造函数是什么意思?就像C++中的虚函数一样,java中没有虚构造函数。

What do you mean by virtual constructors? If it is like virtual function in C++ there is no virtual constructor in java.

落花随流水 2024-11-05 03:04:11

并不是说我不知道​​。 “默认构造函数”是的。 Delphi 有虚拟构造函数,而大多数其他语言没有。有关相关内容,请参阅工厂模式。

Not that I'm aware off. "Default Constructor " yes. Delphi has virtual constructors, most other languages does not. See the Factory pattern for something related.

热鲨 2024-11-05 03:04:11

取决于虚拟构造函数的定义。如果通过虚拟构造函数,您的意思是调用私有构造函数作为某些工厂模式的一部分的静态方法,那么是的,存在虚拟构造函数。我发现它们有时非常有用,当方法必须在构造后调用 self 时。您所要做的只是将构造函数设置为私有,并在类中包含一个创建类对象实例并返回它的静态方法。当用户可以使用对象之前需要调用该对象的方法时,这非常有用。由于对象尚未完全构造,因此在构造函数中引用 self 是一个非常糟糕的主意,因此可以使用虚拟构造函数在实例化之后且用户可以访问它之前调用方法。

Depending on the definition of virtual constructor. If by virtual constructor, you mean static method that calls a private constructor as part of some factory patterns, then yes there are virtual constructors. I have found them very useful at times, when methods must be called on self after construction. All you have to do is simply set the constructor to private, and within the class, include a static method that creates the class object instance and returns it. This is useful when methods need to be called on the object before the user is able to use it. Since it is a very bad idea to refer to self in the constructor due to the fact that the object is not fully constructed, one can use a virtual constructor to call the methods after instantiation and before the user may have access to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文