博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA 8 多态
阅读量:6962 次
发布时间:2019-06-27

本文共 3172 字,大约阅读时间需要 10 分钟。

面向对象的第三个特征,多态:

 
可以理解为事物存在的多种状态。
 
1,多态的体现:父类的引用指向了自己的子类,父类的引用可以接收子类对象。
Animal c1 = new Cat();
  c1.eat();
  function(new Dog());
  function(new Pig());
 
 public static void function (Animal c)
 {
  c.eat();
 }
 
 
2,多态的前提
        必须是类与类之间有关系,要么是继承,要么是实现。
        通常还有前提,存在覆盖。
3,多态的应用
 
4,多态的好处
        大大的提高了程序的扩展性
 
5,弊端
        提高了扩展性,但是只能使用父类的引用访问父类中的成员。
 
例子程序:
 
class Zx
{
 public static void main(String []arr)
 {
 
  Animal c1 = new Cat();
  c1.eat();
  function(new Dog());
  function(new Pig());
 }
 
 
 public static void function (Animal c)
 {
  c.eat();
 }
}
 
abstract class Animal
{
 public 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 Pig extends Animal
{
 public void eat()
 {
  System.out.println("饲料");
 }
 public void gongDi()
 {
  System.out.println("公地");
 }
 
修改之前的
 
class Zx
{
 public static void main(String []arr)
 {
  Cat c = new Cat();
  Cat c1 = new Cat();
  function(c);
  function(new Dog());
  function(new Pig());
 }
 public static void function (Cat c)
 {
  c.eat();
 }
 
 public static void function (Dog c)
 {
  c.eat();
 }
 
 public static void function (Pig c)
 {
  c.eat();
 }
}
 
abstract class Animal
{
 public 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 Pig extends Animal
{
 public void eat()
 {
  System.out.println("饲料");
 }
 public void gongDi()
 {
  System.out.println("公地");
 }
}
 
 
 
需求:
 Animal a= new Cat();
  a.eat();
想调用猫的特有方法,强制将父类的引用转成子类的类型,向下转型。
Cat c =(Cat) a;
c.catch.Mouse;
 
但是不可以
Animal a = new Animal();
Cat c = (Cat)a;
多态自始至终都是子类在变化。
 
 
关键字 instanceof:
判断 对象的引用是否指向某类型
 
Cat m = new Cat();
 
m instanceof Cat;   true
a  instanceof Dog; false
 
 
多态中成员函数(非静态)的特点: 
在编译时期:参阅引用类型所属的类中是否有调用的方法,如果有,则编译通过,否则失败。
在运行时期:查阅对象所属的类中是否有调用的方法。
成员函数在多态调用是,编译看左边,运行看右边。
 
在多态中,成员变量(和静态方法)特点:
无论编译还是运行,成员变量都参考左边。 
 
 
 
 
多态的应用举例:
 
 
class Zx
{
 public static void main(String []arr)
 {
 
  MainBoard mb = new MainBoard();
  mb.ran();
  mb.usePCI(null);
  mb.usePCI(new NetCard());
  mb.usePCI(new MuCard());
 
 }
}
 
class MainBoard
{
 public void ran()
 {
  System.out.println("main board ran");
 
 }
 public void usePCI(PCI P)
 /*接口型引用指向自己的子类对象,接口不可以创建对象,多态的应用之一,
  提高了程序的扩展性,降低了耦合性*/
 {
  if(P!=null)
  {
   P.open();
   P.close();
  }
 
 }
 
 
 
}
 
interface PCI
{
 public void open();
 public void close();
 
}
class NetCard implements PCI
{
 public void open()
 {
  System.out.println("net open");
 }
 public void close()
 {
  System.out.println("net close");
 }
}
 
class MuCard implements PCI
{
 public void open()
 {
  System.out.println("mu open");
 }
 public void close()
 {
  System.out.println("mu close");
 }
}
 
 
 
 
 
扩展性举例:
 
需求:数据库的操作
 
1,连接数据库
2,操作数据库
3,关闭数据库连接
 
 
下面写 UserInfoDao ui = new UserInfoByJDBC(); 即可
 
 
Object 类:
 
是所有对象的直接或者间接父类。
该类中定义的是所有所有对象都具备的功能。
 
class Demo //默认 extends Object
{
}
 
Object.squals(Object obj); //返回值为 true or false 当二者地址值相同为true
 
 
Object.toString();//返回该对象的字符表示   返回   “类名@地址值”地址值为16进制
Object.hanshCode //返回int   十进制的 地址值
 
 
 

转载于:https://www.cnblogs.com/hitxx/p/4675970.html

你可能感兴趣的文章
Django滚动logger
查看>>
负载均衡笔记
查看>>
Maven的使用,Nexus建立本地仓库以及Eclipse导入Maven项目(三): 配置篇
查看>>
装系统
查看>>
如何准备BAT技术面试答案(上)——Java研发方向
查看>>
查找算法(1)--二分查找
查看>>
apache tomcat负载均衡总结
查看>>
深入理解Tomcat系列之二:源码调试环境搭建
查看>>
java简易聊天程序
查看>>
Redis-Cluster实战-
查看>>
Maven提高篇系列之四——使用Profile
查看>>
配置sonar、jenkins进行持续审查
查看>>
如何进行桌面的重定向?
查看>>
趣图:调试过多线程的都会懂!
查看>>
Hadoop 2.0 NameNode HA和Federation实践
查看>>
调用ptyhon函数
查看>>
leetcode 25: Reverse Nodes in k-Group
查看>>
CentOS7上安装mysql8.0.13报 error while loading shared libraries: libaio.so.1
查看>>
Java多线程系列--“基础篇”08之 join()
查看>>
equals方法的重写代码实例
查看>>