对Java不是很熟,请知道的兄弟告诉我以下如何做:
Label label = new Label("createonelabel")
{
private int ddd;
... protected void onFocus()
{
...
} protected void onUnFocus()
{
...
}......
};注意:这段代码的意识是给对象添加Tooltips,其中onFocus()和onUnFocus()分别是获取焦点时和失去焦点时方法,继承某个基类来的,必须Override这种写法方式,因为这段代码我需要再很多地方使用,为了避免代码重用,所以想把它单独列出来变成以下方式:
Label label = new Label("createonelabel")
label.addNewMethod(parameters);
即变成Label的方法。
或者别的方式也行
反正一点,不想代码重用,因为用的地方太多了.
Label label = new Label("createonelabel")
{
private int ddd;
... protected void onFocus()
{
...
} protected void onUnFocus()
{
...
}......
};注意:这段代码的意识是给对象添加Tooltips,其中onFocus()和onUnFocus()分别是获取焦点时和失去焦点时方法,继承某个基类来的,必须Override这种写法方式,因为这段代码我需要再很多地方使用,为了避免代码重用,所以想把它单独列出来变成以下方式:
Label label = new Label("createonelabel")
label.addNewMethod(parameters);
即变成Label的方法。
或者别的方式也行
反正一点,不想代码重用,因为用的地方太多了.
:protected void onFocus() {
A();
} protected void onUnFocus(){
B();
}然后你可以在一个工具类中写方法A和B
public void onFocus();
public void onUnFocus();
...
}public class BaseImpl implements FocusEvent{ public void onFocus() {
} public void onUnFocus() {
}
}
然后Label有类似
Label label = new Label("createonelabel")
label.addFocusListener(focusEvent)// 这里是作为事件监听方式,传入一个实例
按LZ的意思,onFocus和onUnFocus来源于某个基类,只是希望在Label这个类有个缺省的事件
可以做一个缺省的类继承于Label,然后生成对象时new这个缺省的类
class TooltipLabel extends Label {
Map<String, Object> properties = new HashMap<String, Object>();
public TooltopLabel(String name) {super(name);}
public void addProperty(String name, Object value) { //为了通用,可以采用Map的方式添加删除属性
...
}
... protected void onFocus() { //缺省事件的实现
...
} protected void onUnFocus() {
...
}
}//生成实例
Label label = new TooltipLabel("createonelabel");