问题
Is it possible to have class A with an empty method say render().. then you create 3 instances of the class, so you have objects b, c, d, so can I THEN assign bodies for the render method for each object?
Here's an example, in JavaScript you can have an object say a and then anywhere you can say
a.method = function() { /* do something */ }
After that line, you will have a method with the name method for the object a and whenever called it will /* do something */.
Is this possible in Java and what is the syntax?
My idea is to have a button class and assign different actions to it's instance's click method inside the different situations / context it is used, so I don't have to define a different child class for each action.
回答1:
You can annomyously instantiate a subclass of the A class and override the method you'd like in it. Like so
A a = new A(){
@Override
public void render(){
//do something
}
};
A b = new A(){
@Override
public void render(){
//do something
}
}
回答2:
In Java you can't pass around functions as you can in Javascript.
You need to create a class or interface called something like Handler (preferably something more descriptive!) which declares the method you want to call. Then you can do something similar to this:
a.method = function() { /* do something */ }
but you have to formulate it as
a.method = new Handler() {
public void function() {
/* do something */
}
};
and call it as a.method.function().
My idea is to have a
buttonclass and assign different actions to it's instance'sclickmethod inside the different situations / context it is used, so I don't have to define a different child class for each action.
This is precisely how Swing does it. It could typically look something like this:
myButton.setAction(new AbstractAction("Click me!") {
public void actionPerformed() {
// do something
}
});
回答3:
You can't define the method body after instantiating class A but you can define the method body when you instantiate it.
interface A {
void method();
}
A b = new A() {
@Override
public void method() {
//body for instance b
}
};
A c = new A() {
@Override
public void method() {
//body for instance c
}
};
回答4:
No, you can't do that in Java. The implementation of a method is entirely determined by the runtime type of its object.
But you may use the strategy pattern, and it's actually what's being done (more or less) by the standard Swing framework: you may set an Action to a JButton, and the Action's actionPerformed() method is called when the button is clicked. It's just delegation between objects.
回答5:
If you only want to instantiate only one object of the class which has the render method, you can use delegation; you may want to do this if the class is a heavyweight one.
This actually is the Strategy Pattern:
class MyClass {
private Behavior behavior;
public void setBehaviour(Behavior someBehaviour) {
this.behavior = someBehavior;
}
public void render() {
behavior.render();
}
}
interface Behavior {
void render();
}
class BehaviorA implements Behavior {
public void render() {
//do behavior A
}
}
class BehaviorB implements Behavior {
public void render() {
//do behavoir B
}
}
class RunnerClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.setBehavior(new BehaviorA()); //actually you should put a factory for behavioir
myClass.render();//render A logic is done
myClass.setBehavior(new BehaviorB());
myClass.render();//render B logic is done
}
}
回答6:
Well, to be honest that sounds like a pure polymorphism in java that can be achieve in two ways :
either create a common interface that each of your object implements differently
define an abstract class button and create b,c,d button that extends abstract button and define the behavior of each button differently.
http://java.sys-con.com/node/37695
http://www.javaworld.com/javaworld/javatips/jw-javatip30.html
来源:https://stackoverflow.com/questions/10849963/dynamically-reassign-method-bodies-to-objects