Java实验项目三——宠物商店
Program:宠物商店的设计(继承,接口,线性线性表)
Description:本题未实现图形用户界面,项目结构描述如下:
classes.Pet:定义宠物接口,只要实现该接口的宠物类,都可存储进宠物商店
(本例定义的接口为标识接口,未定义任何方法,只用于标识)
classes.PetShop:宠物商店类,采用了单例设计模式
classes.entity.Dog:宠物狗类,实现了Pet接口
classes.entity.Cat:宠物猫类,实现了Pet接口
main.TestDemo:测试类
classes.Pet
1 /* 2 * Description:定义宠物标识接口,只要是实现此接口的类都为宠物类,都可放进宠物商店 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-18 7 * 8 * */ 9 10 11 package classes; 12 13 public interface Pet { 14 15 }
classes.PetShop
1 /* 2 * Description:定义宠物商店,采用单例设计模式 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-18 7 * 8 * */ 9 10 11 package classes; 12 13 import java.util.Iterator; 14 import java.util.List; 15 import java.util.ArrayList; 16 17 public class PetShop { 18 19 private List<Pet> pets = new ArrayList<Pet>(); //存取宠物 20 private static PetShop instance = null; //声明私有对象 21 22 //定义私有构造方法 23 private PetShop() { } 24 25 //取得宠物商店的实例 26 public static PetShop getInstance() { 27 28 if( PetShop.instance == null ) { 29 30 PetShop.instance = new PetShop(); 31 } 32 return PetShop.instance; 33 } 34 35 //取得宠物的数量 36 public int getCount() { 37 38 return this.pets.size(); 39 } 40 41 //添加宠物 42 public void add(Pet pet) { 43 44 if( pet != null ) { 45 46 this.pets.add(pet); 47 } 48 } 49 50 //打印所有宠物 51 52 public void displayInfo() { 53 54 Iterator<Pet> ite = this.pets.iterator(); 55 while( ite.hasNext() ) { 56 57 System.out.println( ite.next() ); 58 } 59 } 60 61 }
classes.entity.Dog
1 /* 2 * Description:定义宠物狗类 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-18 7 * 8 * */ 9 10 package classes.entity; 11 12 import classes.Pet; 13 14 public class Dog implements Pet { 15 16 private String name; 17 private String color; 18 private int age; 19 20 //定义构造方法 21 22 public Dog() {} 23 24 public Dog(String name, String color, int age) { 25 26 super(); 27 this.name = name; 28 this.color = color; 29 this.age = age; 30 } 31 32 33 //定义setter()和getter()方法 34 35 public String getName() { 36 return name; 37 } 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 public String getColor() { 44 return color; 45 } 46 47 public void setColor(String color) { 48 this.color = color; 49 } 50 51 public int getAge() { 52 return age; 53 } 54 55 public void setAge(int age) { 56 this.age = age; 57 } 58 59 60 //覆写toString()方法 61 @Override 62 public String toString() { 63 return "Dog [name=" + name + ", color=" + color + ", age=" + age + "]"; 64 } 65 66 }
classes.entity.Cat
1 /* 2 * Description:定义宠物猫类 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-18 7 * 8 * */ 9 10 package classes.entity; 11 12 import classes.Pet; 13 14 public class Cat implements Pet { 15 16 private String name; 17 private String color; 18 private int age; 19 20 //定义构造方法 21 22 public Cat() {} 23 24 public Cat(String name, String color, int age) { 25 26 super(); 27 this.name = name; 28 this.color = color; 29 this.age = age; 30 } 31 32 33 //定义setter()和getter()方法 34 35 public String getName() { 36 return name; 37 } 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 public String getColor() { 44 return color; 45 } 46 47 public void setColor(String color) { 48 this.color = color; 49 } 50 51 public int getAge() { 52 return age; 53 } 54 55 public void setAge(int age) { 56 this.age = age; 57 } 58 59 60 //覆写toString()方法 61 @Override 62 public String toString() { 63 return "Cat [name=" + name + ", color=" + color + ", age=" + age + "]"; 64 } 65 66 }
main.TestDemo
1 /* 2 * Description:定义测试类,测试宠物商店 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-18 7 * 8 * */ 9 10 package main; 11 12 import classes.*; 13 import classes.entity.Cat; 14 import classes.entity.Dog; 15 16 public class TestDemo { 17 18 public static void main(String args[]) { 19 20 //取得宠物商店的实例 21 PetShop shop = PetShop.getInstance(); 22 23 //添加宠物 24 shop.add( new Dog("小黑","黑色",2) ); 25 shop.add( new Dog("小白","白色",3) ); 26 shop.add( new Cat("小喵","黄色",1) ); 27 shop.add( new Cat("大喵","白色",3) ); 28 29 //打印所有宠物 30 shop.displayInfo(); 31 32 } 33 34 }
1 /* * 2 * Description:定义宠物商店,采用单例设计模式 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-18 7 * 8 */ 9 10 package classes; 11 import java.util.Iterator; 12 import java.util.List; 13 import java.util.ArrayList; 14 public class PetShop { 15 private List<Pet> pets = new ArrayList<Pet>();//存取宠物 16 private static PetShop instance = null;//声明私有对象 17 //定义私有构造方法 18 private PetShop() {} 19 //取得宠物商店的实例 20 public static PetShop getInstance() { 21 if( PetShop.instance == null ) { 22 PetShop.instance = new PetShop(); 23 } 24 return PetShop.instance; 25 } 26 //取得宠物的数量 27 public int getCount() { 28 return this.pets.size(); 29 } 30 //添加宠物 31 public void add(Pet pet) { 32 if( pet != null ) { 33 this.pets.add(pet); 34 } 35 } 36 //打印所有宠物 37 public void displayInfo() { 38 Iterator<Pet> ite = this.pets.iterator(); 39 while( ite.hasNext() ) { 40 System.out.println( ite.next() ); 41 } 42 } 43 }
初学小白,请多指教!