动态方法调度 - 示例代码
抽象类:
package base;
/*
Abstract classes cannot be instantiated, but they can be subclassed
*/
public abstract class ClsVirusScanner {
//With One Abstract method
public abstract void fnStartScan();
protected void fnCheckForUpdateVersion(){
System.out.println("Perform Virus Scanner Version Check");
}
protected void fnBootTimeScan(){
System.out.println("Perform BootTime Scan");
}
protected void fnInternetSecutiry(){
System.out.println("Scan for Internet Security");
}
protected void fnRealTimeScan(){
System.out.println("Perform RealTime Scan");
}
protected void fnVirusMalwareScan(){
System.out.println("Detect Virus & Malware");
}
}
覆盖子类中的抽象方法:
import base.ClsVirusScanner;
//All the 3 child classes inherits the base class ClsVirusScanner
//Child Class 1
class ClsPaidVersion extends ClsVirusScanner{
@Override
public void fnStartScan() {
super.fnCheckForUpdateVersion();
super.fnBootTimeScan();
super.fnInternetSecutiry();
super.fnRealTimeScan();
super.fnVirusMalwareScan();
}
}; //ClsPaidVersion IS-A ClsVirusScanner
//Child Class 2
class ClsTrialVersion extends ClsVirusScanner{
@Override
public void fnStartScan() {
super.fnInternetSecutiry();
super.fnVirusMalwareScan();
}
}; //ClsTrialVersion IS-A ClsVirusScanner
//Child Class 3
class ClsFreeVersion extends ClsVirusScanner{
@Override
public void fnStartScan() {
super.fnVirusMalwareScan();
}
}; //ClsTrialVersion IS-A ClsVirusScanner
动态/后期绑定导致动态方法调度:
//Calling Class
public class ClsRunTheApplication {
public static void main(String[] args) {
final String VIRUS_SCANNER_VERSION = "TRIAL_VERSION";
//Parent Refers Null
ClsVirusScanner objVS=null;
//String Cases Supported from Java SE 7
switch (VIRUS_SCANNER_VERSION){
case "FREE_VERSION":
//Parent Refers Child Object 3
//ClsFreeVersion IS-A ClsVirusScanner
objVS = new ClsFreeVersion(); //Dynamic or Runtime Binding
break;
case "PAID_VERSION":
//Parent Refers Child Object 1
//ClsPaidVersion IS-A ClsVirusScanner
objVS = new ClsPaidVersion(); //Dynamic or Runtime Binding
break;
case "TRIAL_VERSION":
//Parent Refers Child Object 2
objVS = new ClsTrialVersion(); //Dynamic or Runtime Binding
break;
}
//Method fnStartScan() is the Version of ClsTrialVersion()
objVS.fnStartScan();
}
}
结果:
Scan for Internet Security
Detect Virus & Malware
上传:
objVS = new ClsFreeVersion();
objVS = new ClsPaidVersion();
objVS = new ClsTrialVersion()