0922-----homework-part1---about generic

  1 package com.kai.li;
  2 
  3 //Client
  4 public class Text1{
  5     public static void main(String[] args){
  6 
  7         //Outter class's instance
  8         Outter o=new Outter();
  9 
 10         //The first question
 11         //Imitate an class of Music text
 12         System.out.println();
 13         System.out.println("Following is the first question.");
 14         Outter.Music m=o.new Music();
 15         Outter.Instrument<String> i=o.new Wind<String>("Wind");
 16         m.tune(i);
 17         i=o.new Brass<String>("Brass");
 18         m.tune(i);
 19         System.out.println();
 20         
 21         //The second question
 22         System.out.println("Following is the second question.");
 23         Outter.Monkey<String,Integer> monkey=o.new People<String,Integer>("kaili",31);
 24         monkey.speak();
 25         monkey.think();
 26         System.out.println();
 27 
 28         //The third question
 29         System.out.println("Following is the third question.");
 30         Outter.Human<String,Integer> human=o.new Man<String,Integer>("Aorbeme",60);
 31         human.people();
 32         Outter.Man<String,Integer> man=o.new Man<String,Integer>("lim",2); 
 33         if(human instanceof Outter.Man<?,?>)
 34             man=(Outter.Man<String,Integer>)human;
 35         man.people();
 36         man.work();
 37         human=o.new Human<String,Integer>("father",100);
 38         human.people();
 39     }
 40 }
 41 
 42 //Outter class of all the questions
 43 class Outter{
 44     
 45     //The first question
 46     //Imitate three class by Instrument,Wind and Brass about music instrument
 47     //The following is instrument class
 48     class Instrument<T>{
 49         protected void play(){
 50             System.out.println("I can play~");
 51         }
 52         //the function following will be override by his children class
 53         //it's an empty funcion
 54         protected void play2(){}
 55     }
 56     //The following is Wind class
 57     class Wind<T> extends Instrument<T>{
 58         private T name;
 59         Wind(T name){
 60             this.name=name;
 61         }
 62         public T getName(){
 63             return name;
 64         }
 65         public void play(){
 66             System.out.println("I am "+getName()+",I can play with wu... wu... wu... !");
 67         }
 68         public void play2(){
 69             System.out.println("I can play as the Wind");
 70         }
 71     }
 72     //The following is Brass class
 73     class Brass<T> extends Instrument<T>{
 74         private T name;
 75         Brass(T name){
 76             this.name = name;
 77         }
 78         public T getName(){
 79             return name;
 80         }
 81         public void play(){
 82             System.out.println("I am "+getName()+",I can play with ba... ba... ba...");
 83         }
 84         public void play2(){
 85             System.out.println("I can play as the Brass");
 86         }
 87     }
 88     //the following is Music class only have one function by text
 89     class Music{
 90         public void tune(Instrument<? extends String> i){
 91             i.play();
 92             i.play2();
 93         }
 94     }
 95 
 96     //the following is the second question
 97     class Monkey<T2,T3>{
 98         private T2 name;
 99         private T3 age;
100         
101         protected Monkey(T2 name){
102             this.name=name;
103         }
104         protected Monkey(T2 name,T3 age){
105             this(name);
106             this.age=age;
107         }
108         protected void setName(T2 name){
109             this.name=name;
110         }
111         protected void setAge(T3 age){
112             this.age=age;
113         }
114         protected T2 getName(){
115             return name;
116         }
117         protected T3 getAge(){
118             return age;
119         }
120         protected void speak(){
121             System.out.println("yi ya... yi ya... yi ya...");
122         }
123         //using at override
124         protected void think(){}
125     }
126     class People<T2,T3> extends Monkey<T2,T3>{
127         
128         public People(T2 name){
129             super(name);
130         }
131         public People(T2 name,T3 age){
132             super(name,age);
133         }
134         public void speak(){
135             System.out.println("中国的!小子,你哪的?");
136         }
137         public void think(){
138             System.out.println("I am a person,I can think.");
139             System.out.println("So,you can keep your mouth.");
140             System.out.println("Then, do think!");
141         }
142     }
143 
144     //following is the third question
145     class Human<T4,T5>{
146         private T4 name;
147         private T5 age;
148         protected Human(T4 name,T5 age){
149             this.name=name;
150             this.age=age;
151         }
152         protected void setName(T4 name){
153             this.name=name;
154         }
155         protected void setAge(T5 age){
156             this.age=age;
157         }
158         protected T4 getName(){
159             return name;
160         }    
161         protected T5 getAge(){
162             return age;
163         }
164         protected void people(){
165             System.out.println("men and women are person");
166         }
167     }
168     class Man<T4,T5> extends Human<T4,T5>{
169         public Man(T4 name,T5 age){
170             super(name,age);
171         }
172         public void people(){
173             System.out.println("i am a man");
174         }
175         public void work(){
176             System.out.println("a man should work hard");
177         }
178     }
179     class Women<T4,T5> extends Human<T4,T5>{
180         public Women(T4 name,T5 age){
181             super(name,age);
182         }
183         public void people(){
184             System.out.println("i am a women");
185         }
186         public void work(){
187             System.out.println("a women can work hard as a man");
188         }
189     }
190 }

 

posted @ 2016-09-22 10:45  火山林风  阅读(148)  评论(0编辑  收藏  举报