Abstract thinking common_role of abstract classes _ Role of interfaces _ differences between abstract classes and interfaces (2)

Source: Internet
Author: User

In the previous article, we used the simplest method to implement the bubble sort method. It is now implemented using the abstract class method.

Abstract classes Implement Bubble Sorting:

First, define an abstract class. The function of this abstract class is to implement the Bubble sorting method. The code is:

 

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 );
}
}

To verify the versatility of this algorithm, we define a new class person to sort the person according to the age of the 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 ("Age input error! ");
Return 0;
}
}
Set
{
If (0 <value & value <100)
{
Age = value;
}
Else
{
Console. writeline ("Age input error! ");
}
}
}

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;
}
}

 


}
}
The main function used for verification is:

 

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 ("White connection", 23 );
Person B = new Person ("Hou Yong Jun", 24 );
Person c = new Person ("Li Na", 23 );
Person d = new Person ("Li Guoqiang", 24 );
Person [] persongs = {a, B, c, d };
Console. WriteLine ("unordered: \ n ");
Foreach (var item in persongs)
{
Console. WriteLine (item. Name );
}
A. BubleSort (persongs );

Console. writeline ("\ n after sorting: \ n ");
Foreach (VAR item in persongs)
{
Console. writeline (item. Name );
}
}
}
}

The result is as follows:

 

Unordered:

Bai lianqi
Hou Yong-chen
Li Na
Li guoyu

 

After sorting:

Bai lianqi
Li Na
Hou Yong-chen
Li guoyu

From the results, we can conclude that the program is correct. Next, let's analyze what are the benefits of doing so?

The advantage of the program is obvious, that is, to improve code reusability. When we want to sort by the salary of person or the title level of person, we need to correct only a small comparer (object big, object small) method.

Let's take a look at the shortcomings of this operation? As we all know, whether in java or in C #, classes can only be single inheritance, that is, if we inherit this abstract class, we lose the opportunity to inherit other classes. Is there any way to improve it? The answer is yes. Since classes are single inheritance and interfaces are multi-inheritance, we can use interfaces to implement the features in sorting.

Iii. Interface implementation features.

When using interfaces to implement features, I define this method as: using interfaces to encapsulate features, so that the feature interfaces between various objects can be made common, so that code can be reused!

The code for implementing the encapsulation feature of the interface is as follows:

The interface is defined as follows:

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace InterfaceBubleSort
{
Public interface IMyCompare
{
Bool MyComparer (IMyCompare small );
}
}

The person class is defined as follows:

 

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 ("Age input error! ");
Return 0;
}
}
Set
{
If (0 <Value & value <100)
{
Age = value;
}
Else
{
Console. writeline ("Age input error! ");
}
}
}

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 Member

Public bool mycomparer (imycompare small)
{
Return this. age> (person) Small). Age;
}

# Endregion
}
}

Sorting class:

 

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;
}
}
}
}


}
}

 

The main function class is:

 

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 ("White connection", 23 );
Person B = new Person ("Hou Yong Jun", 24 );
Person c = new Person ("Li Na", 23 );
Person d = new Person ("Li Guoqiang", 24 );
Person [] persongs = {a, B, c, d };
Console. WriteLine ("unordered: \ n ");
Foreach (var item in persongs)
{
Console. WriteLine (item. Name );
}
Bublesort. BubleSortMethord (persongs );

Console. WriteLine ("\ n after sorting: \ n ");
Foreach (var item in persongs)
{
Console. WriteLine (item. Name );
}
}
}
}

The result of this implementation is the same as that of the abstract class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.