Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit601c98b

Browse files
committed
Java第九天
1 parent6df4557 commit601c98b

35 files changed

+1665
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
多态的好处:
3+
A:提高了程序的维护性(由继承保证)
4+
B:提高了程序的扩展性(由多态保证)
5+
6+
多态的弊端:
7+
不能访问子类特有的功能。
8+
*/
9+
classAnimal {
10+
publicvoideat(){}
11+
publicvoidsport(){}
12+
13+
/*
14+
public void sleep() {
15+
System.out.println("爱睡觉");
16+
}
17+
*/
18+
}
19+
20+
classDogextendsAnimal {
21+
publicvoideat(){
22+
System.out.println("狗吃肉");
23+
}
24+
25+
publicvoidsport(){
26+
System.out.println("狗跑步");
27+
}
28+
29+
publicvoidlookDoor() {
30+
System.out.println("看门");
31+
}
32+
}
33+
34+
classCatextendsAnimal {
35+
publicvoideat(){
36+
System.out.println("猫吃鱼");
37+
}
38+
39+
publicvoidsport(){
40+
System.out.println("猫和老鼠捉迷藏");
41+
}
42+
}
43+
44+
classPigextendsAnimal {
45+
publicvoideat(){
46+
System.out.println("猪吃饲料");
47+
}
48+
49+
publicvoidsport(){
50+
System.out.println("猪爱睡觉");
51+
}
52+
}
53+
54+
classDuoTaiDemo {
55+
publicstaticvoidmain(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+
Dogd =newDog();
82+
Dogd2 =newDog();
83+
Dogd3 =newDog();
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+
Catc =newCat();
98+
Catc2 =newCat();
99+
Catc3 =newCat();
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+
Pigp =newPig();
114+
Pigp2 =newPig();
115+
Pigp3 =newPig();
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+
publicstaticvoidprintAnimal(Animala) {//Animal a = new Dog(); a = new Cat(); a = new Pig();
157+
a.eat();
158+
//...
159+
//a.lookDoor();
160+
a.sport();
161+
}
162+
}
Lines changed: 74 additions & 0 deletions
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+
classAnimal {
31+
publicvoideat(){}
32+
}
33+
34+
classDogextendsAnimal {
35+
publicvoideat(){
36+
System.out.println("狗吃骨头");
37+
}
38+
39+
publicvoidhelp() {
40+
System.out.println("狗可以帮助警察叔叔抓小偷");
41+
}
42+
}
43+
44+
classCatextendsAnimal {
45+
publicvoideat(){
46+
System.out.println("猫吃老鼠");
47+
}
48+
}
49+
50+
classDuoTaiDemo2 {
51+
publicstaticvoidmain(String[]args) {
52+
//多态
53+
Animala =newDog();//向上转型
54+
a.eat();
55+
//a.help(); //编译看左边
56+
57+
//向下转型
58+
Dogd = (Dog)a;
59+
d.eat();
60+
d.help();
61+
System.out.println("-------------");
62+
63+
//会报错吗?
64+
a =newCat();////向上转型
65+
a.eat();
66+
67+
Catc = (Cat)a;
68+
c.eat();
69+
70+
//Dog dd = (Dog)a; //ClassCastException
71+
//dd.eat();
72+
//dd.help();
73+
}
74+
}
2.24 MB
Binary file not shown.
1.11 KB
Binary file not shown.
1.3 KB
Binary file not shown.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
//猫狗案例
2+
classAnimal {
3+
privateStringname;
4+
privateintage;
5+
6+
publicAnimal() {}
7+
8+
publicAnimal(Stringname,intage) {
9+
this.name =name;
10+
this.age =age;
11+
}
12+
13+
publicvoidsetName(Stringname) {
14+
this.name =name;
15+
}
16+
17+
publicStringgetName() {
18+
returnname;
19+
}
20+
21+
publicvoidsetAge(intage) {
22+
this.age =age;
23+
}
24+
25+
publicintgetAge() {
26+
returnage;
27+
}
28+
29+
publicvoidshow() {
30+
System.out.println("name:"+name+",age:"+age);
31+
}
32+
33+
publicvoidsleep() {
34+
System.out.println("sleep");
35+
}
36+
37+
publicvoideat() {
38+
System.out.println("eat");
39+
}
40+
}
41+
42+
classDogextendsAnimal {
43+
publicDog() {}
44+
45+
publicDog(Stringname,intage) {
46+
super(name,age);
47+
}
48+
49+
publicvoideat() {
50+
System.out.println("dog eat");
51+
}
52+
53+
publicvoidsleep() {
54+
System.out.println("dog sleep");
55+
}
56+
}
57+
58+
classCatextendsAnimal {
59+
publicCat() {}
60+
61+
publicCat(Stringname,intage) {
62+
super(name,age);
63+
}
64+
65+
publicvoideat() {
66+
System.out.println("cat eat");
67+
}
68+
69+
publicvoidsleep() {
70+
System.out.println("cat sleep");
71+
}
72+
}
73+
74+
classAnimalDemo {
75+
publicstaticvoidmain(String[]args) {
76+
//测试Animal
77+
Animala =newAnimal();
78+
a.setName("动物");
79+
a.setAge(10);
80+
a.eat();
81+
a.sleep();
82+
a.show();
83+
System.out.println("------------");
84+
85+
Animala2 =newAnimal("动物",10);
86+
a2.eat();
87+
a2.sleep();
88+
a2.show();
89+
System.out.println("------------");
90+
91+
//测试Dog
92+
Dogd =newDog();
93+
d.setName("大黄");
94+
d.setAge(5);
95+
d.eat();
96+
d.sleep();
97+
d.show();
98+
System.out.println("------------");
99+
100+
Dogd2 =newDog("大黄",5);
101+
d2.eat();
102+
d2.sleep();
103+
d2.show();
104+
System.out.println("------------");
105+
106+
//练习1:测试Cat你们自己练习
107+
Catc =newCat();
108+
c.setName("大花猫");
109+
c.setAge(2);
110+
c.eat();
111+
c.sleep();
112+
c.show();
113+
System.out.println("------------");
114+
115+
Catc2 =newCat("大花猫",2);
116+
c2.eat();
117+
c2.sleep();
118+
c2.show();
119+
System.out.println("------------");
120+
121+
//通过Dog测试多态
122+
Animalaa =newDog();
123+
aa.setName("小黄");
124+
aa.setAge(3);
125+
aa.eat();
126+
aa.sleep();
127+
aa.show();
128+
System.out.println("------------");
129+
130+
Animalaa2 =newDog("小黄",3);
131+
aa2.eat();
132+
aa2.sleep();
133+
aa2.show();
134+
System.out.println("------------");
135+
136+
//练习2:通过Cat测试多态你们自己练习
137+
Animalaa3 =newCat("小花猫",1);
138+
aa3.eat();
139+
aa3.sleep();
140+
aa3.show();
141+
System.out.println("------------");
142+
143+
Animalaa4 =newCat();
144+
aa4.setName("小花猫");
145+
aa4.setAge(1);
146+
aa4.eat();
147+
aa4.sleep();
148+
aa4.show();
149+
}
150+
}
536 Bytes
Binary file not shown.
536 Bytes
Binary file not shown.
406 Bytes
Binary file not shown.
394 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp