搜尋此網誌

2013年1月5日 星期六

Java 物件equals method 的用法

Generally speaking , you must override the equals method of your own class. Otherwise , the jvm will treat two objects with the same memory address as the same.

多型介紹 (polymorphism introduction)

package helloWorld;

public class PolymorphismDemo {
    
    public static void main(String [] args){
        
        callMan(new SuperMan());
        callMan(new SpiderMan());
        callMan(new IronMan());
        callMan((IMan)new MixedHero(){});
    }
    
    private static void callMan(Man man){
        man.weapon();
    }
    
    private static void callMan(IMan man){
        man.weapon();
    }

}

abstract class MixedHero extends Man implements IMan{
    public void weapon(){
        System.out.println("I am a mixed hero");
    }
}

abstract class Man{
    protected void weapon(){
        System.out.println("Normal man is using a hammer");
    }
}

class SuperMan extends Man{
    
    public void weapon(){
        System.out.println("Superman can use his leaser eye");
    }
}

class SpiderMan extends Man{
    
    public void weapon(){
        System.out.println("Spiderman can use many high-tech stuffs");
    }
}

interface IMan{
    void weapon();
}

class IronMan implements IMan{

    @Override
    public void weapon() {
        System.out.println("Ironman (Sorry , I really don't know how to write descriptions for this character)");
    }
}