多 态:
函数本身就具备多态性,某一种事物有不同的具体的体现。
体现:
父类引用或者接口的引用指向了自己的子类对象。//Animal a = new Cat();
多态的好处:1、提高了程序的扩展性,可以创建父类对象,操作共性功能,减少代码操作
2、减少类与类之间的耦合。
多态的弊端:当父类引用指向子类对象时,虽然提高了扩展性,但是只能访问父类中具备的方法,不可以访问子类中特有的方法。(前期不能使用后期产生的功能,即访问的局限性)
多态的前提:
1:必须要有关系,比如继承、或者实现。
2:通常会有覆盖操作,不能覆盖final、static、private修饰的方法,但是可以一模一样(方法名和参数列表)在子类出现,代表子类的私有方法。
class 毕姥爷
{
void 讲课( )
{
System.out.println("企业管理");
}
void 钓鱼()
{
System.out.println("钓鱼");
}
}
class 毕老师 extends 毕姥爷
{
void 讲课()
{
System.out.println("JAVA");
}
void 看电影()
{
System.out.println("看电影");
}
}
class
{
public static void main(String[] args)
{
毕姥爷 x = new 毕老师(); //毕老师对象被提升为了毕姥爷类型。
//x.讲课();
//x.看电影(); //错误.
毕老师 y = (毕老师)x; //将毕姥爷类型强制转换成毕老师类型。
y.看电影();//在多态中,自始自终都是子类对象在做着类型的变化。
}
}
-----------------------------------------------------------------------
abstract class Animal
{
abstract void eat();
}
class Dog extends Animal
{
void eat()
{
System.out.println("啃骨头");
}
void lookHome()
{
System.out.println("看家");
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("吃鱼");
}
void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Pig extends Animal
{
void eat()
{
System.out.println("饲料");
}
void gongDi()
{
System.out.println("拱地");
}
}
class DuoTaiDemo
{
public static void main(String[] args)
{
Animal a = new Cat(); //自动类型提升,猫对象提升了动物类型。但是特有功能无法访问。
//作用就是限制对特有功能的访问。
//专业讲:向上转型。将子类型隐藏。就不用使用子类的特有方法。
// a.eat();
//如果还想用具体动物猫的特有功能。
//你可以将该对象进行向下转型。
// Cat c = (Cat)a;//向下转型的目的是为了使用子类中的特有方法。
// c.eat();
// c.catchMouse();
// 注意:对于转型,自始自终都是子类对象在做着类型的变化。
// Animal a1 = new Dog();
// Cat c1 = (Cat)a1;//ClassCastException
/*
Cat c = new Cat();
// Dog d = new Dog();
// c.eat();
method(c);
// method(d);
// method(new Pig());
*/
method(new Dog());
}
public static void method(Animal a)//Animal a = new Dog(); 向上转型
{
a.eat();
if(a instanceof Cat)//instanceof:用于判断对象的具体类型。只能用于引用数据类型判断
{ // //通常在向下转型前用于健壮性的判断。
Cat c = (Cat)a;
c.catchMouse();
}
else if(a instanceof Dog)
{
Dog d = (Dog)a;
d.lookHome();
}
else
{ }
}
}
----------------------------------------------------
class A {
protected void f() {
System.out.println("A.f()");
this.g();
}
protected void g() {
System.out.println("A.g()");
}
}
class B extends A {
@Override protected void g() {
System.out.println("B.g()");
}
}
public class Ex10 {
public static void main(String[] args) {
B b = new B();
// automatic upgrade to base-class to call base-class method A.f()
// which,by polymorphism, will call the derived-class method B.g():
b.f();
}
}
结果:
A.f()
B.g()
格式:<对象 instanceof 类型> ,判断一个对象是否所属于指定的类型。
Student instanceof Person = true;//student继承了person类
多态在子父类中的成员上的体现的特点**:
成员变量 :在多态中,子父类成员变量同名。
1、在编译时期:参考的是引用型变量所属的类中是否有调用的成员。(编译时不产生对象,只检查语法错误)
2、运行时期:也是参考引用型变量所属的类中是否有调用的成员。
3、简单一句话:无论编译和运行,成员变量参考的都是引用变量所属的类中的成员变量。
再说的更容易记忆一些:成员变量 — 编译运行都看 = 左边。
成员函数
1、编译时期:参考引用型变量所属的类中是否有调用的方法。
2、运行时期:参考的是对象所属的类中是否有调用的方法。
为什么是这样的呢?因为在子父类中,对于一模一样的成员函数,有一个特性:覆盖,没有就掉基类中的函数。
3、简单一句:成员函数,编译看引用型变量所属的类,运行看对象所属的类。
更简单:成员函数 — 编译看 = 左边,运行看 = 右边。
**静态函数
1、编译时期:参考的是引用型变量所属的类中是否有调用的成员。
2、运行时期:也是参考引用型变量所属的类中是否有调用的成员。
3、为什么是这样的呢?因为静态方法,其实不所属于对象,而是所属于该方法所在的类。
调用静态的方法引用是哪个类的引用调用的就是哪个类中的静态方法。
简单说:静态函数 — 编译运行都看 = 左边。
4、因为static修饰的方法会隐藏在基类中,所以没有@override,所以多态时不会调用子类static方法
package com.Test;
public class StaticTest {
public static void main(String[] args) {
A a = new B();
a.f1();
a.f2();
}
}
class A {
public static void f1() {
System.out.println("hello! A");
}
public void f2() {
System.out.println("hello ! A");
}
}
class B extends A {
public static void f1() {
System.out.println("hello! B");
}
public void f2() {
System.out.println("hello! B");
}
}
结果: hello!A
hello!B