1. 分析寵物店種的類與方法
寵物:狗屁使,貓在岂,鴨子
寵物屬性:有名字,健康值
狗類的屬性:狗還有種類
方法:都會(huì)叫蛮寂,都會(huì)簡紹自己
鑒于它們都會(huì)叫和簡紹蔽午,所以可以用接口或者抽象類來實(shí)現(xiàn)方法
2.代碼如下:
定義抽象類,內(nèi)含聲音方法,j簡紹方法
package petshop;
public abstract class Function {
abstract void sound() ;
abstract void speak();
}
定義寵物類酬蹋,內(nèi)含寵物姓名及老,寵物健康值
package petshop;
public class Pets extends Function {
private String name ;
private int healthIndex;
public Pets() {
this("無名氏",80);
}
public Pets(String name, int healthIndex) {
this.name = name;
this.healthIndex = healthIndex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealthIndex() {
return healthIndex;
}
public void setHealthIndex(int healthIndex) {
this.healthIndex = healthIndex;
}
@Override
void sound() {
}
@Override
void speak() {
System.out.println("我的名字是:"+this.name+"我的健康值是:"+this.healthIndex);
}
}
定義狗類,內(nèi)包含狗類別和繼承父類寵物類
package petshop;
public class Dog extends Pets {
private String kinds;
public Dog() {
super();
this.kinds = "品種未知";
}
public Dog(String name, int healthIndex, String kinds) {
super(name, healthIndex);
this.kinds = kinds;
}
public String getKinds() {
return kinds;
}
public void setKinds(String kinds) {
this.kinds = kinds;
}
@Override
public void sound() {
System.out.println("汪汪汪");
}
@Override
public String toString() {
return super.toString()+"范抓,我的品種是:"+kinds;
}
}
Duck 類
package petshop;
public class Duck extends Pets {
public Duck() {
}
public Duck(String name, int healthIndex) {
super(name, healthIndex);
}
@Override
public void sound() {
System.out.println("嘎嘎嘎");
}
}
Cat類
package petshop;
public class Cat extends Pets {
public Cat() {
}
public Cat(String name, int healthIndex) {
super(name, healthIndex);
}
@Override
public void sound() {
System.out.println("喵喵喵");
}
}
PetsShop類 其中包括商店進(jìn)寵物骄恶,還有隨機(jī)給顧客簡紹寵物
package petshop;
import java.util.Random;
import java.util.Scanner;
public class PetShop {
static Pets pet[] = new Pets[3];
static PetShop petShop = new PetShop();
static Scanner scanner = new Scanner(System.in);
static String name[] = new String[3];
static int healthIndex[] = new int[3];
static String kinds[] = new String[3];
public static void main(String[] args) {
String str = new String();
//進(jìn)貨
for (int i = 0; i < 3; i++) {
System.out.println("請輸入Dog/Cat/Duck");
str = scanner.next();
if (str.equalsIgnoreCase("dog")) {
System.out.println("請輸入第" + (i + 1) + "只寵物的名字");
name[i] = scanner.next();
System.out.println("請輸入第" + (i + 1) + "只寵物的健康指數(shù)");
healthIndex[i] = scanner.nextInt();
System.out.println("請輸入第" + (i + 1) + "只寵物的種類");
kinds[i] = scanner.next();
petShop.pet[i] = new Dog(name[i], healthIndex[i], kinds[i]);
} else if(str.equalsIgnoreCase("duck")){
System.out.println("請輸入第" + (i + 1) + "只寵物的名字");
name[i] = scanner.next();
System.out.println("請輸入第" + (i + 1) + "只寵物的健康指數(shù)");
healthIndex[i] = scanner.nextInt();
petShop.pet[i] = new Duck(name[i], healthIndex[i]);
}else if (str.equalsIgnoreCase("cat")){
System.out.println("請輸入第" + (i + 1) + "只寵物的名字");
name[i] = scanner.next();
System.out.println("請輸入第" + (i + 1) + "只寵物的健康指數(shù)");
healthIndex[i] = scanner.nextInt();
petShop.pet[i] = new Cat(name[i], healthIndex[i]);
}else {
System.out.println("輸入錯(cuò)誤!匕垫!");
}
}
for (int i = 0; i < 3; i++) {
//隨機(jī)選擇寵物
int index = new Random().nextInt(petShop.pet.length);
Pets pet = petShop.pet[index];
//寵物開始自我介紹
pet.speak();
//我給你唱歌
pet.sound();
}
}
}
3.接口實(shí)現(xiàn)
將抽象類換成接口僧鲁,方法不用實(shí)現(xiàn),誰調(diào)用接口誰實(shí)現(xiàn)方法象泵,可以將sound()與speak()分成兩個(gè)接口寞秃,使其復(fù)用性更強(qiáng)。