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

Source: Internet
Author: User

4. implemented using the interface in the database

In the above Bubble sorting, we have defined our own interface. In fact, in the library, we have already defined an interface for comparing the size. We can use it directly. The following is an implementation using the interface in the library, and the Bubble Sorting is performed,

The following code uses the library interface implementation and custom interface implementation:

Person class:

 

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

Namespace InterfaceBubleSort
{
Public class Person: IComparable
{
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 IComparable Member

Public int CompareTo (object obj)
{
Return this. Age-(Person) obj). Age;
}

# Endregion
}
}
Bubble Sorting class:

 

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

Namespace InterfaceBubleSort
{
Public class BubleSort
{
Public void BubleSortMethord (IComparable [] array)
{
For (int I = 0; I <array. Length; I ++)
{
For (int j = 0; j <array. Length-i-1; j ++)
{
If (array [I]. CompareTo (array [I + 1])> 0)
{
IComparable temp = array [I];
Array [I] = array [I + 1];
Array [I + 1] = temp;
}
}
}
}


}
}

 

Main function class:

 

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

 

Summary: 1. Classes are abstract objects. abstract classes can be understood as objects and abstract classes are called abstract classes. The interface is only a behavior specification or provision. Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions. 2. The interface basically does not have any specific characteristics of inheritance. It only inherits the methods that can be called. 3. A class can implement several interfaces at a time, but only one parent class can be extended. 4. interfaces can be used to support callback, but inheritance does not. 5. the abstract class cannot be sealed. 6. The methods in the abstract class are virtual by default, but the interface methods in the class implementing the interface are non-virtual by default. Of course, they can also be declared as virtual. 7. abstract classes implement an object-oriented principle that separates mutable and immutable. Abstract classes and interfaces are immutable definitions, and classes can be implemented. 8. Good interfaces should be specific functions rather than multiple functions. Otherwise, interface pollution may occur. 9. If an abstract class implements an interface, you can map the methods in the interface to an abstract class instead of implementing the methods in the abstract class.

The image comparison between interfaces and abstract classes:

1. Planes fly and birds fly. They all inherit the same interface "fly", but F22 belongs to the aircraft abstract class and pigeon belongs to the bird abstract class.

2. Iron and wood doors are both doors (abstract classes). If you want a door, I cannot give it (it cannot be instantiated), but I can give you a specific iron door or wooden door (polymorphism ), it can only be a door. You cannot say it is a window (single inheritance). A door can have a lock (Interface) or a doorbell (multiple implementations ). A door (abstract class) defines what you are and an interface (LOCK) specifies what you can do (one interface is best to do only one thing, you can't ask the lock to make a sound (interface pollution )).

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.