JAva中性子クラスが親メソッドを上書きするために満たす必要がある条件

3324 ワード

一、説明
子クラスが親クラスを書き換える(上書きする)方法で満たさなければならない条件:1.親クラスのメソッドは、子クラスで表示される必要があります.すなわち、子クラスが親クラスのメソッドを継承している(superキーを明示的に使用して親クラスの書き換えられたメソッドにアクセスできる)、親クラスのメソッドがprivateタイプの場合、子クラスは継承できず、上書きできません. 2.子と親のメソッドはインスタンスメソッドでなければなりません.親がstaticメソッドで、子がインスタンスメソッドである場合、または逆の場合、エラーが発生します.親メソッドと子メソッドがstaticメソッドの場合、子メソッドは親メソッドを書き換えるのではなく、親メソッドを非表示にします.  3.子クラスと親クラスのメソッドは、同じ関数名、パラメータリストを有し、子クラスの戻り値が親クラスと同じまたは親クラスの戻りタイプの子タイプ(jdk 1.5以降)である必要があります.メソッド名が同じでパラメータリストが異なる場合(戻りタイプが同じでも異なる場合もあります)、書き換えではなくメソッドのリロードにすぎません.メソッド名とパラメータリストが同じで、戻り値タイプが異なる場合、サブクラス戻り値タイプも親クラス戻り値タイプのサブクラスではない場合、コンパイラはエラーを報告します. 
4.サブクラスメソッドのアクセス権は、親メソッドのアクセス権より小さくすることはできません(同じアクセス権を持つか、サブクラスのアクセス権が親より大きい場合があります).アクセス権は、public、protected、パケットアクセス権、privateから高くなります.子メソッドのアクセス権が親メソッドより低い場合、コンパイラはエラーメッセージを表示します.子メソッドは、親メソッドよりも多くのコンパイル時異常(実行時異常ではない)を放出することはできません.すなわち、子メソッドが放出するコンパイル時異常、または親メソッドと同じまたは親例外の子です.
二、ソースコード
package tong.yue.day4_264;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComponent;
/**
 *     (  )            :
 * 1.              ,             (       super                ),
 *          private   ,         ,     。
 * 2.               ,     static          ,        。
 *          static  ,           ,         。
 * 3.                    、    ,                          (jdk1.5  )。
 *                (             ),         ,    。
 *              ,       ,                    ,       。
 * 4.                      (                        )
 *         :public、protected、     、private。               ,           
 * 5.                     (       ),                              。
 * @author tong
 *
 */

public class Inheritance {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

class Pareant {
	public void doSomething() {

	}

	public static void doSomething1() {

	}

	public JComponent doSomething2() {
		return null;
	}

	public JComponent doSomething3() {
		return null;
	}

	public void doSomething4(int x) {

	}
	public void doSomething5() throws FileNotFoundException{

	}
	
	private void doSomething6(int x) {

	}
}

class Sub extends Pareant {
	
	//          ,      ,                  
	//               ,                       ,           
	private void doSomething6(int x) {

	}
	
	//   ,       ,       
	//This static method cannot hide the instance method from Pareant
	public static void doSomething() {

	}

	//   ,       ,       
	//This static method cannot hide the instance method from Pareant
	public void doSomething1() {

	}

	//   ,       JComponent,          , jdk1.5  ,                  ,         
	public JButton doSomething2() {
		return null;
	}

	//   ,       JComponent,        int,               ,           
	//The return type is incompatible with Pareant.doSomething3()
	public int doSomething3() {
		return 1;
	}
	
	//  ,        public,   protected,             ,               
	//Cannot reduce the visibility of the inherited method from Pareant
	protected void doSomething4(int x) {

	}
	//  ,              ,    FileNotFoundException  ,     IOException        
	//              ,          ,       
	//Exception IOException is not compatible with throws clause in Pareant.doSomething5()
	public void doSomething5() throws IOException{

	}
	
}