Skip to content

Commit

Permalink
Java第九天
Browse files Browse the repository at this point in the history
  • Loading branch information
chao420456 committed Nov 25, 2015
1 parent 6df4557 commit 601c98b
Show file tree
Hide file tree
Showing 35 changed files with 1,665 additions and 0 deletions.
162 changes: 162 additions & 0 deletions day09/code/01_多态/DuoTaiDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
多态的好处:
A:提高了程序的维护性(由继承保证)
B:提高了程序的扩展性(由多态保证)
多态的弊端:
不能访问子类特有的功能。
*/
class Animal {
public void eat(){}
public void sport(){}

/*
public void sleep() {
System.out.println("爱睡觉");
}
*/
}

class Dog extends Animal {
public void eat(){
System.out.println("狗吃肉");
}

public void sport(){
System.out.println("狗跑步");
}

public void lookDoor() {
System.out.println("看门");
}
}

class Cat extends Animal {
public void eat(){
System.out.println("猫吃鱼");
}

public void sport(){
System.out.println("猫和老鼠捉迷藏");
}
}

class Pig extends Animal {
public void eat(){
System.out.println("猪吃饲料");
}

public void sport(){
System.out.println("猪爱睡觉");
}
}

class DuoTaiDemo {
public static void main(String[] args) {
/*
//我喜欢狗,养了一只狗
Dog d = new Dog();
d.eat();
d.sport();
//我很喜欢狗,又养了一只狗
Dog d2 = new Dog();
d2.eat();
d2.sport();
//我特别喜欢狗,又养了一只狗
Dog d3 = new Dog();
d3.eat();
d3.sport();
*/

//我...
//我要养很多只狗,而且,每个狗对象都要调用一些方法,或者还要做一些操作。
//并且这些操作是一样的。仅仅是狗对象不一样。
//如果一直写下去,代码会变得非常麻烦
//所以,我们要考虑对这个代码进行优化
//如何优化呢?因为发现操作是一样的,就是对象不一样
//所以,我们就准备使用方法改进,把对象作为参数传递过来即可。
//有了方法后,我们就可以如下调用
Dog d = new Dog();
Dog d2 = new Dog();
Dog d3 = new Dog();

//printDog(d);
//printDog(d2);
//printDog(d3);
printAnimal(d);
printAnimal(d2);
printAnimal(d3);
System.out.println("-----------");

//我喜欢猫,很喜欢猫,特别喜欢猫
//...
//写一个方法实现吧
//调用
Cat c = new Cat();
Cat c2 = new Cat();
Cat c3 = new Cat();

//printCat(c);
//printCat(c2);
//printCat(c3);
printAnimal(c);
printAnimal(c2);
printAnimal(c3);
System.out.println("-----------");

//我后来养了一只猪,请问我要写成一样的代码,该如何实现呢?
//A:先创建猪类继承自动物类
//B:在测试类里面写方法实现
//C:在测试类中创建对象,调用方法即可
Pig p = new Pig();
Pig p2 = new Pig();
Pig p3 = new Pig();

//printPig(p);
//printPig(p2);
//printPig(p3);
printAnimal(p);
printAnimal(p2);
printAnimal(p3);

//我还喜欢狼,要养狼
//步骤会跟刚才一样,我们自己也是可以完成的
//我还喜欢猴子,...
//我们来分析一下:
//我们重新定义一个新的类是没有任何问题的,我们继续在测试类中的main方法中调用也是没有问题的
//我们不应该去在测试类中添加新的功能
//以后,我们再写一个程序的时候,有一个基本的设计原则:对扩展开放,对修改关闭。
//但是,我们现在如果要完成这个程序,还就得去修改测试类
//为了不去修改测试类中除了main方法以外的内容
//我们需要对下面的几个方法进行一个分析:
}

/*
public static void printDog(Dog d) {
d.eat();
//...
d.sport();
}
public static void printCat(Cat c) {
c.eat();
//...
c.sport();
}
public static void printPig(Pig p) {
p.eat();
//...
p.sport();
}
*/

public static void printAnimal(Animal a) { //Animal a = new Dog(); a = new Cat(); a = new Pig();
a.eat();
//...
//a.lookDoor();
a.sport();
}
}
74 changes: 74 additions & 0 deletions day09/code/01_多态/DuoTaiDemo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
如何访问子类特有功能呢?
A:创建子类对象即可。
B:多态的转型问题
向上转型
从子到父
父类引用指向子类对象
向下转型
从父到子
父类引用转为子类对象
孔子装爹案例
孔子:教书():讲论语,玩游戏():连连看,age:20
孔子爹:教书():JavaSE,age:40
//多态
孔子爹 k爹 = new 孔子(); //Android很火,而JavaSE是Android基础,所以JavaSE很火。
//爹太忙,儿子装爹去讲课。粘上小胡子,带上眼镜。向上转型
System.out.println(k爹.age); //40
k爹.教书(); //讲论语
//k爹.玩游戏(); //报错
//回家了
孔子 k = (孔子)k爹; //去掉小胡子,去掉眼镜。向下转型
System.out.println(k.age); //20
k.教书(); //讲论语
k.玩游戏();
*/
class Animal {
public void eat(){}
}

class Dog extends Animal {
public void eat(){
System.out.println("狗吃骨头");
}

public void help() {
System.out.println("狗可以帮助警察叔叔抓小偷");
}
}

class Cat extends Animal {
public void eat(){
System.out.println("猫吃老鼠");
}
}

class DuoTaiDemo2 {
public static void main(String[] args) {
//多态
Animal a = new Dog(); //向上转型
a.eat();
//a.help(); //编译看左边

//向下转型
Dog d = (Dog)a;
d.eat();
d.help();
System.out.println("-------------");

//会报错吗?
a = new Cat(); ////向上转型
a.eat();

Cat c = (Cat)a;
c.eat();

//Dog dd = (Dog)a; //ClassCastException
//dd.eat();
//dd.help();
}
}
Binary file added day09/code/01_多态/多态转型内存图解.bmp
Binary file not shown.
Binary file added day09/code/02_多态案例/Animal.class
Binary file not shown.
Binary file added day09/code/02_多态案例/AnimalDemo.class
Binary file not shown.
150 changes: 150 additions & 0 deletions day09/code/02_多态案例/AnimalDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//猫狗案例
class Animal {
private String name;
private int age;

public Animal() {}

public Animal(String name,int age) {
this.name = name;
this.age = age;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setAge(int age) {
this.age = age;
}

public int getAge() {
return age;
}

public void show() {
System.out.println("name:"+name+",age:"+age);
}

public void sleep() {
System.out.println("sleep");
}

public void eat() {
System.out.println("eat");
}
}

class Dog extends Animal {
public Dog() {}

public Dog(String name,int age) {
super(name,age);
}

public void eat() {
System.out.println("dog eat");
}

public void sleep() {
System.out.println("dog sleep");
}
}

class Cat extends Animal {
public Cat() {}

public Cat(String name,int age) {
super(name,age);
}

public void eat() {
System.out.println("cat eat");
}

public void sleep() {
System.out.println("cat sleep");
}
}

class AnimalDemo {
public static void main(String[] args) {
//测试Animal
Animal a = new Animal();
a.setName("动物");
a.setAge(10);
a.eat();
a.sleep();
a.show();
System.out.println("------------");

Animal a2 = new Animal("动物",10);
a2.eat();
a2.sleep();
a2.show();
System.out.println("------------");

//测试Dog
Dog d = new Dog();
d.setName("大黄");
d.setAge(5);
d.eat();
d.sleep();
d.show();
System.out.println("------------");

Dog d2 = new Dog("大黄",5);
d2.eat();
d2.sleep();
d2.show();
System.out.println("------------");

//练习1:测试Cat你们自己练习
Cat c = new Cat();
c.setName("大花猫");
c.setAge(2);
c.eat();
c.sleep();
c.show();
System.out.println("------------");

Cat c2 = new Cat("大花猫",2);
c2.eat();
c2.sleep();
c2.show();
System.out.println("------------");

//通过Dog测试多态
Animal aa = new Dog();
aa.setName("小黄");
aa.setAge(3);
aa.eat();
aa.sleep();
aa.show();
System.out.println("------------");

Animal aa2 = new Dog("小黄",3);
aa2.eat();
aa2.sleep();
aa2.show();
System.out.println("------------");

//练习2:通过Cat测试多态你们自己练习
Animal aa3 = new Cat("小花猫",1);
aa3.eat();
aa3.sleep();
aa3.show();
System.out.println("------------");

Animal aa4 = new Cat();
aa4.setName("小花猫");
aa4.setAge(1);
aa4.eat();
aa4.sleep();
aa4.show();
}
}
Binary file added day09/code/02_多态案例/Cat.class
Binary file not shown.
Binary file added day09/code/02_多态案例/Dog.class
Binary file not shown.
Binary file added day09/code/02_多态案例/NorthPerson.class
Binary file not shown.
Binary file added day09/code/02_多态案例/Person.class
Binary file not shown.
Binary file added day09/code/02_多态案例/PersonDemo.class
Binary file not shown.
Loading

0 comments on commit 601c98b

Please sign in to comment.