继承(继承父类,super)
生活随笔
收集整理的這篇文章主要介紹了
继承(继承父类,super)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*
手機(jī)類:第一代功能:
1.打電話:鍵盤輸入手機(jī)號(hào)碼
2.接電話,顯示來電號(hào)碼,鈴聲提示第二代功能:
1.打電話:鍵盤輸入手機(jī)號(hào)碼
2.接電話,顯示來電號(hào)碼,顯示歸屬地,彩鈴提示*/
import java.util.Scanner;//第一代手機(jī)的功能
class PhoneOne{//私有化參數(shù)callNumberprivate int callnumber;int number;//PhoneoOne(){}//構(gòu)造函數(shù)對(duì)變量進(jìn)行初始化賦值,與set不同。set可以被對(duì)象調(diào)用,構(gòu)造函數(shù)在new的時(shí)候只初始化一次,不能被對(duì)象調(diào)用//構(gòu)造函數(shù)傳幾個(gè)參數(shù),在new的時(shí)候傳幾個(gè)參數(shù)//構(gòu)造函數(shù)繼承時(shí)使用superPhoneOne(int callnumber,int number){this.callnumber = callnumber;this.number = number;}//對(duì)參數(shù)賦值public void setCallNumber(int callnumber){this.callnumber = callnumber;}//對(duì)外提供參數(shù)public int getCallNumber(int callnumber){return callnumber;}//打電話public void callPhone(){System.out.println("您撥打的電話是:"+callnumber);}public void acceptPhone(){System.out.println("接電話");System.out.println("顯示來電號(hào)碼");System.out.println("鈴聲提示");}}
//對(duì)一代手機(jī)進(jìn)行繼承
class PhoneTwo extends PhoneOne {//因?yàn)楦割愑袠?gòu)造器,子類也要有構(gòu)造器PhoneTwo(int callnumber,int number){//調(diào)用父類構(gòu)造器super(callnumber,number);this.number = number;}public void acceptPhone(){在子類方法中通過 super 顯式調(diào)用父類被覆蓋的實(shí)例方法super.acceptPhone();System.out.println("顯示歸屬地");System.out.println("彩鈴提示");}}public class PhoneDome {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("請(qǐng)輸入要撥打的電話:");int callnumber = sc.nextInt();Scanner sc1 = new Scanner(System.in);int number = sc.nextInt();//獲取的string 轉(zhuǎn)換為int//int callnumber = Integer.parseInt(number);//構(gòu)造函數(shù)傳幾個(gè)參數(shù),調(diào)用時(shí)也要傳幾個(gè)參數(shù)PhoneOne p = new PhoneOne(callnumber,number);p.setCallNumber(callnumber);p.callPhone();p.acceptPhone();PhoneOne q = new PhoneTwo(callnumber,number);q.setCallNumber(callnumber);q.callPhone();q.acceptPhone();}
}
總結(jié)
以上是生活随笔為你收集整理的继承(继承父类,super)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电除尘原理计算机机箱,高炉煤气布袋除尘器
- 下一篇: python 计算器 eval ctf_