Java 類方法
Java 類方法
您從《Java 方法》一章中瞭解到,方法是在類內部宣告的,並且它們用於執行某些操作。
示例
在 Main 中建立一個名為 myMethod()
的方法
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
}
myMethod()
在被呼叫時會列印一段文字(該操作)。要呼叫一個方法,請寫下方法名,後跟兩個括號 () 和一個分號 ;
示例
在 main
中,呼叫 myMethod()
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "Hello World!"
靜態 vs. 公共
您經常會看到 Java 程式具有 static
或 public
屬性和方法。
在上面的示例中,我們建立了一個 static
方法,這意味著在不建立類物件的情況下即可訪問它,這與 public
不同,後者只能透過物件訪問。
示例
一個演示 static
和 public
方法之間區別的示例
public class Main {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method on the object
}
}
注意:您將在《Java 修飾符》一章中瞭解更多關於這些關鍵字(稱為修飾符)的資訊。
使用物件訪問方法
示例
建立一個名為 myCar
的 Car 物件。在 myCar
物件上呼叫 fullThrottle()
和 speed()
方法,然後執行程式。
// Create a Main class
public class Main {
// Create a fullThrottle() method
public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}
// Create a speed() method and add a parameter
public void speed(int maxSpeed) {
System.out.println("Max speed is: " + maxSpeed);
}
// Inside main, call the methods on the myCar object
public static void main(String[] args) {
Main myCar = new Main(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}
// The car is going as fast as it can!
// Max speed is: 200
示例說明
1) 我們使用 class
關鍵字建立了一個自定義 Main
類。
2) 我們在 Main
類中建立了 fullThrottle()
和 speed()
方法。
3) 當被呼叫時,fullThrottle()
方法和 speed()
方法將打印出一些文字。
4) speed()
方法接受一個名為 maxSpeed
的 int
引數 - 我們將在8) 中使用它。
5) 為了使用 Main
類及其方法,我們需要建立一個 Main
類的物件。
6) 然後,轉到 main()
方法,您現在知道這是一個內建的 Java 方法,可以執行您的程式(main 中的任何程式碼都將被執行)。
7) 透過使用 new
關鍵字,我們建立了一個名為 myCar
的物件。
8) 然後,我們透過物件名稱(myCar
)、一個點(.
)和方法名稱(fullThrottle();
和 speed(200);
)來呼叫 myCar
物件上的 fullThrottle()
和 speed()
方法,並執行程式。請注意,我們在 speed()
方法中添加了一個 200 的 int
引數。
請記住...
點(.
)用於訪問物件的屬性和方法。
要在 Java 中呼叫方法,請寫下方法名稱,後跟一組括號 (),再後跟一個分號(;
)。
類必須有一個匹配的檔名(Main
和 Main.java)。
使用多個類
正如我們在《類》一章中指出的那樣,建立類物件並在另一個類中訪問它是良好的實踐。
請記住,java 檔案的名稱應與類名稱匹配。在此示例中,我們在同一個目錄中建立了兩個檔案:
- Main.java
- Second.java
Main.java
public class Main {
public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}
public void speed(int maxSpeed) {
System.out.println("Max speed is: " + maxSpeed);
}
}
Second.java
class Second {
public static void main(String[] args) {
Main myCar = new Main(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}
編譯好這兩個檔案後:
C:\Users\Your Name>javac Main.java
C:\Users\Your Name>javac Second.java
執行 Second.java 檔案
C:\Users\Your Name>java Second
輸出將是:
汽車正在以最快的速度行駛!
最高速度是:200