java - 創(chuàng)建變量沒有輸出??
問(wèn)題描述
class Car{ int num; String color; public static void run() {System.out.println('行駛'); }}class Demo99 { public static void main(String[] args) { Car baoma = new Car();
//這這兒為什么需要使用baoma.run();這個(gè)語(yǔ)句才有輸出呢?//下面的代碼不需要引用函數(shù)就可以得到輸出了
}}
這個(gè)代碼沒有輸出這是為什么呢??下面這個(gè)代碼
public class CodeBlock02{ { System.out.println('第一代碼塊');}public CodeBlock02(){System.out.println('構(gòu)造方法');}{ System.out.println('第二構(gòu)造塊');} public static void main(String[] args){ CodeBlock02 acv = new CodeBlock02(); //或者用這個(gè)都有輸出 new CodeBlock02();}}
問(wèn)題解答
回答1:new 會(huì)去調(diào)用構(gòu)造方法,
public class Demo{public Demo(){ System.out.println('demo');} } public class Run{public Run(){}public void print(){ System.out.println('run');}
如果你使用了new Demo 這個(gè)時(shí)候會(huì)去調(diào)用Demo()這個(gè)構(gòu)造方法也就是會(huì)輸出。但是new run()不會(huì),因?yàn)闃?gòu)造方法沒有調(diào)用輸出語(yǔ)句,要輸出需要去調(diào)用print()方法。
回答2:第二個(gè)輸出的是“第一代碼塊”吧?
回答3:你要理解這幾個(gè)概念:
構(gòu)造方法
靜態(tài)方法
實(shí)例方法
代碼塊
了解了這幾個(gè), 你就想明白了
相關(guān)文章:
1. javascript - sublime快鍵鍵問(wèn)題2. javascript - immutable配合react提升性能?3. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問(wèn)?4. css - 寫頁(yè)面遇到個(gè)布局問(wèn)題,求大佬們幫解答,在線等,急!~5. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?6. phpstudy8.1支持win11系統(tǒng)嗎?7. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語(yǔ)法錯(cuò)誤8. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化9. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問(wèn)10. 實(shí)現(xiàn)bing搜索工具urlAPI提交

網(wǎng)公網(wǎng)安備