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 }

最新文章

  1. MS SQL Server中数据表、视图、函数/方法、存储过程是否存在判断及创建
  2. smarty string_format用法 取小数点后2位
  3. Svn与Git的一些区别(转载)
  4. CustomValidator验证的使用方法
  5. General protection fault Exceptions in Linux/IA32 Systems
  6. 使用RestTemplate Spring安全认证
  7. 在Python中怎么表达True
  8. 工具系列之Sublime Text 3 使用总结
  9. c++实现查询天气预报
  10. EasyUI - Datatable转Json and Json转Datatable
  11. SRM 583 Div Level Two:IDNumberVerification
  12. AWT与Swing的区别
  13. 【工作手札】Nginx接口代理可跨域
  14. ASP.NET Core 企业开发架构概述
  15. 包装类接受string 会自动将数字类型string转换成对应得包装类型
  16. 点击导出table表格
  17. 001_ansible通过堡垒机登录
  18. win10安装Zookeeper3.4.12
  19. windows下隐藏磁盘分区
  20. HTTP 500.21 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

热门文章

  1. exit()和_exit()的区别
  2. 为鸿蒙OS说两句公道话(我对鸿蒙OS的一些看法)
  3. SQL Server 50道查询训练题,学生Student表
  4. Java设计模式(2:单一职责原则和依赖倒置原则详解)
  5. THINKPHP_(1)_修改TP源码,支持对中文字符串按拼音进行排序。
  6. ALD和CVD晶体管薄膜技术
  7. 自动机器学习(AutoML)
  8. Consistent 与 Mirrored 视角
  9. 在模仿中精进数据分析与可视化01——颗粒物浓度时空变化趋势(Mann–Kendall Test)
  10. 汉枫Wi-Fi串口服务器HF2211S应用案例