C # Learning Diary (class) Declaration and definition of---

Source: Internet
Author: User
Class as an object-oriented soul, in C # has a fairly broad and in-depth application, the depth of the grasp of the nature of the class is an important part of our study of C #. A C # learning diary in the sense of a class is given in the---reference type, which is no longer duplicated. Speaking of classes You have to say the content----objects that are closely related to it.

Classes and objects:

Class: A concept that is abstracted from something with the same attribute, and is generally used to describe a collection of homogeneous individuals.

Object: An individual that is materialized from a class. (for example, a human being is a class, Zhang San is an object of human beings)

Properties: Used to describe the characteristics of an object. (Inside the class, is the data member of the class ^_^)

Method: Describes the ability of an object. (Inside the class, is the data member of the class ^_^, which is called a function in C/s + +, and will later be corrected)

Event: A function that has the ability to trigger.

(unlike the method, the event is passive, emitted by an external thing, the subject is an object, and the method is an action initiated by the object, which is the data member of the Class)

Definition of the class:

The definition of a class begins with the keyword class followed by the name of the class. The body of the class, contained within a pair of curly braces.

Access modifier class  class name   {        access modifier data member;   }

As an example:

public class Student      {            private string name;            private int age;     }

Accessing a data member in a class is like a struct type, you want to use the (.) dot operator (which links the name of the object and the name of the member), and for the access modifier, here's a little bit of private, public, internal (I'll elaborate later):

Private: Privately accessed. Restricted to this class of member access, subclasses, instances (objects) cannot be accessed (in other words, called by the class itself).

Public: publicly accessible. Unrestricted, all classes are accessible.

Internal: All classes in the same assembly are accessible and can be imagined as one of the assembly collections of public.

If you do not specify an access modifier, the associated access permission uses the default permissions, and the default permission for the class is internal, and the member's default permission is private; So when we come up with access issues later, remember to see if you didn't specify an access modifier or inappropriate use.

To create an object:

You can also say that object instantiation of a class requires the use of a new statement. Take the class example defined above Student stu = new Student (); The constructor in the student class is called (functions, which are commonly understood as initialization functions, and later, constructors and destructors), if the data member is not assigned a value of 0 by default;

Here's a story:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {   //defines a class that is named people      public class people      {         //All is specified as publicly-owned member of a          string name;              public char sex;                  public uint-age;            }        Class program      {                   static void Main (string[] args)          {              people person = new people ();        Instantiate a class with the object named Person              Console.WriteLine ("Enter Name:");              Person.name = Console.ReadLine ();  all easy access, no pressure               Console.WriteLine ("Please enter Gender:");              Person.sex = Console.readkey (). KeyChar;    Char type conversion              Console.WriteLine ("\ n Please enter age:");              Person.age = uint. Parse (Console.ReadLine ());   Force type conversion              Console.WriteLine ("Your name is: {0}\t your gender is: {1}\t your age is: {2}", Person.name,person.sex,person.age);}}  

Run under:

Read the code written above, and found that this is different from the struct type in addition to the name of the difference?? There is no difference in the above program (all members are public), see this:

Using System;  Using System.Collections.Generic;  Using System.Linq;    Using System.Text;              namespace Test {//definition of a class, class called people public class people {//all specified private member, private string name;                  private char sex;            private UINT Age;              The Define public method action is to input and output public void input () {Console.WriteLine ("Enter Name:");                 Name = Console.ReadLine ();              Console.WriteLine ("Please enter Gender:"); Sex = Console.readkey ().    KeyChar;              Char type conversion Console.WriteLine ("\ n Please enter age:"); Age = uint.   Parse (Console.ReadLine ()); Force type Conversion} public void output () {Console.WriteLine ("Your name is: {0}\t your gender is: {1                    }\t your age is: {2} ", name, Sex,age); }} class Program {static void Main (string[] args) {peopl        E person = new people (); Instantiate a class with the object named person perSon.input ();    Access the Privat member by calling the public method and assigning a value of person.output (); The direct use of Console.WriteLine (person.name) output cannot be used at this time, access is insufficient}}

Run too above the same, in the class class we can define the method, (the above input and output is two methods) and in the struct struct body cannot define the method, and we specify the attributes in the people class as private privately-owned members, The external object person cannot be accessed directly, only through the public method specified in people. For example: When we are on the phone, we do not have face contact, but instead of exchanging information through the medium of the mobile phone, the phone can be regarded as one of your public methods, you are a private member of a class, and I am the object of this class instantiation.

The above is the C # Learning Diary of the---classes (class) of the Declaration and definition of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.