자바 실험 프로젝트 3-애완동물 상점

Program:애 완 동물 상점 의 디자인(계승,인터페이스,선형 표)
 
설명:본 문 제 는 도형 사용자 인터페이스 가 실현 되 지 않 았 고 프로젝트 구조 설명 은 다음 과 같다.
 
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 pets = new ArrayList();     //    
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 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 pets = new ArrayList();//    
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 ite = this.pets.iterator();
39         while( ite.hasNext() ) {
40             System.out.println( ite.next() );
41         }
42     }
43 }                                            

 
다음으로 전송:https://www.cnblogs.com/caizhen/p/7701923.html

좋은 웹페이지 즐겨찾기