面向对象---------多态(二十三)

一.多态的概述及其代码体现

* A:多态(polymorphic)概述
    * 事物存在的多种形态
* B:多态前提
    * a:要有继承关系。
    * b:要有方法重写。
    * c:要有父类引用指向子类对象。

class Demo1_Polymorphic {
	public static void main(String[] args) {
		Cat c = new Cat();
		c.eat();

		Animal a = new Cat();	//父类引用指向子类对象
		a.eat();
	}
}

class Animal {
	public void eat() {
		System.out.println("动物吃饭");
	}
}

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

二.多态中的成员访问特点之成员变量

成员变量
    * 编译看左边(父类),运行看左边(父类)。

多态中的成员变量:

三.多态中的成员访问特点之成员方法

* 成员方法
    * 编译看左边(父类),运行看右边(子类)。

多态中的成员方法:

四.多态中的成员访问特点之静态成员方法

* 静态方法
    * 编译看左边(父类),运行看左边(父类)。
    * (静态和类相关,算不上重写,所以,访问还是左边的)
    * 只有非静态的成员方法,编译看左边,运行看右边

class Demo2_Polymorphic {
	public static void main(String[] args) {
		/*Father f = new Son();			//父类引用指向子类对象
		System.out.println(f.num);

		Son s = new Son();
		System.out.println(s.num);*/

		Father f = new Son();
		//f.print();
		f.method();				//相当于是Father.method()
	}
}

class Father {
	int num = 10;
	public void print() {
		System.out.println("father");
	}

	public static void method() {
		System.out.println("father static method");
	}
}

class Son extends Father {
	int num = 20;

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

	public static void method() {
		System.out.println("son static method");
	}
}

五.举例

class Demo3_SuperMan {
	public static void main(String[] args) {
		Person p = new SuperMan();//父类引用指向子类对象,超人提升为了人
					 //父类引用指向子类对象就是向上转型
		System.out.println(p.name);
		p.谈生意();
		SuperMan sm = (SuperMan)p;			//向下转型
		sm.fly();

		/*
		基本数据类型自动类型提升和强制类型转换
		*/
		int i = 10;
		byte b = 20;
		//i = b;				//自动类型提升
		//b = (byte)i;				//强制类型转换
	}
}

class Person {
	String name = "John";
	public void 谈生意() {
		System.out.println("谈生意");
	}
}

class SuperMan extends Person {
	String name = "superMan";

	public void 谈生意() {
		System.out.println("谈几个亿的大单子");
	}

	public void fly() {
		System.out.println("飞出去救人");
	}
}

 六.多态中向上转型和向下转型

七.多态的好处和弊端

* A:多态的好处
    * a:提高了代码的维护性(继承保证)
    * b:提高了代码的扩展性(由多态保证)
* B:示例
    * 多态的好处
    * 可以当作形式参数,可以接收任意子类对象
* C:多态的弊端
    * 不能使用子类的特有属性和行为。

class Demo4_Animal {
	public static void main(String[] args) {
		//Cat c1 = new Cat();
		//c1.eat();
		method(new Cat());
		method(new Dog());

		//Animal a = new Cat();	开发的是很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中的特有属性和行为
	}
	
	//Cat c = new Dog();狗是一只猫,这是错误的
	/*public static void method(Cat c) {			
		c.eat();
	}

	public static void method(Dog d) {
		d.eat();
	}*/
	
	//如果把狗强转成猫就会出现类型转换异常,ClassCastException
	public static void method(Animal a) {	//当作参数的时候用多态最好,因为扩展性强
		//关键字 instanceof 判断前边的引用是否是后边的数据类型
		if (a instanceof Cat) {
			Cat c = (Cat)a;
			c.eat();
			c.catchMouse();
		}else if (a instanceof Dog) {
			Dog d = (Dog)a;
			d.eat();
			d.lookHome();
		}else {
			a.eat();
		}
	}
}


class Animal {
	public void eat() {
		System.out.println("动物吃饭");
	}
}

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

	public void catchMouse() {
		System.out.println("抓老鼠");
	}
}

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

	public void lookHome() {
		System.out.println("看家");
	}
}

八.多态中的面试题题目分析题

A:看下面程序是否有问题,如果没有,说出结果
*
        class Fu {
            public void show() {
                System.out.println("fu show");
            }
        }
    
        class Zi extends Fu {
            public void show() {
                System.out.println("zi show");
            }
    
            public void method() {
                System.out.println("zi method");
            }
        }
    
        class Test1Demo {
            public static void main(String[] args) {


                Fu f = new Zi();
                f.method();
                f.show();
            }
        }

 

class Test1_Polymorphic {
	public static void main(String[] args) {
		Fu f = new Zi();
		//f.method();
		f.show();
	}
}
class Fu {
	public void show() {
		System.out.println("fu show");
	}
}

class Zi extends Fu {
	public void show() {
		System.out.println("zi show");
	}

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

* B:看下面程序是否有问题,如果没有,说出结果
*
        class A {
            public void show() {
                show2();
            }
            public void show2() {
                System.out.println("我");
            }
        }
        class B extends A {
            public void show2() {
                System.out.println("爱");
            }
        }
        class C extends B {
            public void show() {
                super.show();
            }
            public void show2() {
                System.out.println("你");
            }
        }
        public class Test2DuoTai {
            public static void main(String[] args) {
                A a = new B();
                a.show();
                
                B b = new C();
                b.show();
            }
        }

 

class Test2_Polymorphic {
	public static void main(String[] args) {
		/*A a = new B();
		a.show();*/
		
		B b = new C();
		b.show();
	}
}
class A {
	public void show() {
		show2();
	}
	public void show2() {
		System.out.println("我");
	}
}
class B extends A {
	public void show() {
		show2();
	}
	public void show2() {
		System.out.println("爱");
	}
}
class C extends B {
	public void show() {
		super.show();
	}
	public void show2() {
		System.out.println("你");
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值