java抽象接口中的用户对象

时间:2015-10-23 14:32:08

标签: java interface abstract

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

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.
    }
});

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你想调用传递display对象的Style方法,对吧?然后是这样的事情:

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();
    }
}