[英]How can I create methods to a specific constructor in java?
When I create a method in Java, that method applies to every instance of the class, no matter which constructor I use to create that instance. 当我在Java中创建一个方法时,无论我使用哪个构造函数来创建该实例,该方法都适用于该类的每个实例。 What I want to know is how can I a method be invoked only by instances created by a specific constructor of my choosing.
我想知道的是,如何只能由我选择的特定构造函数创建的实例调用方法。
/** (that program below is just an example of my point) */
public class Person{
private int height;
/** first constructor */
public Person(){
height = 0;
}
/** second constructor */
public Person(int height){
this.height = height;
}
/** I want this method to work just on the constructor with the int parameter */
public void getTaller(){
height = height + 1;
}
}
The closest to what you're asking is inheritance and factory methods: 你问的最接近的是继承和工厂方法:
public class Person {
public static Person createWithoutHeight() {
return new Person();
}
public static Person createWithHeight(int height) {
return new PersonWithHeight(height);
}
}
public class PersonWithHeight extends Person {
private int height;
public PersonWithHeight(int height) {
this.height = height;
}
public void makeTaller() {
height++;
}
}
在Java中没有这样的东西,你不能创建一个方法并选择这个方法到特定的构造函数。
You cannot limit the use of methods on an instance regardless of which constructor is used. 无论使用哪种构造函数,都不能限制实例上方法的使用。 Read about what an instance is.
了解实例是什么。 The getTaller will just increment the height which can start at 0 or the specified height.
getTaller只会增加从0开始或指定高度开始的高度。
Split the class into an abstract class and two implementations: 将类拆分为抽象类和两个实现:
public abstract class Person {
protected int height;
protected Person(int height) {
this.height = height
}
public int getHeight() {
return height;
}
}
public class PersonImmutable extends Person {
public PersonFixed(int initialHeight) {
super(initialHeight)
}
}
public class PersonMutable extends Person {
public PersonFixed(int initialHeight) {
super(initialHeight)
}
public void setHeight(int newHeight) {
this.height = newHeight;
}
}
I believe what OP is trying to put a function pointer as a parameter to a constructor. 我相信OP正在尝试将函数指针作为参数添加到构造函数中。 Unfortunately, unlike C/C++, Java does not allow such things , as there is no such thing as a function pointer in Java.
不幸的是,与C / C ++不同,Java 不允许这样的事情 ,因为Java 中没有函数指针这样的东西。
On the other hand, you could make use of a light Command pattern , that is, you encapsulate a function through an object. 另一方面,您可以使用轻型Command模式 ,即通过对象封装函数。
Example: 例:
class Function {
// fields here, each stands for a function parameter
public Function ( insert params here ) {
// initialize your Function object here
}
// the meat of it
public void execute() {
// do whatever your function should do
}
}
Then in your constructor, add your Function as a parameter to it, as in 然后在构造函数中,将Function作为参数添加到其中,如
public Person(Function myFunction) {
// do whatever you want here
// ...
// then you call your custom function
myFunction.execute();
}
This is the most generic behavior of passing a function as parameter to another function. 这是将函数作为参数传递给另一个函数的最通用行为。 You may, of course, tweak this to your heart's content (add parameters to the execute() method, etc).
当然,您可以根据内容调整此内容(向execute()方法添加参数等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.