Skip to content

Commit 601c98b

Browse files
committed
Java第九天
1 parent 6df4557 commit 601c98b

35 files changed

+1665
-0
lines changed

day09/code/01_多态/DuoTaiDemo.java

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
多态的好处:
3+
A:提高了程序的维护性(由继承保证)
4+
B:提高了程序的扩展性(由多态保证)
5+
6+
多态的弊端:
7+
不能访问子类特有的功能。
8+
*/
9+
class Animal {
10+
public void eat(){}
11+
public void sport(){}
12+
13+
/*
14+
public void sleep() {
15+
System.out.println("爱睡觉");
16+
}
17+
*/
18+
}
19+
20+
class Dog extends Animal {
21+
public void eat(){
22+
System.out.println("狗吃肉");
23+
}
24+
25+
public void sport(){
26+
System.out.println("狗跑步");
27+
}
28+
29+
public void lookDoor() {
30+
System.out.println("看门");
31+
}
32+
}
33+
34+
class Cat extends Animal {
35+
public void eat(){
36+
System.out.println("猫吃鱼");
37+
}
38+
39+
public void sport(){
40+
System.out.println("猫和老鼠捉迷藏");
41+
}
42+
}
43+
44+
class Pig extends Animal {
45+
public void eat(){
46+
System.out.println("猪吃饲料");
47+
}
48+
49+
public void sport(){
50+
System.out.println("猪爱睡觉");
51+
}
52+
}
53+
54+
class DuoTaiDemo {
55+
public static void main(String[] args) {
56+
/*
57+
//我喜欢狗,养了一只狗
58+
Dog d = new Dog();
59+
d.eat();
60+
d.sport();
61+
62+
//我很喜欢狗,又养了一只狗
63+
Dog d2 = new Dog();
64+
d2.eat();
65+
d2.sport();
66+
67+
//我特别喜欢狗,又养了一只狗
68+
Dog d3 = new Dog();
69+
d3.eat();
70+
d3.sport();
71+
*/
72+
73+
//我...
74+
//我要养很多只狗,而且,每个狗对象都要调用一些方法,或者还要做一些操作。
75+
//并且这些操作是一样的。仅仅是狗对象不一样。
76+
//如果一直写下去,代码会变得非常麻烦
77+
//所以,我们要考虑对这个代码进行优化
78+
//如何优化呢?因为发现操作是一样的,就是对象不一样
79+
//所以,我们就准备使用方法改进,把对象作为参数传递过来即可。
80+
//有了方法后,我们就可以如下调用
81+
Dog d = new Dog();
82+
Dog d2 = new Dog();
83+
Dog d3 = new Dog();
84+
85+
//printDog(d);
86+
//printDog(d2);
87+
//printDog(d3);
88+
printAnimal(d);
89+
printAnimal(d2);
90+
printAnimal(d3);
91+
System.out.println("-----------");
92+
93+
//我喜欢猫,很喜欢猫,特别喜欢猫
94+
//...
95+
//写一个方法实现吧
96+
//调用
97+
Cat c = new Cat();
98+
Cat c2 = new Cat();
99+
Cat c3 = new Cat();
100+
101+
//printCat(c);
102+
//printCat(c2);
103+
//printCat(c3);
104+
printAnimal(c);
105+
printAnimal(c2);
106+
printAnimal(c3);
107+
System.out.println("-----------");
108+
109+
//我后来养了一只猪,请问我要写成一样的代码,该如何实现呢?
110+
//A:先创建猪类继承自动物类
111+
//B:在测试类里面写方法实现
112+
//C:在测试类中创建对象,调用方法即可
113+
Pig p = new Pig();
114+
Pig p2 = new Pig();
115+
Pig p3 = new Pig();
116+
117+
//printPig(p);
118+
//printPig(p2);
119+
//printPig(p3);
120+
printAnimal(p);
121+
printAnimal(p2);
122+
printAnimal(p3);
123+
124+
//我还喜欢狼,要养狼
125+
//步骤会跟刚才一样,我们自己也是可以完成的
126+
//我还喜欢猴子,...
127+
//我们来分析一下:
128+
//我们重新定义一个新的类是没有任何问题的,我们继续在测试类中的main方法中调用也是没有问题的
129+
//我们不应该去在测试类中添加新的功能
130+
//以后,我们再写一个程序的时候,有一个基本的设计原则:对扩展开放,对修改关闭。
131+
//但是,我们现在如果要完成这个程序,还就得去修改测试类
132+
//为了不去修改测试类中除了main方法以外的内容
133+
//我们需要对下面的几个方法进行一个分析:
134+
}
135+
136+
/*
137+
public static void printDog(Dog d) {
138+
d.eat();
139+
//...
140+
d.sport();
141+
}
142+
143+
public static void printCat(Cat c) {
144+
c.eat();
145+
//...
146+
c.sport();
147+
}
148+
149+
public static void printPig(Pig p) {
150+
p.eat();
151+
//...
152+
p.sport();
153+
}
154+
*/
155+
156+
public static void printAnimal(Animal a) { //Animal a = new Dog(); a = new Cat(); a = new Pig();
157+
a.eat();
158+
//...
159+
//a.lookDoor();
160+
a.sport();
161+
}
162+
}

day09/code/01_多态/DuoTaiDemo2.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
如何访问子类特有功能呢?
3+
A:创建子类对象即可。
4+
B:多态的转型问题
5+
向上转型
6+
从子到父
7+
父类引用指向子类对象
8+
向下转型
9+
从父到子
10+
父类引用转为子类对象
11+
12+
孔子装爹案例
13+
14+
孔子:教书():讲论语,玩游戏():连连看,age:20
15+
孔子爹:教书():JavaSE,age:40
16+
17+
//多态
18+
孔子爹 k爹 = new 孔子(); //Android很火,而JavaSE是Android基础,所以JavaSE很火。
19+
//爹太忙,儿子装爹去讲课。粘上小胡子,带上眼镜。向上转型
20+
System.out.println(k爹.age); //40
21+
k爹.教书(); //讲论语
22+
//k爹.玩游戏(); //报错
23+
24+
//回家了
25+
孔子 k = (孔子)k爹; //去掉小胡子,去掉眼镜。向下转型
26+
System.out.println(k.age); //20
27+
k.教书(); //讲论语
28+
k.玩游戏();
29+
*/
30+
class Animal {
31+
public void eat(){}
32+
}
33+
34+
class Dog extends Animal {
35+
public void eat(){
36+
System.out.println("狗吃骨头");
37+
}
38+
39+
public void help() {
40+
System.out.println("狗可以帮助警察叔叔抓小偷");
41+
}
42+
}
43+
44+
class Cat extends Animal {
45+
public void eat(){
46+
System.out.println("猫吃老鼠");
47+
}
48+
}
49+
50+
class DuoTaiDemo2 {
51+
public static void main(String[] args) {
52+
//多态
53+
Animal a = new Dog(); //向上转型
54+
a.eat();
55+
//a.help(); //编译看左边
56+
57+
//向下转型
58+
Dog d = (Dog)a;
59+
d.eat();
60+
d.help();
61+
System.out.println("-------------");
62+
63+
//会报错吗?
64+
a = new Cat(); ////向上转型
65+
a.eat();
66+
67+
Cat c = (Cat)a;
68+
c.eat();
69+
70+
//Dog dd = (Dog)a; //ClassCastException
71+
//dd.eat();
72+
//dd.help();
73+
}
74+
}
Binary file not shown.
1.11 KB
Binary file not shown.
1.3 KB
Binary file not shown.
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
//猫狗案例
2+
class Animal {
3+
private String name;
4+
private int age;
5+
6+
public Animal() {}
7+
8+
public Animal(String name,int age) {
9+
this.name = name;
10+
this.age = age;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setAge(int age) {
22+
this.age = age;
23+
}
24+
25+
public int getAge() {
26+
return age;
27+
}
28+
29+
public void show() {
30+
System.out.println("name:"+name+",age:"+age);
31+
}
32+
33+
public void sleep() {
34+
System.out.println("sleep");
35+
}
36+
37+
public void eat() {
38+
System.out.println("eat");
39+
}
40+
}
41+
42+
class Dog extends Animal {
43+
public Dog() {}
44+
45+
public Dog(String name,int age) {
46+
super(name,age);
47+
}
48+
49+
public void eat() {
50+
System.out.println("dog eat");
51+
}
52+
53+
public void sleep() {
54+
System.out.println("dog sleep");
55+
}
56+
}
57+
58+
class Cat extends Animal {
59+
public Cat() {}
60+
61+
public Cat(String name,int age) {
62+
super(name,age);
63+
}
64+
65+
public void eat() {
66+
System.out.println("cat eat");
67+
}
68+
69+
public void sleep() {
70+
System.out.println("cat sleep");
71+
}
72+
}
73+
74+
class AnimalDemo {
75+
public static void main(String[] args) {
76+
//测试Animal
77+
Animal a = new Animal();
78+
a.setName("动物");
79+
a.setAge(10);
80+
a.eat();
81+
a.sleep();
82+
a.show();
83+
System.out.println("------------");
84+
85+
Animal a2 = new Animal("动物",10);
86+
a2.eat();
87+
a2.sleep();
88+
a2.show();
89+
System.out.println("------------");
90+
91+
//测试Dog
92+
Dog d = new Dog();
93+
d.setName("大黄");
94+
d.setAge(5);
95+
d.eat();
96+
d.sleep();
97+
d.show();
98+
System.out.println("------------");
99+
100+
Dog d2 = new Dog("大黄",5);
101+
d2.eat();
102+
d2.sleep();
103+
d2.show();
104+
System.out.println("------------");
105+
106+
//练习1:测试Cat你们自己练习
107+
Cat c = new Cat();
108+
c.setName("大花猫");
109+
c.setAge(2);
110+
c.eat();
111+
c.sleep();
112+
c.show();
113+
System.out.println("------------");
114+
115+
Cat c2 = new Cat("大花猫",2);
116+
c2.eat();
117+
c2.sleep();
118+
c2.show();
119+
System.out.println("------------");
120+
121+
//通过Dog测试多态
122+
Animal aa = new Dog();
123+
aa.setName("小黄");
124+
aa.setAge(3);
125+
aa.eat();
126+
aa.sleep();
127+
aa.show();
128+
System.out.println("------------");
129+
130+
Animal aa2 = new Dog("小黄",3);
131+
aa2.eat();
132+
aa2.sleep();
133+
aa2.show();
134+
System.out.println("------------");
135+
136+
//练习2:通过Cat测试多态你们自己练习
137+
Animal aa3 = new Cat("小花猫",1);
138+
aa3.eat();
139+
aa3.sleep();
140+
aa3.show();
141+
System.out.println("------------");
142+
143+
Animal aa4 = new Cat();
144+
aa4.setName("小花猫");
145+
aa4.setAge(1);
146+
aa4.eat();
147+
aa4.sleep();
148+
aa4.show();
149+
}
150+
}

day09/code/02_多态案例/Cat.class

536 Bytes
Binary file not shown.

day09/code/02_多态案例/Dog.class

536 Bytes
Binary file not shown.
406 Bytes
Binary file not shown.
394 Bytes
Binary file not shown.
385 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)