1.简单例子
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package array; public class ArrayTest07 { public static void main(String[] args) { // TODO Auto-generated method stub int[] array = {1, 2, 3}; for(int i = 0; i<array.length; i++) { /* int temp = array[i]; System.out.println(temp); */ System.out.println(array[i]); } //静态初始化一个长度为2的Animal类型数组 Animal a1 = new Animal(); Animal a2 = new Animal(); Animal[] animals = {a1, a2}; //对animal数组进行遍历 for(int i= 0; i<animals.length; i++) { /* Animal a = animals[i]; a.move(); */ animals[i].move(); } } }
复制代码
1
2
3
4
5
6
7package array; public class Animal { public void move() { System.out.println("Animal move..."); } }
2.Animal数组中只能存放Animal类型,不能存放其他类型,但可以存放子类
如果调用的方法是父类中存在的方法不需要向下转型。直接使用父类型引用调用即可
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package array; public class ArrayTest07 { public static void main(String[] args) { // TODO Auto-generated method stub //动态初始化一个长度为2的Animal类型数组 Animal[] ans = new Animal[2]; //创建一个Animal对象,放到数组的第一个盒子中 ans[0] = new Animal(); //Animal数组中可以存放Cat类型的数据,因为Cat是一个Animal,Cat是Animal的子类 ans[1] = new Cat(); //创建一个Animal类型的数组,数组当中存储Cat和Bird Cat c = new Cat(); Bird b = new Bird(); Animal[] anis = {c, b}; //Animal[] anis = {new Cat(), new Bird()}; //如果调用的方法是父类中存在的方法不需要向下转型。直接使用父类型引用调用即可 for(int i = 0; i<anis.length; i++) { Animal an = anis[i]; an.move(); } } }
复制代码
1
2
3
4
5
6
7package array; public class Animal { public void move() { System.out.println("Animal move..."); } }
复制代码
1
2
3
4
5
6
7
8package array; //Cat是字类 public class Cat extends Animal { public void move() {//字类重写move方法 System.out.println("猫在走猫步"); } }
复制代码
1
2
3
4
5
6
7package array; public class Bird extends Animal { public void move() {//字类重写move方法 System.out.println("Bird fly"); } }
如果调用的方法是字类特有的方法,需要向下转型
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package array; public class ArrayTest07 { public static void main(String[] args) { // TODO Auto-generated method stub //创建一个Animal类型的数组,数组当中存储Cat和Bird Cat c = new Cat(); Bird b = new Bird(); Animal[] anis = {c, b}; //Animal[] anis = {new Cat(), new Bird()}; /* //如果调用的方法是父类中存在的方法不需要向下转型。直接使用父类型引用调用即可 for(int i = 0; i<anis.length; i++) { Animal an = anis[i]; an.move(); } */ //调用字类中特有方法的话,需要向下转型 for(int i = 0; i<anis.length; i++) { if(anis[i] instanceof Cat) { Cat cat = (Cat)anis[i]; cat.catchMouse(); }else if(anis[i] instanceof Bird) { Bird bird = (Bird)anis[i]; bird.sing(); } } } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12package array; //Cat是字类 public class Cat extends Animal { public void move() {//字类重写move方法 System.out.println("猫在走猫步"); } //特有方法 public void catchMouse() { System.out.println("猫抓老鼠"); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11package array; public class Bird extends Animal { public void move() {//字类重写move方法 System.out.println("Bird fly"); } //特有方法 public void sing() { System.out.println("鸟儿在唱歌"); } }
最后
以上就是快乐音响最近收集整理的关于数组中储存引用类型的全部内容,更多相关数组中储存引用类型内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复