JAVA多态的实现

代码如下:

Mouse.java

package Polymorphism;

public class Mouse{   
    public String getMouse(){   
        return null;   
    }   
}   

 PS2Mouse .java

package Polymorphism;

//PS2鼠标   
public class PS2Mouse extends Mouse {   
    public String getMouse(){   
        return "PS2鼠标";   
    }      
}   

 UsbMouse.java

package Polymorphism;

public class UsbMouse extends Mouse{   
    public String getMouse(){   
        return "USB鼠标";   
    }   
}   

 Computer.java

package Polymorphism;

public class Computer{   
    Mouse mouse ;   
    Computer(){   
        mouse = (Mouse)new UsbMouse();   
        String a = mouse.getMouse();   
        System.out.println("获得"+a+"成功!");   
        mouse = new PS2Mouse();   
        a = mouse.getMouse();   
        System.out.println("获得"+a+"成功!");   
    }   
} 

 Client.java

package Polymorphism;

public class Clinet {
	public static void main(String[] args) {    
		Computer computer = new Computer();    
    }   
}

 

你可能感兴趣的:(java)