Access to members of a class in C #

Source: Internet
Author: User
Tags modifier modifiers

When writing programs, we can define their access levels by using different access modifiers on the members of the class.

Public members

Public members in C # provide the external interface of the class, allowing users of the class to access them from outside. Public members are decorated with the modifier, which is the least restrictive way of access.

Private members

Private members in C # are restricted to members of the class, and access to private members from outside the class is illegal. If a member's access modifier is not present in the declaration, the member is private by default. Private member modifiers are private.

Protect members

In order to facilitate access to derived classes and to expect members to be hidden from the outside world, you can use the protected modifier to declare a member to be a protected member.

Internal members

A member of a class that uses the internal modifier is a special member. This member is transparent to the application or library in the same package, while in the package. NET is not allowed to access.

Use the following example to illustrate the use of the access modifier for a member of a class.

Program Listing 10-1:

Using System;
Class Vehicle//define the Auto class
{
  public int wheels;//Publicly owned member: Wheel number
  protected float weight;  Protection member: Weight public
  void F () {
      wheels=4;//correct, allow access to its own member
      weight=10;//correct, allow access to its own member
  }
}
class Train//Definition train class
{
  public int num;//publicly owned member: number
  of carriages private int passengers;//Private Member: Number of passengers private
  float weight ; Private member: Weight public
  void F () {
      num=5;//correct, allow access to its own member
      weight=100;//correct, allow access to its own members
      Vehicle v1=new Vehicle ();
      v1.wheels=4; Correct, allow access to V1 public member
      //v1.weight=6 error, do not allow access to V1 protection member, can be changed to:
      weight=6;
  }
Class Car:vehicle//Definition Sedan class
{
  int passengers;//Private Member: Number of passengers public
  void F () {
     Vehicle v1=new Vehicle (); c30/>v1.wheels=4; Correct, allow access to V1 's public member
     v1.weight=6;//correct, allow access to V1 protection member
  }
}
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.