日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

构造器(有参、无参)

發(fā)布時(shí)間:2023/12/14 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 构造器(有参、无参) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

構(gòu)造器: 就是和類(lèi)名相同但無(wú)返回類(lèi)型的方法。用于當(dāng)前或某一對(duì)象的實(shí)例化,并將當(dāng)前或某一對(duì)象返回。無(wú)參構(gòu)造:1、如果使用new關(guān)鍵字實(shí)際上是調(diào)用無(wú)參構(gòu)造器;2、無(wú)參構(gòu)造往往是為了初始化一些值。有參構(gòu)造:一旦定義了有參構(gòu)造,必須要顯示定義無(wú)參構(gòu)造
使用構(gòu)造器需注意:

1.構(gòu)造器必須與類(lèi)同名(如果一個(gè)源文件中有多個(gè)類(lèi),那么構(gòu)造器必須與公共類(lèi)同名)2.每個(gè)類(lèi)可以有一個(gè)以上的構(gòu)造器3.構(gòu)造器可以有0個(gè)、1個(gè)或1個(gè)以上的參數(shù)4.構(gòu)造器沒(méi)有返回值5.構(gòu)造器總是伴隨著new操作一起調(diào)用

下面是有參無(wú)參構(gòu)造的一個(gè)例子:

1、編寫(xiě)一個(gè)交通工具類(lèi)Vehicle類(lèi)。具有:屬性——載客量(capacity),方法:(1)無(wú)參構(gòu)造方法(給capacity初始化值為2,并輸出“執(zhí)行交通工具類(lèi)的無(wú)參構(gòu)造方法。”); (2)有參構(gòu)造方法(傳參給capacity初始化,并輸出“執(zhí)行交通工具的有參構(gòu)造方法。”);(3) capacity的set、get方法;(4) print方法:輸出capacity. (5)run方法,從控制臺(tái)中輸出“這是交通工具run方法”。
2、創(chuàng)建Vehicle類(lèi)的三個(gè)子類(lèi),Motor類(lèi)表示汽車(chē),Ship類(lèi)表示船,Aeroplane類(lèi)表示飛機(jī)類(lèi).各自具有;屬性——speed,表示速度. 方法: (1)無(wú)參構(gòu)造方法(給speed初始化值為0,并輸出“執(zhí)行XX類(lèi)的無(wú)參構(gòu)造方法。”);(2)有參構(gòu)造方法(用super關(guān)鍵字調(diào)用父類(lèi)的有參構(gòu)造方法,傳參給speed初始化,并輸出“執(zhí)行XX類(lèi)的有參構(gòu)造方法。”)(3) 加速(speedup):speed+10并返回speed;(4) 減速(speeddown):speed-15并返回speed;(5) 重寫(xiě)print方法:輸出speed和capacity。(6) 三個(gè)類(lèi)各自的run方法.分別輸出”汽車(chē)正在公路上奔跑”,”輪船正在大海上航行”,”飛機(jī)在天上飛行”
3、創(chuàng)建Motor的二個(gè)子類(lèi), Bus和Car(均為final類(lèi)),分別表示公共汽車(chē)和轎車(chē),分別寫(xiě)出各自的run方法。各自具有屬性——載客量(capacity);方法:(1) 無(wú)參構(gòu)造方法(給capacity初始化值為20,并輸出“執(zhí)行XX類(lèi)的無(wú)參構(gòu)造方法。”)(2) 有參構(gòu)造方法(用super關(guān)鍵字調(diào)用父類(lèi)的有參構(gòu)造方法,傳參給capacity初始化,并輸出“執(zhí)行XX類(lèi)的有參構(gòu)造方法。”)(3)寫(xiě)出它們自己的run方法.
同理,寫(xiě)出輪船的兩個(gè)子類(lèi),航空母艦和豪華游輪.飛機(jī)的兩個(gè)子類(lèi),戰(zhàn)斗機(jī)和客機(jī).
4、main函數(shù)中調(diào)用無(wú)參構(gòu)造方法創(chuàng)建一個(gè)Car的對(duì)象car;調(diào)用加速方法將速度加至50,調(diào)用print方法和run方法;調(diào)用減速方法,將速度減至20,調(diào)用print方法。調(diào)用有參構(gòu)造方法創(chuàng)建一個(gè)Bus的對(duì)象bus;調(diào)用print方法和run方法。航空母艦和豪華游輪,戰(zhàn)斗機(jī)和客機(jī).分別選擇一個(gè)構(gòu)造方法,并調(diào)用print和run方法.

package 交通工具;public class Main {public static void main(String[] args) {System.out.println("汽車(chē)類(lèi)->");Car car = new Car();car.setSpeed(40);System.out.println("最開(kāi)始的初速度為:40");System.out.println("加速***********");car.speedUp();car.print();car.run();System.out.println("減速***********");car.speedDown();car.speedDown();car.print();System.out.println("****************");Bus bus = new Bus(10,100);bus.print();bus.run();System.out.println("船的子類(lèi)->");Birdfarm birdfarm = new Birdfarm();birdfarm.setSpeed(40);System.out.println("最開(kāi)始的初速度為:40");System.out.println("加速***********");birdfarm.speedUp();birdfarm.print();birdfarm.run();System.out.println("減速***********");birdfarm.speedDown();birdfarm.speedDown();birdfarm.print();System.out.println("****************");Luxuryship luxuryship = new Luxuryship(10,100);luxuryship.print();luxuryship.run();System.out.println("飛機(jī)的子類(lèi)->");Fighter fighter = new Fighter();fighter.setSpeed(40);System.out.println("最開(kāi)始的初速度為:40");System.out.println("加速***********");fighter.speedUp();fighter.print();fighter.run();System.out.println("減速***********");fighter.speedDown();fighter.speedDown();fighter.print();System.out.println("****************");Aircraft aircraft = new Aircraft(10,100);aircraft.print();aircraft.run();}}

交通工具

package 交通工具;public class Vehicle {public int capacity;Vehicle() {capacity=2;System.out.println("執(zhí)行交通工具類(lèi)的無(wú)參構(gòu)造方法。");}Vehicle(int capacity) {this.capacity=capacity;System.out.println("執(zhí)行交通工具的有參構(gòu)造方法。");}public int getCapacity() {return capacity;}public void setCapacity(int capacity) {this.capacity = capacity;}public void print() {System.out.println("載客量:"+capacity);}public void run() {System.out.println("這是交通工具run方法");}}

汽車(chē)類(lèi)

package 交通工具;public class Motor extends Vehicle {public int speed;public Motor() {speed=0;System.out.println("執(zhí)行汽車(chē)類(lèi)的無(wú)參構(gòu)造方法。");}public Motor(int capacity,int speed) {super(capacity);this.speed=speed;System.out.println("執(zhí)行汽車(chē)類(lèi)的有參構(gòu)造方法。");}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int speedUp(){speed=speed+10;return speed;}public int speedDown() {speed=speed-15;return speed;}public void print(){System.out.println("速度:"+speed);System.out.println("載客量:"+capacity);}public void run(){System.out.println("汽車(chē)正在公路上奔跑");}}

轎車(chē)

package 交通工具;public final class Car extends Motor {int capacity;public Car() {capacity=20;System.out.print("執(zhí)行轎車(chē)類(lèi)的無(wú)參構(gòu)造方法。");}public Car(int capacity,int speed) {super(capacity,speed);this.capacity=capacity;System.out.println("執(zhí)行轎車(chē)類(lèi)的有參構(gòu)造方法。");}public void run() {System.out.println("轎車(chē)速度為:"+speed); System.out.println("轎車(chē)車(chē)載客量:"+capacity);}}

公共汽車(chē)

package 交通工具;public final class Bus extends Motor {public int capacity;public Bus() {capacity=20;System.out.println("執(zhí)行公共汽車(chē)類(lèi)的無(wú)參構(gòu)造方法。");}public Bus(int capacity,int speed) {super(capacity,speed);this.capacity=capacity;System.out.println("執(zhí)行公共汽車(chē)類(lèi)的有參構(gòu)造方法。");}public void run() {System.out.println("速度:"+speed);System.out.println("載客量:"+capacity);}}

輪船類(lèi)

package 交通工具;public class Ship extends Vehicle {public int speed;//無(wú)參構(gòu)造方法public Ship() {speed=0;System.out.println("執(zhí)行汽車(chē)類(lèi)的無(wú)參構(gòu)造方法。");}public Ship(int capacity,int speed) {//調(diào)用父類(lèi)的有參構(gòu)造方法super(capacity);this.speed=speed;System.out.println("執(zhí)行汽車(chē)類(lèi)的有參構(gòu)造方法。");}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int speedUp() {speed=speed+10;return speed;}public int speedDown() {speed=speed-15;return speed;}public void print() {System.out.println("速度"+speed);System.out.println("載客量"+capacity);}public void run() {System.out.println("輪船正在大海上航行");}}

航空母艦

package 交通工具;public final class Birdfarm extends Ship{int capacity;public Birdfarm () {capacity=20;System.out.print("執(zhí)行航空母艦類(lèi)的無(wú)參構(gòu)造方法。");}public Birdfarm (int capacity,int speed) {super(capacity,speed);this.capacity=capacity;System.out.println("執(zhí)行航空母艦類(lèi)的有參構(gòu)造方法。");}public void run() {System.out.println("航空母艦速度為:"+speed); System.out.println("航空母艦車(chē)載客量:"+capacity);}}

豪華游輪

package 交通工具;public final class Luxuryship extends Ship{int capacity;public Luxuryship () {capacity=20;System.out.print("執(zhí)行豪華游輪類(lèi)的無(wú)參構(gòu)造方法。");}public Luxuryship (int capacity,int speed) {super(capacity,speed);this.capacity=capacity;System.out.println("執(zhí)行豪華游輪類(lèi)的有參構(gòu)造方法。");}public void run() {System.out.println("豪華游輪速度為:"+speed); System.out.println("豪華游輪載客量:"+capacity); }}

飛機(jī)類(lèi)

package 交通工具;public class Aeroplane extends Vehicle {public int speed;//無(wú)參構(gòu)造方法public Aeroplane() {speed=0;System.out.println("執(zhí)行汽車(chē)類(lèi)的無(wú)參構(gòu)造方法。");}public Aeroplane (int capacity,int speed) {//調(diào)用父類(lèi)的有參構(gòu)造方法super(capacity);this.speed=speed;System.out.println("執(zhí)行汽車(chē)類(lèi)的有參構(gòu)造方法。");}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int speedUp() {speed=speed+10;return speed;}public int speedDown() {speed=speed-15;return speed;}public void print() {System.out.println("速度"+speed);System.out.println("載客量"+capacity);}public void run() {System.out.println("飛機(jī)在天上飛行");}}

戰(zhàn)斗機(jī)

package 交通工具;public final class Fighter extends Aeroplane{int capacity;public Fighter () {capacity=20;System.out.print("執(zhí)行戰(zhàn)斗機(jī)類(lèi)的無(wú)參構(gòu)造方法。");}public Fighter (int capacity,int speed) {super(capacity,speed);this.capacity=capacity;System.out.println("執(zhí)行戰(zhàn)斗機(jī)類(lèi)的有參構(gòu)造方法。");}public void run() {System.out.println("戰(zhàn)斗機(jī)速度為:"+speed); System.out.println("戰(zhàn)斗機(jī)載客量:"+capacity); }}

客機(jī)

package 交通工具;public final class Aircraft extends Aeroplane {int capacity;public Aircraft () {capacity=20;System.out.print("執(zhí)行客機(jī)類(lèi)的無(wú)參構(gòu)造方法。");}public Aircraft (int capacity,int speed) {super(capacity,speed);this.capacity=capacity;System.out.println("執(zhí)行客機(jī)類(lèi)的有參構(gòu)造方法。");}public void run() {System.out.println("客機(jī)速度為:"+speed); System.out.println("客載客量:"+capacity); }}

總結(jié)

以上是生活随笔為你收集整理的构造器(有参、无参)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。