C # Object Oriented (ii)

Source: Internet
Author: User

One: abstract methods

1. Abstract methods in object-oriented programming languages refer to methods that have only method declarations and no concrete method bodies. Abstract methods generally exist in abstract classes or interfaces.

In some parent classes, some behaviors are not very explicit and therefore cannot be implemented in code, but the class must also have this method, so the method is defined as an abstract method.

2. Declaration method: Public Abstract Eat (); The method declaration ends with a semicolon, and there is no curly brace after the signature, no body of the function, because it is too abstract to be clear, and the implementation is implemented by overriding functions in each sub-class.

3. Its features:

(1) The abstract method is an implicit virtual method.

(2) Only abstract method declarations are allowed in abstract classes.

(3) There is no method body because the abstract method only declares that it does not provide a real implementation. Abstract methods are only really implemented in derived classes, which means that abstract methods only store function prototypes (the return type of the method, the name and parameters used), and not the principal code.

(4) Add abstract keywords.

(5) The purpose of the abstract method is to specify that the derived class must implement the behavior associated with this method.

Two: abstract class

1. Abstract class: A class that cannot be instantiated. The keyword is abstract, and any class with an abstract keyword cannot be new. An abstract class is incomplete and can only be used as a base class. In an object-oriented approach, abstract classes are primarily used for type concealment and for roles that act as global variables.

2. Declaration: Abstract class declaration: Public abstract Class ren{};

3. Note:

(1) A class with abstract methods is definitely an abstract class, but abstract classes do not necessarily contain abstract methods.

(2) A constructor method, a static member method cannot be declared as an abstract method.

(3) A non-abstract class must implement all the abstract methods inherited from the parent class, and if an abstract method is not implemented, the class must be added with the abstract keyword. If the parent class is declared abstract and there are non-implemented abstract methods, the subclass must implement all the abstract members of the parent class, unless the class is also abstract.

4. Features:

(1) Abstract classes cannot be instantiated.

(2) An abstract class can contain both abstract and non-abstract methods.

(3) The abstract class cannot be decorated with the sealed modifier, because the meanings of the two modifiers are reversed, the class with the sealed modifier cannot inherit, and the abstract modifier requires that the class be inherited.

(4) A non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstract accessor.

Example: There is an abstract method eat () in the Ren class, which must be overridden in its derived classes Chinese and American

Abstract classRen {protected stringname;  Public Abstract voidEat (); }    classChinese:ren { Public Override voidEat () {Console.WriteLine ("eat with chopsticks"); }    }    classAmerican:ren { Public Override voidEat () {Console.WriteLine ("eat with a knife and fork"); }    }

Three: interface

1. Keywords: interface, with interface keyword to define.

2. Concepts: Extremely abstract classes, no member variables, no instance attributes and instance methods, only abstract methods or abstract attributes, examples of life: standards, rules.

3. Writing: Interface without class, with interface, the name generally with I as the first letter; do not write abstract, inside all is, do not write public, must be public.

    Interface IUSB         // interface     {        void  start ();         void stop ();    }

4. Features:

(1) The methods in the interface are abstract, so there is no need to add an abstract modifier.

(2) The methods in the interface are common, so there is no need to add the public modifier.

(3) An interface is a rule standard.

(4) The interface can inherit from the parent interface.

(5) A class can implement (inherit) multiple interfaces. A class can have only one parent class, but it is possible to implement multiple interfaces.

Example: A simple IUSB interface with two abstract methods start () and stop (), the derived class implementation interface must implement all the methods in the interface.

 InterfaceIusb//Interface    {        voidstart (); voidStop (); }    classUdisk:iusb//implementing the interface must implement all the methods inside    {         Public voidstart () {Console.WriteLine ("the USB flash drive is activated"); }         Public voidStop () {Console.WriteLine ("the USB stick stopped."); }    }    classCammer:iusb { Public voidstart () {Console.WriteLine ("the camera's booted."); }         Public voidStop () {Console.WriteLine ("the camera's off."); }    }    classComputer { Public voidCheckusb (Iusb USB) {Usb.start (); }         Public voidCloseusb (Iusb USB) {usb.stop (); }    }

When used:

       New computer ();        New Udisk ();        New Cammer ();        // inserting a USB flash drive       // Insert Camera // Unplug USB drive                // Insert Camera

Four: namespaces

1. Concept: namespace is equivalent to a package, it is a class classification management tools, to the computer, the same namespace under the same class can be divided into different files stored.

2. General wording: namespace company name. Project name. Module Name

3. To use a class in another namespace above another namespace, you can write the namespace used to use it on top of it;

V: Project Name

General default: ConsoleApplication1, which is used to identify what this project is for people to see.

VI: ASSEMBLY

Right-click on the project name: property to modify the assembly name

The compiled exe or DLL file is the assembly. EXE is a console application, and a DLL is a library class.

The name of the assembly is the compiled EXE or DLL name.

If a project is divided into three people to write, a write a class library, b write a class library, C write the main function, the process of integrating them together is:

1. A write a file that compiles a name such as Cc.dll;

2. b When writing to use a namespace of a class, B first copy A's Cc.dll file to its own directory, B need to right-click on a reference in the project: Add a reference, browse to find the reference to add, and then using a namespace;

3. b finished compiling a file with a name such as Dd.dll;

4. C to write the main function, in the main function of the class AB, he needs to copy the AB DLL file, then the project in the reference to the right, add a reference, browse to find the reference to add, and then using a namespace; The namespace of using B, so that it can be used in the main function.

5. If A's namespace has a class Ren and B in the namespace there is a class ren name, this C at the time of use to use a namespace under which the Ren class will use the namespace point out and reuse.

C # Object-oriented (ii)

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.