向上向下转型
- 向上转型:即子类转换成父类
- 向下转型:父类转换成子类是不允许的,即Java不支持向下转型,但是如果该父类本身就是由子类转换而来,则可向下转型
- 类型转换带来的效用就是多态
- 类型转换前后的对象引用的方法皆为子类的方法(即使子类重写过父类的方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class animal1 { public void eat() { System.out.println("You can eat!"); } } public class human extends animal1{ public void eat() { System.out.println("I can eat!"); } public static void main(String[] args) { human obj1 = new human(); obj1.eat(); animal1 obj2 = (animal1) obj1; obj2.eat(); human obj3 = (human) obj2; obj3.eat(); } }
|
输出:
I can eat!
I can eat!
I can eat!
泛型类
- 泛型类是一种特殊的类。前面说类型的转换只能向上不能向下,这就给会造成一些不必要的麻烦或是安全问题,为此,泛型类应运而生.
- 语法:class+类名+{}
这个T可以代表任何类型,看例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class man1 <T>{ public T a; public man1(T a) { this.a = a; } public T get() { return a; } } public class human{ public static void main(String[] args) { man1 obj1 = new man1(1); man1 obj2 = new man1(1.1); man1 obj3 = new man1("abc"); man1 obj4 = new man1(true); System.out.println("a="+obj1.get()); System.out.println("a="+obj2.get()); System.out.println("a="+obj3.get()); System.out.println("a="+obj4.get()); } }
|
输出:
a=1
a=1.1
a=abc
a=true
泛型接口
1 2 3
| public interfase man1 <T>{ public T eat(); }
|
- 实现这个接口的方法时,如果没有泛型实参传入 ,则需要在声明这个类时,声明该类为泛型类
1 2 3 4 5 6
| lass human<T> implements man1<T>{ @Override public T eat() { return null; } }
|
1 2 3 4 5 6 7
| class human implements man1<String>{ public String eat() { public s = "I can eat!"; return s; } }
|