C # Sharp Experience (iv)

Source: Internet
Author: User
Tags abstract instance method modifier modifiers

Chapter Four Class and Object

Component programming is not to discard traditional object-oriented objects, and the opposite component programming is the deepening and development of object-oriented programming. Class as the object-oriented soul in the C # language has a considerable depth of application, many very "Sharp" component features are even directly from the class packaging. The depth of the class mastery of nature is our "Sharp XP" important link.

Class

The C # class is a data structure that encapsulates data members, function members, and nested types. Where data members can be constants, fields. A function member can be a method, a property, an indexer, an event, an operator, an instance builder, a static builder, and a destructor. We will make a detailed analysis of these members and their characteristics in the five-talk constructor and destructor and the sixth domain method properties and indexers. In addition to some imported external methods, declarations and implementations of classes and their members in C # are often put together.

C # uses a variety of modifiers to express the different properties of a class. There are five different restriction modifiers based on their protection-level C # classes:

Public can be arbitrarily accessed;

Protected can only be accessed by this class and its inheriting subclasses;

Internal can only be accessed by all classes within this group (Assembly), which is the logical unit and physical unit of a class in the C # language, and the compiled file name extension is often ". DLL "or". EXE ".

Protected internal a unique combination restriction modifier that can be accessed only by all classes in this combination and by inheriting subclasses of those classes.

Private can only be accessed by this class.

If it is not a nested class, the class within the namespace or compilation unit has only public and internal two adornments.

The new modifier can only be used for nested classes that represent a hidden type that inherits the parent class with the same name.

Abstract is used to decorate an abstraction class, which means that the class can only be used as a parent class for inheritance, not object instantiation. An abstract class can contain abstract members, but this is not necessary. Abstract cannot be used at the same time as new. Here is the pseudo code for the abstract class usage:

abstract class A
{
  public abstract void F();
}
abstract class B: A
{
  public void G() {}
}
class C: B
{
  public override void F()
  {
    //方法F的实现
    }
}

Abstract Class A contains an abstract method F (), which cannot be instantiated. Class B inherits from Class A, which contains an instance method g (), but does not implement the abstract method F (), so it must still be declared as an abstract class. Class C inherits from Class B, implements the class abstract method F (), and can then instantiate the object.

Sealed is used to decorate a class as a sealed class, preventing the class from being inherited. At the same time, the modification of the abstract and sealed of a class is meaningless and is forbidden.

Related Article

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.