簡體   English   中英

如果我創建一個新對象並將其放在main方法中,java將運行構造函數還是整個類?

[英]if i create a new object and put it in the main method, is java going to run the constructor or the entire class?

假設我有一個名為Panel的class

 public class Panel extends JPanel implements ActionListener{
       public Board(){
            setFocusable(true);
            setBackground(Color.black)
            setDoubleBuffered(true);
            timer= new Timer(16,this);
            timer.start();
       }
       public void actionPerformed(ActionEvent e){
            //.........some codes here...//

       }
       public void render(Graphics2D g){
            //....some code here....//
       }
}

我在主class運行它。

public class Main extends JFrame{
public Main(){
    add(new Panel());
    //....some code....//
}
public static void main(String[] args){
    new Main();
}
}

因為actionPerformed()render()方法不在構造函數中。

創建新對象時,只會執行構造函數(以及它調用的任何方法)。 通常,方法只有在您的代碼或框架調用時才會運行(通常是響應事件 - 在您的情況下,事件是Timer延遲時間的過去)。

請注意,您最初發布的代碼中存在拼寫錯誤:

public void actionPerfomred(ActionEvent e){

應該

public void actionPerformed(ActionEvent e){

如果使用@Override注釋重寫方法,編譯器將為您捕獲這樣的錯誤。 這樣你就不必在運行時弄清楚為什么沒有調用你的方法。

它顯然只會調用構造函數。 如果您沒有任何構造函數,則編譯器將調用默認的空構造函數。 但是當你只創建一個對象並且只調用構造函數時。

Java只會運行構造函數。 另外我注意到Panel類的構造函數名稱與其構造函數不匹配。 它必須是同一個名字。 而不是這個

public class Panel extends JPanel implements ActionListener{
       public Board(){
            setFocusable(true);
            setBackground(Color.black)
            setDoubleBuffered(true);
            timer= new Timer(16,this);
            timer.start();
       }

}

你做

public class Panel extends JPanel implements ActionListener{
   public Panel(){
        setFocusable(true);
        setBackground(Color.black)
        setDoubleBuffered(true);
        timer= new Timer(16,this);
        timer.start();
   }
}

另外,對於actionPerformed事件方法,您必須使用您必須執行某些操作的按鈕/列表手動連接它們。

但是在沒有在構造函數中編寫的情況下調用actionPerformed方法。

現在這是一個更好的問題。 這是你第一次提出問題時應該說明的行為,所以我們不猜你為什么要問這個問題。

那是因為你正在使用Timer 當Timer觸發時,調用添加到Timer的ActionListener 您的類實現了ActionListener,因此調用了您的類的actionPerformed()方法。

它只會調用構造函數。

是java要運行構造函數還是整個類?

不。 它不會運行整個班級。

同樣在類加載時,它會運行靜態塊內的東西,如果有的話。

if i create a new object and put it in the main method, is java going to run
the constructor or the entire class?

首先,你想要運行java是什么意思。 當您使用類名稱例如javac *.java調用javac ,所有java文件都將被編譯為中間字節代碼。 現在當你調用java Main java解釋器將從public static void main(String args[])函數開始解釋你的字節碼。 Java只是一個編程語言的名稱,你可以說是jvm going to execute the constructor or the entire class?

其次,執行整個課程是什么意思。 這有兩個方面

  1. Creating new object :每當您創建新對象(通過使用關鍵字new)時,都會調用相應的構造函數並創建對象。 如果你從構造函數中調用了任何方法,那么執行構造函數時它們也會被執行。
  2. Calling function/methods :如果您的方法不是私有的,那么您可以在創建對象后調用這些方法(取決於所使用的訪問修飾符)。

你的情況

new Panel();

創建新的Panel對象(調用相應的構造函數)。沒有其他方法將自己執行。

my question is that render and actionPerformed are not in the constructor,
how are they being executed? it is because of Timer?

這是你不應該問的問題。 您是程序員,您必須為您的要求編寫代碼。 至於用例,我可以告訴你這個

   public void actionPerformed(ActionEvent e){
        //.........some codes here...//

   }

您可以創建一個JButton或任何組件,並為其添加一個ActionListner。 ActionListner具有此方法。 由於您的類實現了ActionListner,因此您正在實現函數actionPerformed()。 例如。

    JButton button = new JButton();
    button.addActionListener(new Panel());

actionPerformed將通過計時器觸發器調用。

render方法不會被調用。 如果你想在swing JPanel繪制一些東西,應該覆蓋JComponent定義的paint(Graphics g)方法。

看看http://docs.oracle.com/javase/tutorial/uiswing/index.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM