天天看點

java中繼承的了解

一:在子類中建立的方法與父類的名字相同,但是參數類型不同,不算是對父類方法的重寫

class A{
	double f(double x,double y){
		return x+y;
	}
}

class B extends A{
	double f(int x,int y){
		return x*y;
	}
}
public class extend_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		B b=new B();
		System.out.println(b.f(3, 5));
		System.out.println(b.f(3.0, 5.0));
	}

}
           

結果:

15.0

8.0

二:在子類中被隐藏的父類方法使用super調用父類方法

class C{
	double f(double x,double y){
		return x+y;
	}
	static int g(int n){
		return n*n;
	}
}

class D extends C{
	double f(double x,double y){
		double m=super.f(x,y);
		return m+x*y;
	}
	static int g(int n){
		int m=C.g(n);
		return m+n;
	}
}
public class extends_2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		D b=new D();
		System.out.println(b.f(10.0, 8.0));
		System.out.println(b.g(3));

	}

}
           

結果:

98.0

12