是否可以在 Java 中扩展没有构造函数的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1004539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to extend a class with no constructors in Java?
提问by Ben S
For unit testing purposes I'm trying to write a mock objectof a class with no constructors.
出于单元测试的目的,我试图编写一个没有构造函数的类的模拟对象。
Is this even possible in Java, of is the class simply not extensible?
这在 Java 中甚至是可能的,是不是该类根本不可扩展?
回答by Steve Reed
A class with no constructors has an implicit public no-argument constructor and yes, as long as it's not final, it can be sub-classed.
一个没有构造函数的类有一个隐式的公共无参数构造函数,是的,只要它不是最终的,它就可以被子类化。
If the class has only private constructors then no, it can't.
如果该类只有私有构造函数,则不,它不能。
回答by Bill K
Question has been answered, but to add a comment. This is often a good time to propose that code be written to be somewhat testable.
问题已回答,但要添加评论。这通常是建议将代码编写为可测试的好时机。
Don't be a pain about it, research what it takes (probably Dependency Injection at least), learn about writing mocks and propose a reasonable set of guidelines that will allow classes to be more useful.
不要为此感到痛苦,研究它需要什么(至少可能是依赖注入),学习编写模拟并提出一套合理的指导方针,使类更有用。
We just had to re-write a bunch of singletons to use DI instead because singletons are notoriously hard to mock.
我们只需要重写一堆单例来使用 DI,因为单例是出了名的难以模拟。
This may not go over well, but some level of coding for testability is standard in most professional shops.
这可能不会很好,但在大多数专业商店中,一定程度的可测试性编码是标准的。
回答by Yishai
Yes, you can mock the object, although it may not be possible to subclass it (certainly not without getting very intimate with the class loader, anyway). Hereis how you do it with JMock.
是的,您可以模拟该对象,尽管可能无法对其进行子类化(无论如何,在不与类加载器非常密切的情况下当然不能)。以下是您如何使用 JMock 执行此操作。
Mocking in this fashion allows you to keep the type, without subclassing though it will probably quite hard to impossible to tease out only certain behavior. So this method is appropriate for testing classes that use this class, not for testing the class itself.
以这种方式模拟允许您保留类型,而无需子类化,尽管可能很难甚至不可能仅梳理出某些行为。所以这个方法适用于测试使用这个类的类,而不是测试类本身。
If you actually have access to the source code of the class, you could implement an inner class which allows you to extend it, although if you could do that, you could just make one of the constructors package private as well.
如果您确实可以访问该类的源代码,您可以实现一个允许您扩展它的内部类,尽管如果您可以这样做,您也可以将其中一个构造函数包设为私有。
There are also dynamic languages which will allow you to do the subclassing, and implement a Java interface which the Java code can interact with, but I'm not that familiar with the details.
还有一些动态语言可以让您进行子类化,并实现 Java 代码可以与之交互的 Java 接口,但我对细节并不熟悉。
回答by javamonkey79
You can change the visibility modifiers via reflection. Here is an article listing how.
您可以通过反射更改可见性修饰符。这是一篇文章,列出了如何。
回答by okutane
If java class doesn't have any defined constructors then there is no problem for you. The problems will be if class will have any constructors defined and all of them will be invisible for you (private).
如果 java 类没有任何已定义的构造函数,那么您就没有问题。问题将是类是否定义了任何构造函数,并且所有这些构造函数对您来说都是不可见的(私有)。
回答by Rogério
You can mock the class, and any or all of its constructors, using JMockit.
您可以使用JMockit模拟该类及其任何或所有构造函数。
It's a mocking toolkit for Java which lets you mock just about anything. Even if the class is non-public and/or nested, it can still be mocked. There are several mechanisms in JMockit that can be used for that. The toolkit distribution contains lots of sample JUnit tests as well.
它是一个用于 Java 的模拟工具包,可让您模拟任何内容。即使类是非公共的和/或嵌套的,它仍然可以被模拟。JMockit 中有几种机制可用于此目的。工具包分发版还包含许多示例 JUnit 测试。
If the class implements some interface or extends an abstract class, you can tell JMockit to "capture" and mock implementations of the base type on demand, as they are loaded by the JVM, and even assign the created instances to a field in the test class.
如果该类实现了某个接口或扩展了一个抽象类,您可以告诉 JMockit 按需“捕获”和模拟基类型的实现,因为它们由 JVM 加载,甚至将创建的实例分配给测试中的字段班级。
回答by Nathaniel Flath
If there are only private constructors, you can still use reflection in order to be able to access them from outside that class.
如果只有私有构造函数,您仍然可以使用反射以便能够从该类外部访问它们。

