java調(diào)用另一個類的方法:1、類方法用static修飾,代碼為【public static void sayStatic()】;2、沒有static修飾的方法,代碼為【 public void sayInstance()】。
java調(diào)用另一個類的方法:
java類有兩種方法一種是類方法就是用static修飾的,一種是實(shí)例方法,就是沒有static修飾的方法。類方法可以同時類名,方法名的方式調(diào)用。而實(shí)例方法必須先生存類的實(shí)例在通過實(shí)例.方法名的方式調(diào)用。例如:
public class MethodCall { public static void main(String[] args) { Test.sayStatic(); Test test = new Test(); test.sayInstance(); } } class Test { public static void sayStatic() { System.out.println("這是一個靜態(tài)方法。"); } public void sayInstance() { System.out.println("這是一個實(shí)例方法。"); } }
相關(guān)免費(fèi)學(xué)習(xí)推薦:java基礎(chǔ)教程
以上就是java如何調(diào)用另一個類的方法的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
java怎么學(xué)習(xí)?java怎么入門?java在哪學(xué)?java怎么學(xué)才快?不用擔(dān)心,這里為大家提供了java速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號