Steps:
1. Create a template method in the parent class to complete consistent operations
2. Perform different operations on sub-classes
Code:
/// <Summary>
/// Data Verification parent class
/// </Summary>
Public Abstract Class Validation
{
Public Void Valid ( String Value ){
Console. writeline ( " \ R \ n Verification Result: {0} \ r \ n " , This . Isvald (value ));
}
Protected Virtual Bool Isvald ( String Value ){
Return True ;
}
}
/// <Summary>
/// Subclass: Numerical verification
/// </Summary>
Public Class Validbyint: Validation
{
Protected Override Bool Isvald ( String Value)
{
Try
{
Int Number = Int . Parse (value ); Return True ;
}
Catch
{
Return False ;
}
}
}
/// <Summary>
/// Subclass: Gender Verification
/// </Summary>
Public Class Validbysex: Validation
{
Protected Override Bool Isvald ( String Value)
{
If (Value = " Male " | Value = " Female " ){ Return True ;}
Return False ;
}
}
/// client call
Validation V1 = New validbyint ();
v1.valid ( " male " );
V1 = New validbysex ();
v1.valid ( " male " );
console. readkey ();