您的位置:首页 > 其它

多态

2016-03-02 21:47 281 查看
----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------

多态:可以理解为事物存在的多种体现形态。

-----------------------------------

1,多态的体现

父类的引用指向了自己的子类对象。

//动物 x = new 猫();

----------------------------------

2,多态的前提

1) 必须是类与类之间有关系。要么继承(extends),要么实现(implements)。

2) 通常还有一个前提:存在覆盖(重写)。

3)父类引用指向自己的子类对象

----------------------------------

3,多态的好处

预先定义的程序可以运行后期程序的内容。

多态的出现大大的提高程序的扩展性。

----------------------------------

4,多态的弊端:

虽然可以预先使用,但是只能访问父类中已有的功能,运行的是后期子类的功能内容。不能预先使用子类中定义的特有功能。

----------------------------------

5,多态的应用

 

在多态中成员函数的特点:

 

1)对于成员函数:// class Fu{}    class Zi extends Fu{}    Fu f = new Zi(); f.method();  

编译时期:看左边。

运行时期:看右边。

 

在编译时期:参阅引用型变量所属的类中是否有调用的方法。如果有,编译通过,如果没有编译失败。

在运行时期:参阅对象所属的类中是否有调用的方法。

 

2) 对于成员变量(静态成员变量)的特点:

    无论编译和运行,都参考左边(引用型变量所属的类)。

 

[java] view plain copy

public class DuoTai {  

   

public static void main(String[] args) {  

// TODO Auto-generated method stub  

Fu f = new Zi();  

System.out.println("num="+f.num+",num1="+f.num1);  

}  

   

}  

class Fu  

{  

 int num = 6;  

 static int num1 = 7;  

}  

   

   

class Zi extends Fu  

{  

 int num = 88;  

 static int num1 = 99;  

}  

 

3)静态函数:java的静态函数和对象无关,只和声明的类有关,静态函数由类直接引用。

 

[java] view plain copy

class Fu  

{  

void method1()  

{  

System.out.println("fu method_1");  

}  

void method2()  

{  

System.out.println("fu method_2");  

}  

static void method4()  

{  

System.out.println("fu method_4");  

}  

}  

   

   

class Zi extends Fu  

{  

void method1()  

{  

System.out.println("zi method_1");  

}  

void method3()  

{  

System.out.println("zi method_3");  

}  

   

static void method4()  

{  

System.out.println("zi method_4");  

}  

}  

class  DuoTaiDemo4  

{  

public static void main(String[] args)   

{  

//  Fu f = new Zi();  

//  

//  System.out.println(f.num);  

//  

//  Zi z = new Zi();  

   

//f.method1();  

//f.method2();  

//f.method3();  

   

Fu f = new Zi();  

f.method4();  

   

Zi z = new Zi();  

z.method4();  

}  

}  

 

4)向下转型

[java] view plain copy

class Fu{}  

   

class Zi  extends Fu{}  

Fu  fu= new Zi();   //Zi向上转型为了Fu。向上转型,一般多态都采用这种。  

   

Zi  zi = (Zi)fu;    //将代表Zi对象的父类引用fu强制转换成了Zi类型。向下转型。  

 

----------------------------------

 

 

例子1:(抽象类实现多态,有分析过程):

 

[java] view plain copy

abstract class Animal  

{  

abstract void eat();  

   

}  

   

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 kanJia()  

{  

System.out.println("看家");  

}  

}  

   

class DuoTaiDemo1   

{  

public static void main(String[] args)   

{  

//Animal a = new Cat();//类型提升。 向上转型。  

//a.eat();  

   

//如果想要调用猫的特有方法时,如何操作?  

//强制将父类的引用。转成子类类型。向下转型。  

///Cat c = (Cat)a;  

//c.catchMouse();  

//千万不要出现这样的操作,就是将父类对象转成子类类型。  

//我们能转换的是父类应用指向了自己的子类对象时,该应用可以被提升,也可以被强制转换。  

//多态自始至终都是子类对象在做着变化。  

//  Animal a = new Animal();  

//  Cat c = (Cat)a;  

function(new Dog());  

function(new Cat());  

   

   

}  

   

public static void function(Animal a)//Animal a = new Cat();  

{  

a.eat();  

if(a instanceof Cat)  

{  

Cat c = (Cat)a;  

c.catchMouse();  

}  

else if(a instanceof Dog)  

{  

Dog c = (Dog)a;  

c.kanJia();  

}  

   

   

/* 

instanceof : 用于判断对象的类型。 对象 intanceof 类型(类类型 接口类型)   

*/  

}  

   

   

}  

 

----------------------------------

 

 

例子2:(利用抽象类实现多态):

[java] view plain copy

/* 

基础班学生: 

学习,睡觉。 

高级班学生: 

学习,睡觉。 

  

可以将这两类事物进行抽取。 

  

*/  

   

abstract class Student  

{  

public abstract void study();  

public void sleep()  

{  

System.out.println("躺着睡");  

}  

}  

   

class DoStudent  

{  

public void doSome(Student stu)  

{  

stu.study();  

stu.sleep();  

}  

}  

   

class BaseStudent extends Student  

{  

public void study()  

{  

System.out.println("base study");  

}  

public void sleep()  

{  

 System.out.println("坐着睡");  

}  

}  

   

class AdvStudent extends Student  

{  

public void study()  

{  

System.out.println(" adv study");  

}  

}  

   

   

   

   

class  DuoTaiDemo2  

{  

public static void main(String[] args)   

{  

   

DoStudent ds = new DoStudent();  

ds.doSome(new BaseStudent());  

ds.doSome(new AdvStudent());  

   

//  BaseStudent bs = new BaseStudent();  

//  bs.study();  

//  bs.sleep();  

//  AdvStudent as = new AdvStudent();  

//  as.study();  

//  as.sleep();  

}  

   

}  

   

----------------------------------

 

例子3:(利用接口实现多态):

 

[java] view plain copy

/* 

需求: 

电脑运行实例, 

电脑运行基于主板。 

*/  

   

   

interface PCI  

{  

public void open();  

public void close();  

}  

   

class MainBoard  

{  

public void run()  

{  

System.out.println("mainboard run ");  

}  

public void usePCI(PCI p)//PCI p = new NetCard()//接口型引用指向自己的子类对象。  

{  

if(p!=null)  

{  

p.open();  

p.close();  

}  
}  

}  

   

   

class NetCard implements PCI  

{  

public void open()  

{  

System.out.println("netcard open");  

}  

public void close()  

{  

System.out.println("netcard close");  

method();  

}  

}  

   

class SoundCard implements PCI  

{  

public void open()  

{  

System.out.println("SoundCard open");  

}  

public void close()  

{  

System.out.println("SoundCard close");  

}  

}  

   

   

class DuoTaiDemo3   

{  

public static void main(String[] args)   

{  

MainBoard mb = new MainBoard();  

mb.run();  

mb.usePCI(null);  

mb.usePCI(new NetCard());  

mb.usePCI(new SoundCard());  

}  
}  

----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: