@liruiyi962464
2017-03-20T11:22:56.000000Z
字数 3847
阅读 468
java
- 父类同一个方法 不同的子类调用它 因为方法体不同 产生不同的执行结果
- 需要存在继承和实现关系
- 同样的方法调用而执行不同的操作、运行不同的代码(重写操作)
- 在运行是父类或者接口的引用变量可以引用其子类的对象
- 多态通过分离做什么和怎么做,从另外一个角度讲接口和实现进行分离
- ‘多态’则消除了类型之间的耦合关系
- 多态的存在提高了程序的扩展性和后期的可维护性
- 子类到父类是向上转型,父类到子类是向下转型
- 向上转型比较专用类型向通用类型转换
- 向上转型是安全的
- 向上转型过程中 类可能会丢失方法 而不是获取方法
- 比较安全的操作是向下转型 不安全(需要instanceof操作符)
- 向下转换前,使用instanceof来判断一下,以免出ClassCastException(类转换错误)
父类(Anmial)
public class Animal {
public void run(){
System.out.println("奔跑。。");
}
}
子类(Dog)
public class Dog extends Animal{=
public void run(){
System.out.println("Dog 奔跑");
}
public void eat(){
System.out.println("Dog 饿啦。。。");
}
}
子类(Cat)
public class Cat extends Animal {
public void run() {
System.out.println("Cat 奔跑");
}
}
测试类(Test)
public static void main(String[] args) {
//向上转型:子类对象转换成父类对象。
。--。。------。-。-。-。--。。-。-。-
Dog dog = new Dog();
Cat cat = new Cat();
dog.eat();
//向上转型
Animal animal = (Animal)cat;
animal.run();
//animal.eat();//向上转型中 丢失了子类中的eat()方法
//向下转型:父类对象转换成子类对象
if(animal instanceof Dog){//如果是Dog 就走这个方法
Dog dog1 = (Dog)animal;
dog1.eat();
dog1.run();
}else if(animal instanceof Cat){//如果是Cat 就走这个方法
Cat cat1 = (Cat)animal;
cat1.eat();
cat1.run();
}
}
MyPrint父类
public class MyPrint {
public void print(){
System.out.println("打印机打印");
}
}
子类MyPrint
public class ColorPrint extends MyPrint{
public void print(){
System.out.println("彩色打印机打印机打印出彩色纸张!!!");
}
}
子类MyPrint
public class BlackWhitePrint extends MyPrint{
public void print(){
System.out.println("黑白打印机打印机打印出黑白纸张!!!");
}
}
操作人Printer(操作父类)
public class Printer {
public void startPrint(MyPrint mp){
mp.print();
}
}
测试
public class Test {
public static void main(String[] args) {
Printer pt = new Printer();
ColorPrint cp = new ColorPrint();
pt.startPrint(cp);
BlackWhitePrint bwp = new BlackWhitePrint();
pt.startPrint(bwp);
}
}
public class Engineer {
private String name;
public Engineer() {}
public Engineer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void work(Product p){}
}
public class SoftwareEngineer extends Engineer{
public SoftwareEngineer() {}
public SoftwareEngineer(String name) {
super(name);
}
@Override
public void work(Product p) {
// TODO Auto-generated method stub
System.out.println("软件工程师");
}
}
public class HardwareEngineer extends Engineer{
public HardwareEngineer() {}
public HardwareEngineer(String name) {
super(name);
}
@Override
public void work(Product p) {
// TODO Auto-generated method stub
System.out.println("硬件工程师");
}
}
public class Product {
private String name;
public Product() {}
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class SoftwareProduct extends Product{
public SoftwareProduct() {}
public SoftwareProduct(String name) {
super(name);
}
}
public class HaardwareProduct extends Product{
public HaardwareProduct() {}
public HaardwareProduct(String name) {
super(name);
}
}
public class Manager {
private String name;
public Manager() {}
public Manager(String name) {
this.name = name;
}
public void AssignWork(Engineer e,Product p){
if(e instanceof SoftwareEngineer){
if(p instanceof SoftwareProduct){
System.out.println(this.name+"派送的"+e.getName()+"会维护"+p.getName());
}else{
System.out.println(this.name+"派送的"+e.getName()+"不会"+p.getName());
}
}else if(e instanceof HardwareEngineer){
if(p instanceof HaardwareProduct){
System.out.println(this.name+"派送的"+e.getName()+"会维护"+p.getName());
}else{
System.out.println(this.name+"派送的"+e.getName()+"不会"+p.getName());
}
}else{
System.out.println("没有这个工程师");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
//公司经理
Manager manager = new Manager("洛神");
//工程师
Engineer se = new SoftwareEngineer("软件工程师--->lisi--->");
Engineer he = new HardwareEngineer("硬件工程师--->zhangsan--->");
//产品
Product sp = new SoftwareProduct("ERP");
Product hp = new HaardwareProduct("网卡");
manager.AssignWork(se, sp);
manager.AssignWork(se, hp);
manager.AssignWork(he, hp);
manager.AssignWork(he, sp);
}
}