Use interface or class?

Source: Internet
Author: User

For those who have previously used VB6.0 and now switched to C # programming, while sighing the flexibility of C #, they are often designed inProgramThe architecture cannot be started. Indeed, C # not only features fast development like VB, but also features C ++. In this case, the more you know about C ++, the more favorable it is for developing programs using C.

When designing a program architecture, it is often difficult to decide whether to use interfaces or class inheritance. From the perspective of C # syntax, class does not support multiple inheritance, but interface does. My colleagues often argue that the use of interfaces is based on the multi-inheritance of the language features of the C # interface. I would like to talk about the advantages of using interfaces, at this time, it is often argued that "field cannot be defined in the interface definition, and variable cannot be defined!"

In fact, you don't have to argue about this in theory. The simplest example may explain the problem.

This example is from the help of VB 6.0 (msdn 2001 ). The example requires the development of a system to simulate the Jurassic biological system, which has only two biological roles: dinosaurs and coops.

There are two general ideas for designing this biological simulation system.

The first idea is to use inheritance:

1) since dinosaurs and coops are both animals, first design an abstract class animal.

Public abstract class animal {
...
// Abstract method bite to simulate "biting" behavior of animals
Public abstract void bite (animal O );
...
}
Obviously, it is reasonable to define an animal class as abstract, because instantiating an animal is meaningless.

2) inherit and define the dinosaur and Prophet objects

First define flee

Public class flee: Animal {
Public int size; // The Head of the cockroach
Public int weight; // the weight of the cockroach.

Public override void bite (animal OBJ)
{
// Implement the "Bite" action here-insert a thin straw
// Gently suck blood after the skin of the prey. This is known as a prey.
// For the input parameter OBJ
}
}

Then define the dinosaur object dinosaur

Public class dinosaur: Animal {
Public int size; // dinosaur head
Public int weight; // dinosaur weight

Public override void bite (animal OBJ)
{
// Here, the dinosaur is "Biting"-from time to time
// Shake your head and scream. This is known as a prey.
// For the input parameter OBJ
}
}

3) Start simulating the Jurassic Biological System

// The first dinosaur ano was born! ANo is awesome, but a little lonely!
Dinosaur ano = new dinosaur ();

// Generate the Gigi family (10)
Flee [10] Gigi = new flee [10] {...};

// At the beginning of the war, the First cockroach attacked the dinosaur Ano!
Gigi [0]. Bite (ANO );

// Follow up the rest
For (INT I = 1; I <= 9; I ++)
Gigi [I]. Bite (ANO );

// The dinosaur ano is furious and starts to fight back!
Ano. Bite (Gigi [0]);

// The dinosaur ano continues to fight back!
For (INT I = 1; I <10; I ++)
Ano. Bite (Gigi [I]);

......

Well, how is this biological system busy?

(Stop first. It's too late to go to bed! Let them fight! __^ To be continued)

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.