gpt4 book ai didi

java - java抽象接口(interface)中的用户对象

转载 作者:行者123 更新时间:2023-12-02 04:16:05 25 4
gpt4 key购买 nike

我看到抽象内容就是接口(interface)传递的变量值。请告诉我如何运行这些功能

public interface Style {

void display();
}

public abstract class ActionFrame {

//Pass the parameter to the function is the interface
void addStyle(Style style) {
}
}

public class Test {
public static void main(String[] args) {
//Call class abstract
CActionFrame cActionFrame = new CActionFrame();
//Call function addStyle with parameter is the interface : Style
cActionFrame.addStyle(new Style() {
//Override function display() in Style
@Override
public void display() {
//I want to display text : "This is Test" but when run result is not output display
System.out.println("This is Test");
}
});
//How to call display ()
cActionFrame.?
}
}

我的问题是你遇到过很多这样的说法。

btnButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});

最佳答案

如果我理解正确的话,您想调用传递的 Style 对象的 display 方法,对吗?然后是这样的:

public interface Style {

public void display();
}

public abstract class ActionFrame {
private Style mStyle;
public void addStyle(Style style) {
mStyle = style;
}
public Style getStyle(){
return mStyle;
}
}

public class Test {
public static void main(String[] args) {
CActionFrame cActionFrame = new CActionFrame();
cActionFrame.addStyle(new Style() {

@Override
public void display() {
System.out.println("aaaaaaaaaaaaaaa");
}
});


cActionFrame.getStyle().display();
}
}

关于java - java抽象接口(interface)中的用户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com