1. 引用类型数组:------记住它和基本类型数组的两种区别即可
- 区别1:给引用类型数组的元素赋值时,需要new个对象
- 区别2:访问引用类型数组的元素的属性/行为时,需要打点访问
public class RefArrayDemo {
public static void main(String[] args) {
Dog[] dogs = new Dog[3];
dogs[0] = new Dog("小黑",2,"黑");
dogs[1] = new Dog("小白",1,"白");
dogs[2] = new Dog("小灰",3,"灰");
System.out.println(dogs[0].name); //输出第1只狗狗的名字
dogs[1].age = 4; //修改第2只狗狗的年龄为4岁
dogs[2].swim(); //第3只狗狗在游泳
System.out.println("-------------------------");
for(int i=0;i<dogs.length;i++){ //遍历dogs数组
System.out.println(dogs[i].name); //输出每只狗狗的名字
dogs[i].eat(); //每只狗狗吃饭
}
Chick[] chicks = new Chick[2];
chicks[0] = new Chick("小花",1,"花");
chicks[1] = new Chick("大花",2,"花");
for(int i=0;i<chicks.length;i++){ //遍历chicks数组
System.out.println(chicks[i].name);
chicks[i].layEggs();
}
Fish[] fish = new Fish[4];
fish[0] = new Fish("小金",2,"金");
fish[1] = new Fish("大金",4,"白");
fish[2] = new Fish("小绿",1,"绿");
fish[3] = new Fish("小红",3,"红");
for(int i=0;i<fish.length;i++){ //遍历fish数组
System.out.println(fish[i].color);
fish[i].swim();
}
/*
//声明Dog型数组dogs,包含3个元素,每个元素都是Dog型,默认值为null
Dog[] dogs = new Dog[3];
//声明Chick型数组chicks,包含3个元素,每个元素都是Chick型,默认值为null
Chick[] chicks = new Chick[3];
//声明Fish型数组fish,包含2个元素,每个元素都是Fish型,默认值为null
Fish[] fish = new Fish[2];
*/
}
}
2. 多态:多种形态
1.向上造型/自动类型转换:
- 超类型的引用指向派生类的对象
- 能点出来什么,看引用的类型--------------这是规定,记住它
- 能向上造型成为的类型有:所实现的接口+所继承的类
2. 向下转型/强制类型转换,成功的条件只有如下两种:
- 引用所指向的对象,就是该类型
- 引用所指向的对象,实现了该接口或继承了该类
- 强转时若不符合如上条件,则发生ClassCastException类型转换异常
- 建议:在强转之前先通过instanceof来判断引用的对象是否是该类型
- 注意:instanceof返回boolean结果,它为true的条件就是强转成功的条件
- 何时需要强转:若想访问的属性/行为在超类中没有,则需要强制类型转换
public abstract class Animal {
String name;
int age;
String color;
Animal(){
}
Animal(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
void drink(){
System.out.println(color+"色的"+age+"岁的"+name+"正在喝水...");
}
abstract void eat();
}
public interface Swim {
/** 游泳 */
void swim();
}
public class Dog extends Animal implements Swim {
Dog(){
}
Dog(String name,int age,String color){
super(name,age,color);
}
void lookHome(){
System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在看家...");
}
void eat(){
System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在吃肯头...");
}
public void swim(){
System.out.println(color+"色的"+age+"岁的狗狗"+name+"正在游泳...");
}
}
public class Fish extends Animal implements Swim {
Fish(){
}
Fish(String name,int age,String color){
super(name,age,color);
}
void eat(){
System.out.println(color+"色的"+age+"岁的小鱼"+name+"正在吃小虾...");
}
public void swim(){
System.out.println(color+"色的"+age+"岁的小鱼"+name+"正在游泳...");
}
}
public class Chick extends Animal {
Chick(){
}
Chick(String name,int age,String color){
super(name,age,color);
}
void layEggs(){
System.out.println(color+"色的"+age+"岁的小鸡"+name+"正在下蛋...");
}
void eat(){
System.out.println(color+"色的"+age+"岁的小鸡"+name+"正在吃小米...");
}
}
public class Master {
void feed(Animal animal){ //喂动物,将范围扩大了,所有动物都可以
animal.eat();
}
}
package ooday04;
/** 多态的测试类 */
public class PolymorphicDemo {
public static void main(String[] args) {
/*
//演示强转成功的条件
Animal o = new Dog("小黑",2,"黑"); //向上造型
Dog g = (Dog)o; //引用o所指向的对象,就是Dog类型
Swim s = (Swim)o; //引用o所指向的对象,实现了Swim接口
//Fish f = (Fish)o; //运行时会发生ClassCastException类型转换异常
System.out.println(o instanceof Dog); //true
System.out.println(o instanceof Swim); //true
System.out.println(o instanceof Fish); //false
if(o instanceof Fish){ //false
Fish f = (Fish)o;
}
*/
//演示向上造型(多态)的第2点应用:
//Animal o = new Animal(); //编译错误,抽象类不能被实例化
Animal[] animals = new Animal[5];
animals[0] = new Dog("小黑",2,"黑"); //向上造型
animals[1] = new Dog("小白",1,"白");
animals[2] = new Fish("小金",1,"金");
animals[3] = new Fish("小花",2,"花");
animals[4] = new Chick("小灰",3,"灰");
for(int i=0;i<animals.length;i++){ //遍历所有动物
System.out.println(animals[i].name); //输出每个动物的名字
animals[i].drink(); //每个动物喝水
animals[i].eat(); //每个动物吃饭
if(animals[i] instanceof Dog){
Dog dog = (Dog)animals[i];
dog.lookHome();
}
if(animals[i] instanceof Chick){
Chick chick = (Chick)animals[i];
chick.layEggs();
}
if(animals[i] instanceof Swim){ //适用于所有实现Swim接口的(会游泳的)
Swim s = (Swim)animals[i];
s.swim();
}
}
/*
//演示向上造型(多态)的第1点应用:
Master master = new Master();
Dog dog = new Dog("小黑",2,"黑");
Chick chick = new Chick("小花",3,"花");
Fish fish = new Fish("小金",1,"金");
master.feed(dog); //在传参的同时,系统自动做了向上造型 Animal animal = dog;
master.feed(chick);
master.feed(fish);
*/
/*
//演示向上造型:
Dog o1 = new Dog("小黑",2,"黑"); //狗是狗
//o1.name/age/color/drink()/eat()/swim()/lookHome();
Animal o2 = new Dog("小黑",2,"黑"); //狗是动物----向上造型
//o2.name/age/color/drink()/eat();
Swim o3 = new Dog("小黑",2,"黑"); //狗会游泳-----向上造型
//o3.swim();
*/
}
}
3.匿名内部类:
何时用:若想创建一个派生类的对象,并且对象只创建一次,可以设计为匿名内部类,可以大大简化代码
package ooday04;
public class AnonInnerClassDemo {
public static void main(String[] args) {
//1)创建了Inter的一个派生类,但是没有名字
//2)为该派生类创建了一个对象,名为o1,向上造型为Inter类型
// ----new Inter(){};是在创建Inter的派生类的对象
//3)大括号中的为派生类的类体
Inter o1 = new Inter(){};
//1)创建了Inter的一个派生类,但是没有名字
//2)为该派生类创建了一个对象,名为o2,向上造型为Inter类型
//3)大括号中的为派生类的类体
Inter o2 = new Inter(){};
//1)创建了InterInter的一个派生类,但是没有名字
//2)为该派生类创建了一个对象,名为o3,向上造型为InterInter类型
//3)大括号中的为派生类的类体
InterInter o3 = new InterInter(){
public void show(){
System.out.println("showshow");
}
};
o3.show();
}
}
interface InterInter{
void show();
}
interface Inter{
}
补充:
1. null:表示空,没有指向任何对象。
- 若引用的值为null,则该引用不能再进行任何操作了,若操作则发生NullPointerException空指针异常。
2. 多态的实际应用:
- 将不同对象(狗、鱼、鸡)统一封装到超类数组(动物数组)中来访问,实现代码复用
- 将超类型(Animal)作为参数或返回值类型,传递或返回派生类(Dog/Fish/Chick)对象,以扩大方法的应用范围(所有Animal),实现代码复用
3. 问:内部类有独立的.class字节码文件吗?
答:有