c#中的多态性

谈到一种面向对象的语言中的多态性,无非两种:编译时多态以及运行时多态。
这两种多态对应到c#里面就是重载和覆写,其主要区别如下:

重载

类中定义的方法的不同版本

         public int Calculate(int x, int y)

         public double Calculate(double x, double y)

特点(两必须一可以)

         方法名必须相同

         参数列表必须不相同

         返回值类型可以不相同

覆写

         子类中为满足自己的需要来重复定义某个方法的不同实现。

         通过使用override关键字来实现覆写。

         只有虚方法和抽象方法才能被覆写。

         要求(三相同)

                   相同的方法名称

                   相同的参数列表

                   相同的返回值类型
 谈到c#的覆写就不能不说说override与new的区别:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Csharp_duotai_test
{

    
//===============================================================
    class Person1
    
{
        
public virtual void output()
        
{
            Console.WriteLine(
"person1");

        }

    }

    
class Employee1 : Person1
    
{
        
public void output()
        
{
            Console.WriteLine(
"employee1");

        }

    }

    
//=======================================================================
    class Person2
    
{
        
public virtual void output()
        
{
            Console.WriteLine(
"person2");

        }

    }

    
class Employee2 : Person2
    
{
        
public override void output()
        
{
            Console.WriteLine(
"employee2");

        }

    }

    
//================================================================================
    class Person3
    
{
        
public virtual void output()
        
{
            Console.WriteLine(
"person3");

        }

    }

    
class Employee3 : Person3
    
{
        
public new void output()
        
{
            Console.WriteLine(
"employee3");

        }

    }

    
//========================================================================================

    
class Program
    
{
        
static void Main(string[] args)
        
{
            
//测试第一种类型
            Person1 person1 = new Person1();
            Person1 employee1 
= new Employee1();
            Employee1 employee2 
= new Employee1();

            person1.output();
            employee1.output();                               
//输出person1
            employee2.output();

            
//测试第二种类型
            
//Person2 person1 = new Person2();
            
//Person2 employee1 = new Employee2();        
            
//Employee2 employee2 = new Employee2();

            
//person1.output();
            
//employee1.output();                        //输出employee2
            
//employee2.output();

            
//测试第三种类型
            
//Person3 person1 = new Person3();
            
//Person3 employee1 = new Employee3();        
            
//Employee3 employee2 = new Employee3();

            
//person1.output();
            
//employee1.output();                        //输出person3
            
//employee2.output();

            Console.ReadLine();

        }

    }

}

 
有句话说得很天才:virtual 方法主要用于重写了基类里的方法,new则体现了版本的控制。之所以说它体现了版本的控制,我想主要是基于如下的考虑:
using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Csharp_duotai_test
{


    
class Person1
    
{
        
public virtual void output()
        
{
            Console.WriteLine(
"person1");

        }

    }

    
class Person2 : Person1
    
{
        
public new void output()
        
{
            Console.WriteLine(
"person2");

        }

    }

    
class Person3 : Person2
    
{
        
public new void output()
        
{
            Console.WriteLine(
"person3");

        }

    }



    
class Program
    
{
        
static void Main(string[] args)
        
{
            Person1 person1 
= new Person1();
            Person1 person2 
= new Person2();
            Person2 person3 
= new Person3();

            person1.output();
            person2.output();
            person3.output();

            Console.ReadLine();

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值