首页 技术 正文
技术 2022年11月20日
0 收藏 921 点赞 3,245 浏览 1524 个字

/*

对象的多态性。

class 动物
{}

class 猫 extends 动物
{}

class 狗 extends 动物
{}

猫 x = new 猫();

动物 x = new 猫();//一个对象,两种形态。

猫这类事物即具备者猫的形态,又具备着动物的形态。
这就是对象的多态性。

简单说:就是一个对象对应着不同类型.

多态在代码中的体现:
 父类或者接口的引用指向其子类的对象。

多态的好处:
 提高了代码的扩展性,前期定义的代码可以使用后期的内容。

多态的弊端:
 前期定义的内容不能使用(调用)后期子类的特有内容。

多态的前提:
 1,必须有关系,继承,实现。
 2,要有覆盖。

*/

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)
 {
  
//  Cat c = new Cat();
//  c.eat();
//  c.catchMouse();

Animal a = new Cat(); //自动类型提升,猫对象提升了动物类型。但是特有功能无法s访问。
       //作用就是限制对特有功能的访问。
       //专业讲:向上转型。将子类型隐藏。就不用使用子类的特有方法。

//  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
  {
  
  }
  
 }
 /*
 public static void method(Cat c)
 {
  c.eat();
 }
 public static void method(Dog d)
 { 
  
 }
 */ 
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894