Java学习第十二章 之 自定义数据类型的使用

  1 /*
  2 
  3 类作为方法参数
  4 
  5 */
  6 
  7 public class Person{
  8 
  9    public void show(){
 10 
 11      System.out.println("类作为参数");
 12 
 13    }
 14 
 15 }
 16 
 17 public class Test{
 18 
 19     public static void main(String[] args){
 20 
 21         Person p = new Person();
 22 
 23         method(p);
 24 
 25     }
 26 
 27 //定义一个方法,把类作为参数传递
 28 
 29     public static void method(Person p){
 30 
 31           p.show();
 32 
 33     }
 34 
 35 }
 36 
 37 /*
 38 
 39 类作为返回值
 40 
 41 */
 42 
 43 public class Animal{
 44 
 45     public void run(){
 46 
 47       System.out.println("类作为返回值");
 48 
 49    }
 50 
 51 }
 52 
 53 public class Test{
 54 
 55    public static void main(String[] args){
 56 
 57          Animal a = method();
 58 
 59          a.run();
 60 
 61    }
 62 
 63    public static Animal method(){
 64 
 65             Animal a = new Animal();
 66 
 67             return a;
 68 
 69     }
 70 
 71 }
 72 
 73 /*
 74 
 75   抽象类作为方法参数
 76 
 77 */
 78 
 79 public abstract class Person{
 80 
 81    public abstract void eat();
 82 
 83 }
 84 
 85 public class Student extends Person{
 86 
 87     public void eat(){
 88 
 89          System.out.println("吃饭");
 90 
 91     }
 92 
 93 }
 94 
 95 public class Test{
 96 
 97     public static void main(String[] args){
 98 
 99            Person p = new Student();
100 
101            method(p);
102 
103      }
104 
105     //定义一个方法,把抽象类作为参数传递
106 
107     public static void method(Person p){
108 
109           p.eat();
110 
111      }
112 
113 }
114 
115 /*
116 
117 抽象类作为返回值
118 
119 */
120 
121 public abstract class Animal {
122 
123     public abstract void run();
124 
125 }
126 
127 public class Dog{
128 
129    public void run(){
130 
131      System.out.println("跑");
132 
133     }
134 
135 }
136 
137 public class Test{
138 
139     public static void main(String[] args){
140 
141          Animal a =  method();
142 
143         a.run();
144 
145    }
146 
147   //定义一个方法,把抽象类作为返回值
148 
149    public static Animal method(){
150 
151        Animal a = new Dog();
152 
153        return a;
154 
155    }
156 
157 }
158 
159 /*
160 
161 接口作为方法参数
162 
163 */
164 
165 public interface Person{
166 
167    public abstract void work();
168 
169 }
170 
171 public class Student implements Person{
172 
173    public void work(){
174 
175      System.out.println("工作");
176 
177    }
178 
179 }
180 
181 public class Test{
182 
183    public static  void main(String[] args){
184 
185           Person p = new Student();
186 
187           method(p);
188 
189    }
190 
191   //定义一个方法,把接口作为参数传递
192 
193    public static void method(Person p){
194 
195              p.work();
196 
197    }
198 
199 }
200 
201 /*
202 
203 接口作为返回值
204 
205 */
206 
207 public interface Person{
208 
209     public abstract void work();
210 
211 }
212 
213 public class Teacher implements Person{
214 
215      public void work(){
216 
217        System.out.println("工作");
218 
219      }
220 
221 }
222 
223 public class Test{
224 
225    public static void main(String[] args){
226 
227         Person p = method();
228 
229         p.work();
230 
231    }
232 
233    //定义一个方法,将接口作为返回值
234 
235    public static Person method(){
236 
237          Person p = new Teacher();
238 
239          return p;
240 
241    }
242 
243 }

猜你喜欢

转载自www.cnblogs.com/z97-/p/12622229.html