Java重载经典面试题

public class StaticDispatch {
	static abstract class Human{
		
	}
	static class Man extends Human{
		
	}
	static class Woman extends Human{
		
	}
	public void sayHello(Human guy) {
		System.out.println("hello , guy!");
	}
	public void sayHello(Man guy) {
		System.out.println("hello , gentle!");
	}
	public void sayHello(Woman guy) {
		System.out.println("hello , ladly!");
	}
	public static void main(String[] args) {
		Human man = new Man();
		Human woman = new Woman();
		StaticDispatch sr = new StaticDispatch();
		sr.sayHello(man);
		sr.sayHello(woman);
	}
}
/***************************以下代码依次屏蔽char、int、float。。。。看看输出啥?************************************/

import java.io.Serializable;

public class Overload {
	public static void sysHello(Object arg) {
		System.out.println("hello Object");
	}
	public static void sysHello(float arg) {
		System.out.println("hello float");
	}
	public static void sysHello(double arg) {
		System.out.println("hello float");
	}
	public static void sysHello(int arg) {
		System.out.println(arg);
		System.out.println("hello int");
	}
	public static void sysHello(long arg) {
		System.out.println("hello long");
	}
	public static void sysHello(Character arg) {
		System.out.println("hello Character");
	}
	public static void sysHello(char arg) {
		System.out.println("hello char");
	}
	public static void sysHello(char... arg) {
		System.out.println("hello char...");
	}
	public static void sysHello(Serializable arg) {
		System.out.println("hello Serializable...");
	}
	
	public static void main(String[] args) {
		sysHello('b');
	}
}

这两段代码摘抄自周大神的《深入理解Java虚拟机》一书中,能答上的小伙伴证明你对重载理解的非常透彻了。。。。。。。。

你可能感兴趣的:(java开发)