Java中的多态的一些体现,重写和包装类

1.异构数组

多态的表现 和double直接可以把int转换接受 int a=3;double b=a;
Animal dog=new Dog();//实例化一个Dog对象 修改前面注意 向上转换
向下转换 //类型还原
instanceof 判断属性类型,为避免类型转换异常,可以判断使用。
d instanceof Dog,判断d是不是Dog类型,如果是完成强制转换Dog d=(Dog)dog;

属于同一父类的,不同子类:

Animal 是Dog Cut 的父类

Animal dog=new Dog();//用父类的引用指向子类的实例化。
Cut cut =new Cut();
Animal [] animal=new Animal[2];
animal[0]=dog;
animal[1]=cut;

2.重写

重写的注意,在重写过程中,访问修饰符可以扩大,返回值可缩小;


父类 方法:
public Animal lingyang(Animal d){
return d;
}
子类 重写:

public Dog lingyang(Animal d){
Dog dong =(Dog)d;
return dog;
}

3.8种基本数据类型对应的包装类
int-->Integer;

double-->Double;

float-->Float;

byte-->Byte;

boolean-->Boolean;

short-->Short;

long-->Long;

char-->Character;


int b=1;
Integer c=b;---------->//将基本数据类型转换成包装类的过程就叫装箱

Integer b=1;
int c=b;----->拆箱;

 

以上都是些小知识,***

转载于:https://www.cnblogs.com/lx1154493917/p/6618797.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值