简体   繁体   English

如何在java中为特定构造函数创建方法?

[英]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.

相关问题 我可以在Java中用构造函数调用方法吗? - Can I call methods in constructor in Java? 如何在Java中的其他方法中使用此构造函数中的变量? - How do I use variables from this constructor in other methods in Java? 如何从特定的 class 创建 object 并使用 java 中的方法更改其属性 - How do I create an object from a specific class and change its attributes by using methods in java 在 Java 中,如何为文件的特定部分创建 InputStream? - In Java, how can I create an InputStream for a specific part of a file? 如何在 Java 中创建具有排序字符串和特定 Integer 的列表? - How can I create a List with sorted Strings and a specific Integer in Java? 如何在Java中实现哈希表的构造函数 - how can I implement the constructor of a hashtable in java 当我通过反射创建一个Object时,如何覆盖Java中的方法? - How can I override methods in Java when I create an Object via reflection? 构造函数中的参数是什么(上下文,属性)? 我如何创建新的 object 这个 class? Java 和 Kotlin - What is parameters in constructor (context, attrs)? How I can create new object this class? Java and Kotlin java-无法创建构造函数 - java - can't create a constructor 我可以为Java类创建非显式构造函数吗? - Can I create a non-explicit constructor for a Java class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM