LY.JAVA面向对象编程.形式参数和返回值、包的概述、修饰符、内部类

2018-07-09 13:29:16

运动员和教练案例

  1 /*
  2     教练和运动员案例(学生分析然后讲解)
  3         乒乓球运动员和篮球运动员。
  4         乒乓球教练和篮球教练。
  5         为了出国交流,跟乒乓球相关的人员都需要学习英语。
  6         请用所学知识:
  7         分析,这个案例中有哪些抽象类,哪些接口,哪些具体类。
  8     
  9     整个分析过程,我是通过画图讲解的。    
 10 */
 11 //定义一个说英语的接口
 12 interface SpeakEnglish {
 13     //说英语
 14     public abstract void speak();
 15 }
 16 
 17 //定义人的抽象类
 18 abstract class Person {
 19     private String name;
 20     private int age;
 21     
 22     public Person() {}
 23     
 24     public Person(String name,int age) {
 25         this.name = name;
 26         this.age = age;
 27     }
 28     
 29     public String getName() {
 30         return name;
 31     }
 32     
 33     public void setName(String name) {
 34         this.name = name;
 35     }
 36     
 37     public int getAge() {
 38         return age;
 39     }
 40     
 41     public void setAge(int age) {
 42         this.age = age;
 43     }
 44     
 45     //睡觉
 46     public void sleep() {
 47         System.out.println("人都是要睡觉的");
 48     }
 49     
 50     //吃饭
 51     public abstract void eat();
 52 }
 53 
 54 //定义运动员抽象类
 55 abstract class Player extends Person {
 56     public Player() {}
 57     
 58     public Player(String name,int age) {
 59         super(name,age);
 60     }
 61     
 62     //学习
 63     public abstract void study();
 64 }
 65 
 66 //定义教练抽象类
 67 abstract class Coach extends Person {
 68     public Coach() {}
 69     
 70     public Coach(String name,int age) {
 71         super(name,age);
 72     }
 73     
 74     //
 75     public abstract void teach();
 76 }
 77 
 78 //定义乒乓球运动员具体类
 79 class PingPangPlayer extends Player implements SpeakEnglish {
 80     public PingPangPlayer(){}
 81     
 82     public PingPangPlayer(String name,int age) {
 83         super(name,age);
 84     }
 85     
 86     //
 87     public void eat() {
 88         System.out.println("乒乓球运动员吃大白菜,喝小米粥");
 89     }
 90     
 91     //学习
 92     public void study() {
 93         System.out.println("乒乓球运动员学习如何发球和接球");
 94     }
 95     
 96     //说英语
 97     public void speak() {
 98         System.out.println("乒乓球运动员说英语");
 99     }
100 }
101 
102 //定义篮球运动员具体类
103 class BasketballPlayer extends Player {
104     public BasketballPlayer(){}
105     
106     public BasketballPlayer(String name,int age) {
107         super(name,age);
108     }
109     
110     //
111     public void eat() {
112         System.out.println("篮球运动员吃牛肉,喝牛奶");
113     }
114     
115     //学习
116     public void study() {
117         System.out.println("篮球运动员学习如何运球和投篮");
118     }
119 }
120 
121 //定义乒乓球教练具体类
122 class PingPangCoach extends Coach implements SpeakEnglish {
123     public PingPangCoach(){}
124     
125     public PingPangCoach(String name,int age) {
126         super(name,age);
127     }
128     
129     //
130     public void eat() {
131         System.out.println("乒乓球教练吃小白菜,喝大米粥");
132     }
133     
134     //
135     public void teach() {
136         System.out.println("乒乓球教练教如何发球和接球");
137     }
138     
139     //说英语
140     public void speak() {
141         System.out.println("乒乓球教练说英语");
142     }
143 }
144 
145 //定义篮球教练具体类
146 class BasketballCoach extends Coach {
147     public BasketballCoach(){}
148     
149     public BasketballCoach(String name,int age) {
150         super(name,age);
151     }
152     
153     //
154     public void eat() {
155         System.out.println("篮球教练吃羊肉,喝羊奶");
156     }
157     
158     //
159     public void teach() {
160         System.out.println("篮球教练教如何运球和投篮");
161     }
162 }
163 
164 class InterfaceDemo {
165     public static void main(String[] args) {
166         //测试运动员(乒乓球运动员和篮球运动员)
167         //乒乓球运动员
168         PingPangPlayer ppp = new PingPangPlayer();
169         ppp.setName("王浩");
170         ppp.setAge(33);
171         System.out.println(ppp.getName()+"---"+ppp.getAge());
172         ppp.eat();
173         ppp.sleep();
174         ppp.study();
175         ppp.speak();
176         System.out.println("----------------");
177         //通过带参构造给数据(留给你们)
178         
179         //篮球运动员
180         BasketballPlayer bp = new BasketballPlayer();
181         bp.setName("姚明");
182         bp.setAge(34);
183         System.out.println(bp.getName()+"---"+bp.getAge());
184         bp.eat();
185         bp.sleep();
186         bp.study();
187         //bp.speak(); //没有该方法
188         
189         //测试教练自己做
190     }
191 }

形式参数:

  基本类型:

  引用类型:

      类:

      抽象类:

      接口:

 1 /*
 2     形式参数:
 3         基本类型(太简单,不是我今天要讲解的)
 4         引用类型
 5             类名:(匿名对象的时候其实我们已经讲过了) 需要的是该类的对象
 6             抽象类:
 7             接口
 8 */
 9 class Student {
10     public void study() {
11         System.out.println("Good Good Study,Day Day Up");
12     }
13 }
14 
15 class StudentDemo {
16     public void method(Student s) { //ss; ss = new Student();  Student s = new Student();
17         s.study();
18     }
19 }
20 
21 class StudentTest {
22     public static void main(String[] args) {
23         //需求:我要测试Student类的study()方法
24         Student s = new Student();
25         s.study();
26         System.out.println("----------------");
27         
28         //需求2:我要测试StudentDemo类中的method()方法
29         StudentDemo sd = new StudentDemo();
30         Student ss = new Student();
31         sd.method(ss);
32         System.out.println("----------------");
33         
34         //匿名对象用法
35         new StudentDemo().method(new Student());
36     }
37 }

 形式参数:抽象类

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

 1 /*
 2     形式参数:
 3         基本类型(太简单,不是我今天要讲解的)
 4         引用类型
 5             类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象
 6             抽象类:需要的是该抽象的类子类对象
 7             接口
 8 */
 9 abstract class Person {
10     public abstract void study();
11 }
12 
13 class PersonDemo {
14     public void method(Person p) {//p; p = new Student();  Person p = new Student(); //多态
15         p.study();
16     }
17 }
18 
19 //定义一个具体的学生类
20 class Student extends Person {
21     public void study() {
22         System.out.println("Good Good Study,Day Day Up");
23     }
24 }
25 
26 class PersonTest {
27     public static void main(String[] args) {
28         //目前是没有办法的使用的
29         //因为抽象类没有对应的具体类
30         //那么,我们就应该先定义一个具体类
31         //需求:我要使用PersonDemo类中的method()方法
32         PersonDemo pd = new PersonDemo();
33         Person p = new Student();
34         pd.method(p);
35     }
36 }

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

 1 /*
 2     形式参数:
 3         基本类型(太简单,不是我今天要讲解的)
 4         引用类型
 5             类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象
 6             抽象类:需要的是该抽象的类子类对象
 7             接口:需要的是该接口的实现类对象
 8 */
 9 //定义一个爱好的接口
10 interface Love {
11     public abstract void love();
12 }
13 
14 class LoveDemo {
15     public void method(Love l) { //l; l = new Teacher();  Love l = new Teacher(); 多态
16         l.love();
17     }
18 }
19 
20 //定义具体类实现接口
21 class Teacher implements Love {
22     public void love() {
23         System.out.println("老师爱学生,爱Java,爱林青霞");
24     }
25 }
26 
27 class TeacherTest {
28     public static void main(String[] args) {
29         //需求:我要测试LoveDemo类中的love()方法
30         LoveDemo ld = new LoveDemo();
31         Love l = new Teacher();
32         ld.method(l);
33     }
34 }

 返回值类型

    基本类型:

    引用类型:

        类:返回的是该类的对象

        抽象类:返回的是该抽象类的子类对象

        接口:

返回值类型  引用类型:类:返回的是该类的对象

 返回值类型  引用类型:抽象类:返回的是该抽象类的子类对象

猜你喜欢

转载自www.cnblogs.com/twinkle-star/p/9283431.html