向上转型和向下转型都是为了类可以调用的方法范围发生变化。
- class Fu{
- void method1(){
- System.out.println("fu method1");
- }
- void method2(){
- System.out.println("fu method2");
- }
- void methodFu(){
- System.out.println("fu methodFu");
- }
-
- }
- class Zi extends Fu{
- void method1(){
- System.out.println("zi method1");
- }
- void method2(){
- System.out.println("fu method2");
- }
- void methodZi(){
- System.out.println("fu methodZi");
- }
- }
- public class HelloWorld {
-
- public static void main(String args[]) {
- Fu fu=new Zi();
- fu.method1();
- fu.method2();
- fu.methodFu();
- Zi zi=(Zi)fu;
- zi.methodFu();
- zi.methodZi();
-
-
- }
- }
复制代码 |