java9-2形式参数

1、形式参数:
    基本类型(太简单)
    引用类型
    类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象
    抽象类:需要的是该抽象的类子类对象
    接口:需要的是该接口的实现类对象

A、 类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象 (这个例子就是这个)

 1 class student{
 2 public void study(){
 3 System.out.println("===学习===");
 4 }
 5 }
 6 
 7 class teacher{
 8 public void method(student s)//这里引用了一个类,它所需要的是这个类的对象
 9 {
10 System.out.println("---教导---");
11 }
12 }
13 
14 class StudentTest1 {
15 public static void main(String[] args) {
16 //调用student类中的study方法
17 student t = new student();
18 t.study();
19 System.out.println("----------");
20 //调用teacher类的method方法
21 teacher tc = new teacher();
22 //teacher类的method方法 引用了一个类,它所需要的是这个类的对象
23 student t1 = new student();
24 tc.method(t1);//它所需要的是这个类的对象
25 System.out.println("----------");
26 //匿名对象的用法调用teacher类的method方法
27 new teacher().method(new student());
28 }
29 
30 }

 

B、 抽象类:需要的是该抽象的类子类对象

 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 //目前是没有办法的使用的
21 //因为抽象类没有对应的具体类
22 //那么,我们就应该先定义一个具体类
23 //需求:我要使用PersonDemo类中的method()方法
24 PersonDemo pd = new PersonDemo();
25 Person p = new Student();
26 pd.method(p);
27 }
28 }

 

C、 接口:需要的是该接口的实现类对象

 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 }

 

posted @ 2016-09-11 07:17  卡拉瓦  阅读(293)  评论(0编辑  收藏  举报