面向对象之形式参数与返回值问题

1.形式参数为类名时:需要的是该类的对象

程序代码如下:

   1: class Student {
   2:     public void study() {
   3:         System.out.println("Good Good Study,Day Day Up");
   4:     }
   5: }
   6:  
   7: class StudentDemo {
   8:     public void method(Student s) { //ss; ss = new Student();  Student s = new Student();
   9:         s.study();
  10:     }
  11: }
  12:  
  13: class StudentTest {
  14:     public static void main(String[] args) {
  15:         //需求:我要测试Student类的study()方法
  16:         Student s = new Student();
  17:         s.study();
  18:         System.out.println("----------------");
  19:         
  20:         //需求2:我要测试StudentDemo类中的method()方法
  21:         StudentDemo sd = new StudentDemo();
  22:         Student ss = new Student();
  23:         sd.method(ss);
  24:         System.out.println("----------------");
  25:         
  26:         //匿名对象用法
  27:         new StudentDemo().method(new Student());
  28:     }
  29: }

2.形式参数为抽象类时:需要的是该抽象的类子类对象

程序代码如下:

   1: abstract class Person {
   2:     public abstract void study();
   3: }
   4:  
   5: class PersonDemo {
   6:     public void method(Person p) {//p; p = new Student();  Person p = new Student(); //多态
   7:         p.study();
   8:     }
   9: }
  10:  
  11: //定义一个具体的学生类
  12: class Student extends Person {
  13:     public void study() {
  14:         System.out.println("Good Good Study,Day Day Up");
  15:     }
  16: }
  17:  
  18: class PersonTest {
  19:     public static void main(String[] args) {
  20:         //需求:我要使用PersonDemo类中的method()方法
  21:         PersonDemo pd = new PersonDemo();
  22:         Person p = new Student();
  23:         pd.method(p);
  24:     }
  25: }

3.形式参数为接口名时:需要该接口的实现类对象

程序代码如下:

   1: //定义一个爱好的接口
   2: interface Love {
   3:     public abstract void love();
   4: }
   5:  
   6: class LoveDemo {
   7:     public void method(Love l) { //l; l = new Teacher();  Love l = new Teacher(); 多态
   8:         l.love();
   9:     }
  10: }
  11:  
  12: //定义具体类实现接口
  13: class Teacher implements Love {
  14:     public void love() {
  15:         System.out.println("老师爱学生,爱Java,爱林青霞");
  16:     }
  17: }
  18:  
  19: class TeacherTest {
  20:     public static void main(String[] args) {
  21:         //需求:我要测试LoveDemo类中的love()方法
  22:         LoveDemo ld = new LoveDemo();
  23:         Love l = new Teacher();
  24:         ld.method(l);
  25:     }
  26: }

4.返回值类型为类名时:返回的是该类的对象

程序代码如下:

   1: class Student {
   2:     public void study() {
   3:         System.out.println("Good Good Study,Day Day Up");
   4:     }
   5: }
   6:  
   7: class StudentDemo {
   8:     public Student getStudent() {
   9:         //Student s = new Student();
  10:         //Student ss = s;
  11:         
  12:         //Student s = new Student();
  13:         //return s;
  14:         return new Student();
  15:     }
  16: }
  17:  
  18: class StudentTest2 {
  19:     public static void main(String[] args) {
  20:         //需求:我要使用Student类中的study()方法
  21:         //但是,这一次我的要求是,不要直接创建Student的对象
  22:         //让你使用StudentDemo帮你创建对象
  23:         StudentDemo sd = new StudentDemo();
  24:         Student s = sd.getStudent(); //new Student(); Student s = new Student();
  25:         s.study();
  26:     }
  27: }

5.返回值类型为抽象类名时:返回的是该类的子类对象

程序代码如下:

   1: abstract class Person {
   2:     public abstract void study();
   3: }
   4:  
   5: class PersonDemo {
   6:     public Person getPerson() {
   7:         //Person p = new Student();
   8:         //return p;
   9:         
  10:         return new Student();
  11:     }
  12: }
  13:  
  14: class Student extends Person {
  15:     public void study() {
  16:         System.out.println("Good Good Study,Day Day Up");
  17:     }
  18: }
  19:  
  20: class PersonTest2 {
  21:     public static void main(String[] args) {
  22:         //需求:我要测试Person类中的study()方法
  23:         PersonDemo pd = new PersonDemo();
  24:         Person p = pd.getPerson(); //new Student();  Person p = new Student(); 多态
  25:         p.study();
  26:     }
  27: }

6.返回值类型为接口名时:返回的是该接口的实现类的对象

程序代码如下:

   1: //定义一个爱好的接口
   2: interface Love {
   3:     public abstract void love();
   4: }
   5:  
   6: class LoveDemo {
   7:     public Love getLove() {
   8:         //Love l = new Teacher();
   9:         //return l;
  10:         
  11:         return new Teacher();
  12:     }
  13: }
  14:  
  15: //定义具体类实现接口
  16: class Teacher implements Love {
  17:     public void love() {
  18:         System.out.println("老师爱学生,爱Java,爱林青霞");
  19:     }
  20: }
  21:  
  22: class TeacherTest2 {
  23:     public static void main(String[] args) {
  24:         //如何测试呢?
  25:         LoveDemo ld = new LoveDemo();
  26:         Love l = ld.getLove(); //new Teacher(); Love l = new Teacher(); 多态
  27:         l.love();
  28:     }
  29: }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值