Virtual methods, abstract classes, polymorphism

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
. 1 . There must achieve some virtual methods, implement the abstract method does not provide part of the abstract method is a method of covering the derived class forced, or derived class can not be instantiated. Such as:

// abstract methods public abstract class  Animal { public abstract void  Sleep (); public abstract void  Eat. (); } // virtual method public class  Animal { public Virtual void  Sleep () {} public Virtual void  Eat. () {} } 2 . abstract method declarations in an abstract class only, not a virtual method. In fact, if the class contains abstract methods, the class is abstract, it must be declared as abstract. Such as:  public class  Animal { public
  

    
  
    
  



 

    
  
    
  


 

    
 abstract void Sleep();
    
public abstract void Eat();
}

编译器会报错:
Main.cs(
10): 'VSTest.Animal.Sleep()' is abstract but it is contained in nonabstract class 'VSTest.Animal'
Main.cs(
11): 'VSTest.Animal.Eat()' is abstract but it is contained in nonabstract class 'VSTest.Animal'

3Abstract method must be overridden in a derived class, which is similar with the interface, a virtual method does not have to. If a class derived from the abstract class but not fully implement all abstract methods, the class itself must be declared as drawn

objects. Moreover abstract class can not be instantiated, but the abstract class can define a reference, this reference point can be derived from the when examples of non-abstract subclass of abstract class, and these virtual abstract method call will automatically follow polymorphism

executed. abstract methods can not have private visibility, otherwise the derived class will not be able to achieve it. An abstract class can have a constructor, field, properties such as:

public abstract class  Animal { public abstract void  Sleep (); public abstract void  Eat. (); } public class  Cat: Animal { public the override void  Sleep ()     {         Console.WriteLine (  " Cat Sleeping IS "  );     } //  we need implement Animal.Eat (here)  

    
  
    
  


 

    
  



    


}
Compiler error: Main.cs (
14 ):  ' VSTest.Cat '  does Not Implement Inherited  abstract  Member  ' VSTest.Animal.Eat () ' , because we do not implement all abstract class pumping

like methods.

Examples of polymorphisms:
the using  the System;
the using  the System.Collections.Generic;
the using  the System.Text;

namespace  AbstractApplication
{
    
public class  the Employee     { protected String  m_Name; public  the Employee ( String  name)          {              m_Name  =  name;  

        
 
        



        }
        
public virtual void Display()
        {
            System .Console.WriteLine(
"Name:{0}", m_Name );
        }
    }
    
class Secretary : Employee
    {
        
public Secretary(string name) : base(name) { }
        
public override void Display()
        {
            
base.Display();
            System.Console.WriteLine(
"Excute: Sectretary\n");
        }
    }
    
class Technician : Employee
    {
        
public Technician(string name) : base(name) { }
        
public override void Display()
        {
            
base.Display();
            System.Console.WriteLine(
"Excute:Technician\n");
        }
    }
    
class Manager : Employee
    {
        
public Manager(string name) : base(name) { }
        
public new void Display()//屏蔽多态性
        {
            
base.Display();
            System.Console.WriteLine(
"Excute:Technician\n");
        }
    }
    
class AbstractApp
    {
        
static void Main(string[] args)
        {
            Console.WriteLine(
"---------多态性------------");
            Employee[] array 
= new Employee[4];        
            array[
0= new Technician("carl");
            array[
1= new Secretary("hubcarl");
            array[
2= new Secretary("anan");
            array[
3= new Employee("Employee");
            
foreach (Employee employee in array)
            {
                employee.Display();
            }
            Console.WriteLine(
"---------屏蔽多态性------------");
            Employee[] array1 
= new Employee[4];
            array1[
0= new Manager("carl");//调用的是基类的Display方法
            array1[1= new Secretary("hubcarl");
            array1[
2= new Secretary("anan");
            array1[
3= new Employee("Employee");
            
foreach (Employee employee in array1)
            {
                employee.Display();
            }
            
//((Manager)array[0]).Display();
        }
    }
}

operation result:

Reproduced in: https: //www.cnblogs.com/hubcarl/archive/2009/11/07/1597827.html

Guess you like

Origin blog.csdn.net/weixin_34177064/article/details/93817263
Recommended