Deep analysis of C # inheritance mechanism 6

Source: Internet
Author: User
Tags abstract inheritance modifier modifiers domain
Inheriting v. Inheritance and access modifiers

An access modifier is a keyword that specifies the accessibility of a declared member or type. There are four access modifiers in the inheritance of the class: public protected internal private. Use these access modifiers to specify the following five levels of accessibility: public protected internal internal protected private.


The accessibility meaning of the Declaration
Public access is unrestricted.
Protected access is limited to the containing class or types derived from the containing class.
Internal access is limited to the current project.
Protected internal access is limited to the current project or type derived from the containing class.
Private access is limited to the containing type.

1. Some questions about accessibility domain in inheritance

All members of the base class (except instance constructors, destructors, and static constructors) are inherited by derived types. This even includes the private members of the base class. However, the accessibility domain of a private member includes only the program text of the type that declares the member. In the following example


Class A
{
int x;
static void F (b) {
b.x = 1; Right
}
}
Class B:a
{
static void F (b) {
b.x = 1; Error
}
}

Class B inherits the private member X of Class A. Because the member is private, it can only be accessed in the "Class body" of a. Thus, access to b.x has been successful in the A.F approach and failed in the B.F approach.

2. Some questions about attributes in inheritance

As with the member methods of a class, we can also define the concepts of overloads, virtual properties, abstract properties, and sealed attributes for a property. As with classes and methods, property adornments should also conform to the following rules:

Overload of a property

1. A property that uses modifiers in a derived class to overload an attribute of the same name in a base class.

2. In overloaded declarations, the property's name, type, and access modifier should be consistent with the inherited property in the base class.

3. If the properties of the base class have only one property accessor, the overloaded property should have only one. However, if the properties of the base class contain both get and set property accessors, the overloaded property can have only one or two property accessors at a time.

Note: Unlike method overloads, an overloaded declaration of a property does not actually declare a new property, but rather simply provides the specific implementation of the accessor for an existing virtual property.

Virtual property

1. A property declared with the virtual modifier is a virtual property.

2. The accessors of a virtual property include a get accessor and a set accessor, and are also virtual.

Abstract properties

1. An attribute declared with the abstract modifier is an abstract property.

2. An accessor to an abstract property is also virtual and does not provide a specific implementation of the accessor. This requires that in a non-virtual derived class, the derived class itself provides a concrete implementation of the accessor by overloading the property.

3. The simultaneous use of abstract and override modifiers not only indicates that the attribute is abstract. And it overloads the virtual property in the base class. The accessor of the property is also abstract.

4. Abstract properties are only allowed to be declared in an abstract class.

5. In addition to the use of both the abstract and override modifiers, any two of the static, virtual, override, and abstract modifiers cannot appear at the same time.

Sealing properties

1. The property declared using the sealed modifier is a sealed property. The sealed property of a class is not allowed to be inherited in a derived class. The accessors of the sealed property are also sealed.

2. When a property declaration has a sealed modifier, it must also have a override modifier.

As you can see from the above, these rules for attributes are very similar to the methods. For an accessor to a property, we can think of a get accessor as a method of a value type that is the same as the property modifier, has no parameters, and returns a value of the property, and regards the set accessor as a method with the same property modifier, containing only one value parameter, and a return type void. Look at the following program:


Using System;
public enum Sex
{Woman, man,};
Abstract public class people
{
private string S_name;
Public virtual String Name
{
Get
{return s_name;}
}
private sex m_sex;
Public Virtual Sex Sex
{
Get
{return m_sex;}
protected string S_card;
Public abstract String Card
{get; set;}
}

The above example declares the class "person", whose name and gender sex are two read-only virtual attributes: ID card is an abstract property that allows reading and writing because the class people contains the abstract property card, so people must declare it to be abstract. Below we write a class class for the guest of accommodation to inherit from the people. Then look at the following program:


Class Customer:people
{
String S_No;
int i_day;
public string No
{
Get
{return s_no;}
Set
{
if (s_no!= value)
S_No = value;
}
}
public int Day
{
Get
{return i_day;}
Set
{
if (i_day!= value)
I_day = value;
}
}
public override string Name
{
get {return base. Name; }
}
public override sex Sex
{
get {return base. Sex}
}
public override string Card
{
Get
{return s_ card;}
Set
{S_ card = value;}
}
}

In class customer, property name, Sex, and card declarations are all added with the override modifier, and the declaration of the property is consistent with the base class people. The get accessor for Name and sex, the card's got and set accessors use the base keyword to access accessor properties in the base class people. The card's declaration overloads the abstract accessors in the base class people. In this way, there is no abstract member in the customer class, and the customer can be non-virtual.





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.