[英]Class Diagram from my code
我使用Eclipse的插件來創建此代碼的類圖:
public class ButtonGrid
{
private static int difficulty, moveleft, Counter, treasure_x , treasure_y;
private static String message;
JTextField tf = new JTextField();
public static JTextField tf2 = new JTextField();
JFrame frame = new JFrame(); //creation of the main game window
JPanel panel = new JPanel();
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel();
JLabel hint = new JLabel("Hint:");
JButton[][] grid; //grid buttons
public ButtonGrid (int width, int length)
{
}
ActionListener al = new ActionListener() //Action listener for the buttongrid
{
public void actionPerformed(ActionEvent e)
{
}
};
ActionListener al2 = new ActionListener() // Action listener for the reset button
{
public void actionPerformed (ActionEvent e)
{
}
}
};
public static void main (String[] args)
{
}
我削減了一些無用的零件以減小尺寸。 Eclipse繪制的圖就是這個圖:
您認為正確嗎? 我想知道是因為我認為ActionListeners被認為是子類,並且也沒有顯示main方法中的ActionListener,但也許只是我不了解類圖的工作原理。
您的圖表似乎正確。 您在方法內部創建的變量都不會出現在此圖中。 圖中僅顯示您在頂部(或方法外部,但在類定義內部)定義的變量:
private static int difficulty, moveleft, Counter, treasure_x , treasure_y;
private static String message;
JTextField tf = new JTextField();
public static JTextField tf2 = new JTextField();
JFrame frame = new JFrame(); //creation of the main game window
JPanel panel = new JPanel();
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel();
JLabel hint = new JLabel("Hint:");
JButton[][] grid; //grid buttons
ActionListener al = new ActionListener() //Action listener for the buttongrid
{
//defintion of this ActionListner
};
ActionListener al2 = new ActionListener() // Action listener for the reset button
{
//definition of this ActionListener
};
ActionListener
實際上是一個interface
:
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
您必須定義它,否則您將無法使用它。 子類是具有父類的類:
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
在我看來不錯。 您定義的ActionListener是用於受保護屬性a1和a2的匿名類。 基本上,匿名類在做什么是將ActionListener類子類化。 這些新的未命名類設置為a1和a2。 這就是為什么他們在類圖中顯示自己的方式的原因。 同樣,未顯示main方法中的那個原因的原因是匿名ActionListener是main函數的局部變量。
這是Oracle:關於匿名類的一些信息( http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html )
希望對您有所幫助,祝您編程愉快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.