抽象思维之共性_抽象类的作用_接口的作用_抽象类和接口的区别(二)

     在上篇随笔中,对冒泡法排序用了最简单的方式予以了实现。现在用抽象类的方法实现。

     抽象类实现冒泡排序:

     首先需要定义一个抽象类,这个抽象类的作用就是实现了冒泡法排序,代码为:

 

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

namespace AbstractBubleSort
{
    public abstract class AbstractBubleSort
    {
        public void BubleSort(object[] array)
        {
            for (int i = 0; i < array.Length; i++)
   {
       for (int j = 0; j < array.Length-i-1; j++)
       {
                    if (Comparer(array[i],array[i+1]))
                 {
                        object temp=array[i];
                        array[i]=array[i+1];
                        array[i+1]=temp;
                 }      
       }
   }
        }

        public abstract bool Comparer(object i,object j);
    }
}

为了验证这个算法的通用性,我们再定义一个新的类person,根据person的年龄来对person排序。

 

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

namespace AbstractBubleSort
{
    public class Person:AbstractBubleSort
    {
        private int age;
        public int Age
        {
            get
            {
                if (0<age&&age<100)
                {
                    return age;
                }
                else
                {
                    Console.WriteLine("年龄输入错误!");
                    return 0;
                }
            }
            set
            {
                if (0<value&&value<100)
                {
                    age = value;
                }
                else
                {
                    Console.WriteLine("年龄输入错误!");
                }
            }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Person(string name,int age)
        {
            this.age = age;
            this.name = name;
        }

        public override bool Comparer(object big, object small)
        {
            if (((Person)big).Age>((Person)small).Age)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

 

       
    }
}
用来验证的主函数为:

 

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

namespace AbstractBubleSort
{
    class MainEntry
    {

        static void Main(string[] args)
        {
            Person a = new Person("白连启", 23);
            Person b = new Person("侯勇蛟",24);
            Person c = new Person("李娜",23);
            Person d = new Person("李国璋",24);
            Person[] persongs = { a,b,c,d};
            Console.WriteLine("未排序时:\n");
            foreach (var item in persongs)
            {
                Console.WriteLine(item.Name);
            }
            a.BubleSort(persongs);

            Console.WriteLine("\n排序后:\n");
            foreach (var item in persongs)
            {
                Console.WriteLine(item.Name);
            }
        }
    }
}

显示结果为:

 

未排序时:

白连启
侯勇蛟
李娜
李国璋

 

排序后:

白连启
李娜
侯勇蛟
李国璋

由结果我们可以得出程序正确。下面我们再来分析一下,这么做的好处是什么?

程序的优点是显而易见的,就是提高的代码的复用性,当我们再想根据person的工资或者person的职称等级排序时,我们需要改正的仅仅是一个小小的comparer(object big,object small)方法。

下面我们来看看这么做有什么缺点?大家都知道,无论是在java还是在C#中,类只能是单继承,也就是如果我们继承了这个抽象类,那么我们就失去了继承其他的类的机会。有什么方法可以改进吗?答案是肯定的。既然类是单继承,而接口是多继承,因此,排序中的特性部分,我们可以通过接口来实现。

三、接口实现特性部分。

     用接口实现特性部分,我自己把这个方法定义为:用接口封装特性,让各个对象之间的特性接口化,共性化,从而实现代码的复用!

     下面看一下接口实现封装特性的代码:

     接口定义如下:

 

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

namespace InterfaceBubleSort
{
    public interface IMyCompare
    {
        bool MyComparer(IMyCompare small);
    }
}

person类定义如下:

 

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

namespace InterfaceBubleSort
{
    public class Person:IMyCompare
    {
        private int age;
        public int Age
        {
            get
            {
                if (0<age&&age<100)
                {
                    return age;
                }
                else
                {
                    Console.WriteLine("年龄输入错误!");
                    return 0;
                }
            }
            set
            {
                if (0<value&&value<100)
                {
                    age = value;
                }
                else
                {
                    Console.WriteLine("年龄输入错误!");
                }
            }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Person(string name,int age)
        {
            this.age = age;
            this.name = name;
        }

 

 


        #region IMyCompare 成员

        public bool MyComparer(IMyCompare small)
        {
            return this.Age > ((Person)small).Age;
        }

        #endregion
    }
}

排序类:

 

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

namespace InterfaceBubleSort
{
    public  class BubleSort
    {
        public void  BubleSortMethord(IMyCompare[] array)
        {
            for (int i = 0; i < array.Length; i++)
   {
       for (int j = 0; j < array.Length-i-1; j++)
       {
                    if (array[0].MyComparer(array[i+1]))
                 {
                        IMyCompare temp=array[i];
                        array[i]=array[i+1];
                        array[i+1]=temp;
                 }      
       }
   }
        }

      
    }
}

 

主函数类为:

 

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

namespace InterfaceBubleSort
{
    class MainEntry
    {

        static void Main(string[] args)
        {
            BubleSort bublesort = new BubleSort();
            Person a = new Person("白连启", 23);
            Person b = new Person("侯勇蛟",24);
            Person c = new Person("李娜",23);
            Person d = new Person("李国璋",24);
            Person[] persongs = { a,b,c,d};
            Console.WriteLine("未排序时:\n");
            foreach (var item in persongs)
            {
                Console.WriteLine(item.Name);
            }
            bublesort.BubleSortMethord(persongs);

            Console.WriteLine("\n排序后:\n");
            foreach (var item in persongs)
            {
                Console.WriteLine(item.Name);
            }
        }
    }
}

这种方式实现的结果和用抽象类实现,结果是一样的。
posted @ 2008-09-03 09:56  IT-CEO  阅读(582)  评论(0编辑  收藏  举报