java基础,传参与返回值

一 参数

1,类名作为参数

 
  1. package com.ma.demo;

  2.  
  3.  
  4. public class People {

  5. void show() {

  6. System.out.println("people show");

  7. }

  8. }

  9. ---

  10. package com.ma.demo;

  11.  
  12.  
  13. public class PeopleDemo {

  14. void method(People p) {

  15. p.show();

  16. }

  17. }

  18. ---

  19. package com.ma.demo;

  20.  
  21.  
  22. public class Test {

  23. public static void main(String[] args) {

  24. People p = new People();

  25. PeopleDemo pd = new PeopleDemo();

  26. pd.method(p);

  27. }

  28. }

  29. ----

  30.  




2,抽象类做为参数

 
  1. package com.ma.demo2;

  2.  
  3.  
  4. abstract class People {

  5. abstract void show();

  6. }

  7. ---

  8. package com.ma.demo2;

  9.  
  10.  
  11. public class PeopleDemo {

  12. void method(People p) {

  13. p.show();

  14. }

  15. }

  16. ---

  17. package com.ma.demo2;

  18.  
  19.  
  20. public class Student extends People {

  21.  
  22.  
  23. @Override

  24. void show() {

  25. // TODO Auto-generated method stub

  26. System.out.println("student show");

  27. }

  28.  
  29.  
  30. }

  31. ---

  32. package com.ma.demo2;

  33.  
  34.  
  35. public class Test {

  36. public static void main(String[] args) {

  37. People p = new Student();

  38. PeopleDemo pd = new PeopleDemo();

  39. pd.method(p);

  40. }

  41. }

  42. ------

  43.  
  44.  



3,接口做为参数

 
  1. package com.ma.demo3;

  2.  
  3.  
  4. interface People {

  5. public abstract void show();

  6. }

  7. ---

  8. package com.ma.demo3;

  9.  
  10.  
  11. public class PeopleDemo {

  12. void method(People p) {

  13. p.show();

  14. }

  15. }

  16. ---

  17. package com.ma.demo3;

  18.  
  19.  
  20. public class Student implements People {

  21.  
  22.  
  23. @Override

  24. public void show() {

  25. // TODO Auto-generated method stub

  26. System.out.println("student show");

  27. }

  28.  
  29.  
  30. }

  31. ---

  32. package com.ma.demo3;

  33.  
  34.  
  35. public class Test {

  36. public static void main(String[] args) {

  37. People p = new Student();

  38. PeopleDemo pd = new PeopleDemo();

  39. pd.method(p);

  40. }

  41. }

  42. ------

  43.  





 

二.返回值

 

1,类名作为返回值

 
  1. package com.ma.demo4;

  2.  
  3.  
  4. public class People {

  5. void show() {

  6. System.out.println("people show");

  7. }

  8. }

  9. ---

  10. package com.ma.demo4;

  11.  
  12.  
  13. public class PeopleDemo {

  14. People method() {

  15. return new People();

  16. }

  17. }

  18. ---

  19. package com.ma.demo4;

  20.  
  21.  
  22. public class Test {

  23. public static void main(String[] args) {

  24. PeopleDemo pd = new PeopleDemo();

  25.  
  26.  
  27. People p = pd.method();

  28. p.show();

  29. }

  30. }

  31. ------



2,抽象类作为返回值

 
  1. package com.ma.demo5;

  2.  
  3.  
  4. abstract class People {

  5. public abstract void show();

  6. }

  7. ---

  8. package com.ma.demo5;

  9.  
  10.  
  11. public class PeopleDemo {

  12. Student method() {

  13. return new Student();

  14. }

  15. }

  16. ---

  17. package com.ma.demo5;

  18.  
  19.  
  20. public class Student extends People {

  21.  
  22.  
  23. @Override

  24. public void show() {

  25. // TODO Auto-generated method stub

  26. System.out.println("student show");

  27. }

  28.  
  29.  
  30. }

  31. ---

  32. package com.ma.demo5;

  33.  
  34.  
  35. public class Test {

  36. public static void main(String[] args) {

  37. PeopleDemo pd = new PeopleDemo();

  38. People p = pd.method();

  39. p.show();

  40. }

  41. }

  42. ----



3,接口作为返回值

 
  1. package com.ma.demo6;

  2.  
  3.  
  4. interface People {

  5. public abstract void show();

  6. }

  7. --

  8. package com.ma.demo6;

  9.  
  10.  
  11. public class PeopleDemo {

  12. People method() {

  13. return new Student();

  14. }

  15. }

  16. --

  17. package com.ma.demo6;

  18.  
  19.  
  20. public class Student implements People {

  21.  
  22.  
  23. @Override

  24. public void show() {

  25. // TODO Auto-generated method stub

  26. System.out.println("student show");

  27. }

  28.  
  29.  
  30. }

  31. ----

  32. package com.ma.demo6;

  33.  
  34.  
  35. public class Test {

  36. public static void main(String[] args) {

  37. PeopleDemo pd = new PeopleDemo();

  38. People p = pd.method();

  39. p.show();

  40. }

  41. }

  42. ----

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值