面向对象编程<继承覆盖>之——JAVA

 

引用:

C#首席架构师:版本控制、虚拟化和覆盖

网址:http://www.csdn.net/article/2012-09-17/2810048-Anders-Hejlsberg-on-Versioning-Virtual-O

 

在Java中,实例方法默认是虚拟化的,只有显式声明为final时,才可以在子类中被覆盖

 

 

public class Father {
	public int getInt(){
		System.out.println("Father  getInt()");
		return 1;
	}

	public int setInt(){
		System.out.println("Father  setInt()");
		return 2;
	}
}



public class Son extends Father {
	public int getInt(){
		System.out.println("Son  getInt()");
		return 3;
	}	
	
	public int setInt(){
		System.out.println("Son  setInt()");
		return 4;
	}
}


public class MainCeshi {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Father fa=new Son();
		
		System.out.println(fa.getInt());
		System.out.println(fa.setInt());
                
                 System.out.println("----------------------------");
		
		Son so= new Son();
		System.out.println(so.getInt());
		System.out.println(so.setInt());
	}

}





//输出结果


Son  getInt()
3
Son  setInt()
4
----------------------------
Son  getInt()
3
Son  setInt()
4

你可能感兴趣的:(java)