Java学习第十二章 之 自定义数据类型的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java学习第十二章 之 自定义数据类型的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2550字,纯文字阅读大概需要4分钟。
内容图文
![Java学习第十二章 之 自定义数据类型的使用](/upload/InfoBanner/zyjiaocheng/1332/d1cf27067b7e463f893d8ad8547df7fe.jpg)
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 17publicclass Test{ 18 19publicstaticvoid main(String[] args){ 20 21 Person p = new Person(); 22 23 method(p); 24 25 } 26 27//定义一个方法,把类作为参数传递 28 29publicstaticvoid method(Person p){ 30 31 p.show(); 32 33 } 34 35} 36 37/* 38 39类作为返回值 40 41*/ 42 43publicclass Animal{ 44 45publicvoid run(){ 46 47 System.out.println("类作为返回值"); 48 49 } 50 51} 52 53publicclass Test{ 54 55publicstaticvoid main(String[] args){ 56 57 Animal a = method(); 58 59 a.run(); 60 61 } 62 63publicstatic Animal method(){ 64 65 Animal a = new Animal(); 66 67return a; 68 69 } 70 71} 72 73/* 74 75 抽象类作为方法参数 76 77*/ 78 79publicabstractclass Person{ 80 81publicabstractvoid eat(); 82 83} 84 85publicclass Student extends Person{ 86 87publicvoid eat(){ 88 89 System.out.println("吃饭"); 90 91 } 92 93} 94 95publicclass Test{ 96 97publicstaticvoid main(String[] args){ 98 99 Person p = new Student(); 100101 method(p); 102103 } 104105//定义一个方法,把抽象类作为参数传递106107publicstaticvoid method(Person p){ 108109 p.eat(); 110111 } 112113} 114115/*116117抽象类作为返回值 118119*/120121publicabstractclass Animal { 122123publicabstractvoid run(); 124125} 126127publicclass Dog{ 128129publicvoid run(){ 130131 System.out.println("跑"); 132133 } 134135} 136137publicclass Test{ 138139publicstaticvoid main(String[] args){ 140141 Animal a = method(); 142143 a.run(); 144145 } 146147//定义一个方法,把抽象类作为返回值148149publicstatic Animal method(){ 150151 Animal a = new Dog(); 152153return a; 154155 } 156157} 158159/*160161接口作为方法参数 162163*/164165publicinterface Person{ 166167publicabstractvoid work(); 168169} 170171publicclass Student implements Person{ 172173publicvoid work(){ 174175 System.out.println("工作"); 176177 } 178179} 180181publicclass Test{ 182183publicstaticvoid main(String[] args){ 184185 Person p = new Student(); 186187 method(p); 188189 } 190191//定义一个方法,把接口作为参数传递192193publicstaticvoid method(Person p){ 194195 p.work(); 196197 } 198199} 200201/*202203接口作为返回值 204205*/206207publicinterface Person{ 208209publicabstractvoid work(); 210211} 212213publicclass Teacher implements Person{ 214215publicvoid work(){ 216217 System.out.println("工作"); 218219 } 220221} 222223publicclass Test{ 224225publicstaticvoid main(String[] args){ 226227 Person p = method(); 228229 p.work(); 230231 } 232233//定义一个方法,将接口作为返回值234235publicstatic Person method(){ 236237 Person p = new Teacher(); 238239return p; 240241 } 242243 }
原文:https://www.cnblogs.com/z97-/p/12622229.html
内容总结
以上是互联网集市为您收集整理的Java学习第十二章 之 自定义数据类型的使用全部内容,希望文章能够帮你解决Java学习第十二章 之 自定义数据类型的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】