V. Abstract base classes and interfaces

Source: Internet
Author: User

5.1 Abstract classes and abstract methods

Before a class is preceded by the "abstract" keyword, this class becomes an abstract class.

Corresponding to a method class preceded by the "abstract" keyword, this method becomes an abstract method.

Abstract class Fruit//Abstraction Classes

{

public abstract void Growinarea (); Abstract methods

}

Note that abstract methods cannot have implementation code, followed by a semicolon directly after the function name.

Abstract classes are dedicated to deriving subclasses, subclasses must implement abstract methods declared by abstract classes, or subclasses are still abstract classes.

Abstract classes are generally used to express a kind of relatively abstract things, such as "fruit", and abstract methods that the abstract class should have a certain nature, such as fruit class has an abstract method Growinarea (), that the fruit must have a most suitable for its growth region, But different fruits grow in different ways.

Subclasses that inherit from the same abstract class have the same methods (that is, abstract methods defined by abstract classes), but the specific code for these methods can be different for each class, such as the following two classes representing Apple (apple) and pineapple (Pineapple):

Class Apple:fruit//Apple

{

public override void Growinarea ()

{

Console.WriteLine ("I can be planted in the north and south." ");

}

}

Class Pineapple:fruit//Pineapple

{

public override void Growinarea ()

{

Console.WriteLine ("I like warmth and can only see me in the south.") ");

}

}

Note the override keyword in the preceding code, which indicates that the subclass overrides the abstract method of the base class. An abstract class cannot create an object, which is typically used to refer to a child class object.

Fruit F;

F=new Apple ();

F.growinarea ();

F=new Pineapple ();

F.growinarea ();

Operation Result:

I can be planted in the north and south.

I like warm, can only see me in the south.

Note the same sentence code "F.growinarea ();" Different results are output due to the different objects referenced by F. As you can see, the code runs like the "virtual method call" described in the previous section, with no intrinsic difference.

You can write code according to the following formula:

Abstract class abstract class variable name =new inherits from this abstract class's specific subclass name ();

An abstract class can contain non-abstract methods and fields. So:

A class that contains an abstract method must be an abstract class, but a method in an abstract class is not necessarily an abstract method.

5.2 Abstract Properties

In addition to the method can be abstract, attributes can also be abstract, see the following code:

Abstract class Parent

{

Public abstract String Message//abstract property

{

Get

Set

}

}

Class Child:parent

{

Private String _msg;

public override String Message

{

Get

{

return _msg;

}

Set

{

_msg=value;

}

}

}

Using code:

Parent p=new Child ();

P.message= "Hello";

5.3 Interface

Let's take a look at the following phrase:

A duck is a kind of bird that can swim and is also a kind of food.

How do you express this relationship in an object-oriented program?

If you use C + +, you can design a duck (Duck) class to inherit from two parent classes (bird bird and food foods). However, all classes in C # can have only one parent class, and this method is not feasible.

To solve this problem, C # introduces the concept of Interface (interface) and stipulates that "a class can implement multiple interfaces."

(1) Definition and use of the interface

The keyword interface is used to define the interface:

Define two interfaces

public interface Iswim

{

void Swim ();

}

public interface Ifood

{

void Cook ();

}

An interface can be thought of as a " pure " abstract class, and all its methods are abstract methods.

You can implement some interfaces by defining a class with the same syntax as inheritance:

Define an abstract class

Public abstract class Bird

{

public abstract void Fly ();

}

Inherits from an abstract class, implements two interfaces

public class Duck:bird, Ifood, Iswim

{

Implementing the Iswim Interface

public void Swim ()

{

Console.WriteLine ("A duck can Swim");

}

Implementing the Ifood Interface

public void Cook ()

{

Console.WriteLine ("Duck is often grilled, Peking duck is very famous");

}

Implementing abstract methods in abstract class bird

public override void Fly ()

{

Console.WriteLine ("Only Ducks can Fly");

}

}

As you can see, the abstract class defines the class to which the object belongs, and the interface actually defines the behavior attributes that an object should have.

The interface can be used as follows:

interface type name Variable name =New implements the type name of the interface ();

The sample code is as follows:

static void Main (string[] args)

{

Duck d = new Duck ();

Duck Object D can be used in 3 different ways:

1. self-defined;

2. The parent class defines the

3. Interface-Defined

D.fly ();

D.cook ();

D.swim ();

Assigning a subclass (Duck) object to a base class variable

Bird B = D;

You can now only use the fly () method defined by the base class

B.fly ();

Assigning a Duck object to a Iswin interface variable

Iswim s = d;

You can now only use the swim () method defined by the interface

S.swim ();

Assigning an Duck object to another implemented interface Ifood interface variable

Ifood F = d;

You can now only use the Cook () method defined by the interface

F.cook ();

}

Please read the comments of the above code carefully, because the duck class inherits from the abstract base class Bird, but also implements the Iswim and Ifood two interfaces, so the Duck object has all the methods defined by the three, and can be assigned to the three types of variables.

It is important to note that although there is always only one Duck object in the program, it can be used differently after assigning it to different types of variables.

(2) explicitly implement an interface

As mentioned above, a class can implement multiple interfaces, and when an object of this class is created, all its public methods (including its own public method and the public method defined by the interface) can be accessed by referencing the object variable

To). In this case, there is no way to tell which methods are defined by the interface and which are defined by the class itself. C # provides an " Explicit interface " implementation mechanism that can be separated in both cases, and one example code is as follows:

Interface IMyInterface

{

void Func ();

}

public class A:imyinterface

{

void Imyinterface.func ()

{

......

}

public void Func2 ()

{

......

}

}

Note the interface names that are highlighted in bold before the method func, which is the explicit implementation of C # for interface IMyInterface.

When Class A explicitly implements an interface IMyInterface, the method of the interface definition can only be accessed in the following way:

IMyInterface a = new A ();

A.func ();

The following code will not compile:

A = new A ();

A.func ();

This leads to a conclusion:

If a class explicitly implements an interface, the methods defined by this interface can be called only in a variable of this interface type, not directly through the object variables of the class.

Or say this:

An explicitly implemented interface method can only be accessed through an interface instance and not directly through the class instance.

V. Abstract base classes and interfaces

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.