LENOVO-PC_刘甲波
多态的引用
1.可替换性 2.可扩充性 3.接口性 4.灵活性 5.简化性
多态的使用:父类引用作为方法参数的情况
public class Pet {
private String name;
public Pet() {
this.name="无名氏";
}
public void sound() {
}
}
public class Dog extends Pet {
public void sound() {
System.out.println("25252525252525");
}
}
public class Cat extends Pet {
public void sound() {
System.out.println("喵喵喵");
}
}
import java.util.Random;
public class PetShop {
public static void petSound(Dog dog) {
dog.sound();
}
public static void petSound(Cat cat) {
cat.sound();
}
public static void main(String[] args) {
Pet pets[]={
new Dog(),
new Cat()
};
int index=new Random().nextInt(pets.length);
if(pets[index] instanceof Dog) {
petSound((Dog)pets[index]);
}else if (pets[index] instanceof Cat) {
petSound((Cat)pets[index]);
}
}
}
分析:PetShop中为每个子类都定义了petSound方法,表示子类的叫声,如果增加子类会导致添加新的叫声方法,不利于扩展性。
重构:解决办法就是定义统一的一个声方法,将父类Pet作为方法参数
import java.util.Random;
public class PetShop {
public static void petSound(Pet pet) {
pet.sound();
}
public static void petSound(Cat cat) {
cat.sound();
}
public static void main(String[] args) {
Pet pets[]=new Pet[]{
new Dog(),
new Cat()
};
int index=new Random().nextInt(pets.length);
petSound(pets[index]);
}
}
多态的使用:父类作为方法返回值的情况
public class PetShop {
//父类引用作为方法参数时,可以传入该类的任意子类对象,实现多态
public static void petSound(Pet pet) {
pet.sound();
}
public static Dog getDog() {
return new Dog("大黑");
}
public static Cat getCat() {
return new Cat("加菲");
}
public static void main(String[] args) {
Pet pets[] = new Pet[] {
new Dog("大黄"),
new Cat("花花"),
new Dog("小黄"),
new Cat("波斯")
};
int index = new Random().nextInt(pets.length);
petSound(pets[index]);
Dog dog = PetShop.getDog();
Cat cat = PetShop.getCat();
}
}
分析 :如果增加了宠物的种类,那么就需要在Petshop中增高进货的方法。不利于扩展。解决的办法是定义一个统一的进货方法,方法返回Pet类型,方法参数设计参数设计为进货的种类。
public class PetShop {
public static void petSound(Pet pet) {
pet.sound();
}
public static Dog getDog() {
return new Dog();
}
public static Cat getCat() {
return new Cat();
}
public static void main(String[] args) {
Pet pets[]=new Pet[] {
new Dog(),
new Cat()
};
int index = new Random().nextInt(pets.length);
petSound(pets[index]);
Dog dog=PetShop.getDog();
Cat cat = PetShop.getCat();
}
}
public class PetShop {
//父类引用作为方法参数时,可以传入该类的任意子类对象,实现多态
public static void petSound(Pet pet) {
pet.sound();
}
//父类引用作为方法返回值时,可以返回该类的任意子类对象,实现多态
public static Pet getPet(String breed) {
Pet pet = null;
switch(breed) {
case "dog":
pet = new Dog();
break;
case "cat":
pet = new Cat();
break;
}
return pet;
}
public static void main(String[] args) {
Pet pets[] = new Pet[] {
new Dog("大黄"),
new Cat("花花"),
new Dog("小黄"),
new Cat("波斯")
};
int index = new Random().nextInt(pets.length);
petSound(pets[index]);
Pet pet = PetShop.getPet("dog");
System.out.println("新进的货物是"+pet.toString());
}
}
分析:父类引用作为方法的返回值时,可以返回该类的任意子类对象,实现多态。
抽象
抽象类
什么是抽象类?
如果类使用abstracte关键字修饰,那么该类就是抽象类。
例如:
public abstract calss Pet{
}
什么是抽象方法
方法中使用abstract关键字修饰,那么该方法就是抽象方法,抽象方法不能有{},
抽象类和抽象方法的规则
1.抽象类中可以包含普通方法或者抽象方法
2.抽象类中可以定义实例属性或者静态属性
3.如果以个类中包含抽象方法,那么这个类就是抽象类
4.抽象类中不一定包含抽象方法
5.抽象类不允许实例化。因为抽象类是半成品,因为抽象类的抽象方法没有方法体。
为什么使用抽象类?
父类通过抽象方法让子类遵守父类的规范
继承:java语言是单继承的,也就是一个类只能有一个直接父类。
近期评论