java-forced conversion type

1655 ワード

import java.util.Scanner;


//   ;      ,     (    ,    ) 
class Test {
    public static void main(String[] args) {
        System.out.println("hello world !!! ");  //     ,           ,             ,      
        method(new Cat());
        method(new Dog());
    }

    public static void method (Animal a) {
        a.eat();
    }
}


class Animal {
    public void eat() {
        System.out.println(" Animal eat some foods !!! ");
    }

}

class Cat  extends Animal {
    public void eat() {
        System.out.println(" Cat eats fish");
    }

    public void catchMouse() {
        System.out.println(" Catch Mouse");
    }
}

class Dog  extends Animal {
    public void eat() {
        System.out.println(" Dog eats meat");
    }

    public void lookHome() {
        System.out.println(" look Home");
    }
}



//    

class Test {
    public static void main(String[] args) {
        Person p = new SuperMan();
        System.out.println(p.name);    // join
        p.talk();           //superman talk with someone for one million
        Superman t = (SuperMan)p;   //  ,         
        t.fly();
    }
}




class Person {
    String name = "John";
    public void talk (){
        System.out.println(" join talk with someone ... "); 
    }
}

class SuperMan extends Person {
    String name = "SuperMan";
    public void talk (){
        System.out.println("superman talk with someone for one million ... ");      
    }

    public void fly (){
        System.out.println("talk with someone for one million ... ");   
    }
}