您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Java中封裝和繼承的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
一、代碼及效果圖
1.封裝
案例:需求說明
使用封裝實現(xiàn)電子寵物系統(tǒng)的企鵝類正確輸入健康值和親密度
保證健康值的有效性(0-100),否則取默認值60
保證親密度的有效性(0-100),否則取默認值60
代碼如下(示例):
package work1; public class Dog { private String name; private String sex; private int love; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int sex) { if(sex==1){ this.sex = "Q仔"; }else{ this.sex = "Q妹"; } } public int getLove() { return love; } public void setLove(int love) { if(love>=0&&love<=100){ this.love = love; }else { System.out.println("親密度應(yīng)該在0-100之間,默認值是60"); this.love = 60; } } }
package work1; public class Penguin { private String name; private String sex; private int health; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int sex) { if(sex==1){ this.sex = "Q仔"; }else{ this.sex = "Q妹"; } } public int getHealth() { return health; } public void setHealth(int health) { if(health>=0&&health<=100){ this.health = health; }else { System.out.println("健康值應(yīng)該在0-100之間,默認值是60"); this.health = 60; } } }
package work1; import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("歡迎您來到寵物店! \n 請輸入要領(lǐng)養(yǎng)寵物的名字:"); String name = sc.next(); System.out.println("請選擇要領(lǐng)養(yǎng)的寵物類型:(1.狗狗 2.企鵝)"); int i = sc.nextInt(); if(i==1){//狗狗 Dog dog = new Dog(); dog.setName(name); System.out.println("請選擇狗狗的性別:(1.Q仔 2.Q妹)"); dog.setSex(sc.nextInt()); System.out.println("請輸入狗狗的親密度(1~100之間):"); dog.setLove(sc.nextInt()); System.out.println("寵物的自白:\n 我的名字叫"+name+",健康值是"+0+",和主人的" + "親密度是"+dog.getLove()+",我的性別是"+dog.getSex()); }else {//企鵝 Penguin p = new Penguin(); p.setName(name); System.out.println("請選擇企鵝的性別:(1.Q仔 2.Q妹)"); p.setSex(sc.nextInt()); System.out.println("請輸入企鵝的健康值(1~100之間):"); p.setHealth(sc.nextInt()); System.out.println("寵物的自白:\n 我的名字叫"+name+",健康值是"+p.getHealth()+",和主人的" + "親密度是"+0+",我的性別是"+p.getSex()); } } }
2.繼承
案例:使用繼承優(yōu)化電子寵物系統(tǒng),抽取父類,創(chuàng)建子類,在子類中使用super調(diào)用父類構(gòu)造方法。
代碼如下(示例):
package work3; public class Dog extends Pet { @Override public void show() { System.out.println("寵物的自白:\n 我的名字叫"+this.getName()+",健康值是"+0+",和主人的" + "親密度是"+this.getLove()+",我的性別是"+this.getSex()); } private int love; public Dog(String name, int sex, int love) { super(name, sex); this.love = love; } public int getLove() { return love; } public void setLove(int love) { if(love>=0&&love<=100){ this.love = love; }else { System.out.println("親密度應(yīng)該在0-100之間,默認值是60"); this.love = 60; } } }
package work3; public class Penguin extends Pet { @Override public void show() { System.out.println("寵物的自白:\n 我的名字叫"+this.getName()+",健康值是"+this.getHealth()+",和主人的" + "親密度是"+0+",我的性別是"+this.getSex()); } private int health; public Penguin(String name, int sex, int health) { super(name, sex); this.health = health; } public int getHealth() { return health; } public void setHealth(int health) { if(health>=0&&health<=100){ this.health = health; }else { System.out.println("健康值應(yīng)該在0-100之間,默認值是60"); this.health = 60; } } }
package work3; public class Pet { private String name; private String sex; public void show(){ } public Pet(String name, int sex) { this.name = name; if(sex==1){ this.sex = "Q仔"; }else{ this.sex = "Q妹"; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int sex) { if(sex==1){ this.sex = "Q仔"; }else{ this.sex = "Q妹"; } } }
package work3; import java.util.Scanner; import static java.lang.System.out; public class Test2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); out.println("歡迎您來到寵物店! \n 請輸入要領(lǐng)養(yǎng)寵物的名字:"); String name = sc.next(); out.println("請選擇要領(lǐng)養(yǎng)的寵物類型:(1.狗狗 2.企鵝)"); int i = sc.nextInt(); if (i==1){//狗狗 out.println("請選擇狗狗的性別:(1.Q仔 2.Q妹)"); int sex=sc.nextInt(); out.println("請輸入狗狗的親密度(1~100之間):"); Dog dog = new Dog(name, sex, sc.nextInt()); dog.show(); }else {//企鵝 out.println("請選擇企鵝的性別:(1.Q仔 2.Q妹)"); int sex=sc.nextInt(); out.println("請輸入企鵝的健康值(1~100之間):"); Penguin p = new Penguin(name, sex, sc.nextInt()); p.show(); } } }
感謝各位的閱讀!關(guān)于“Java中封裝和繼承的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。