Home >Backend Development >C#.Net Tutorial >C# Learning Diary 13---Declaration and Definition of Class

C# Learning Diary 13---Declaration and Definition of Class

黄舟
黄舟Original
2017-01-20 13:51:401571browse

Classes, as the soul of object-oriented, have quite extensive and in-depth applications in C#. The in-depth mastery of classes is naturally an important part of learning C#. An article about the meaning of classes C# Learning Diary 12--- Reference types have already been given, so I won’t repeat them here. When it comes to classes, we have to talk about something closely related to them - objects.

Classes and Objects:

Class: A concept abstracted from things with the same attributes, generally used to describe a collection of similar individuals.

Object: An individual embodied from a class. (For example, human beings are a class, and Zhang San is an object in human beings)

Attributes: used to describe the characteristics of objects. (In a class, it is the data member of the class^_^)

Method: Describes the capabilities of the object. (Inside the class, it is the data member of the class ^_^, in C/C++ it is called a function, we will change the name in the future)

Event: A function with the ability to trigger. The difference between methods is that events are passive, emitted by external things, and the recipient is the object; while methods are actions actively emitted by the object and are data members of the class)

of the class Definition:

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

访问修饰符 class  类名
   {
        访问修饰符数据成员;
   }

For example:

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

The way to access the data members in the class is the same as the Struct type. You have to use the (.) dot operator (linked object name and member name). Regarding access modifiers, here is a little talk about private, public, and internal (I will go into detail later):

private: private access. Access is limited to members of this class. Subclasses and instances (objects) cannot access (in other words, they can be called by the class itself).

public: Public access. Accessible to all classes without any restrictions.

Internal: All classes in the same assembly can be accessed, which can be imagined as one of the Public assembly collections.

If no access modifier is specified, the relevant access permissions will use the default permissions. The default permissions of the class are internal, and the default permissions of the members are private; so when we have relevant issues in the future When there is an access permission problem, remember to check whether the access modifier is not specified or used inappropriately.

Create an object:

It can also be said that to instantiate an object of a class, you need to use the new statement. Take the class defined above as an example: Student stu = new Student(); At this time, the constructor (function, commonly understood as the initialization function, we will talk about the constructor and destructor later) in the Student class will be called. If there is no By default, data members are assigned a value of 0;

Write an example below:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{   //定义的一个类,类名叫People  
    public class People  
    {   
      //全部指定为公有成员  
        public string name;      
        public char sex;          
        public uint age;  
      
    }  
  
    class Program  
    {  
         
        static void Main(string[] args)  
        {  
            People person = new People();        //实例化一个类,对象名叫 person  
            Console.WriteLine("输入姓名: ");  
            person.name = Console.ReadLine();   //  全部轻松访问,没有压力   
            Console.WriteLine("请输入性别:");  
            person.sex = Console.ReadKey().KeyChar;    //char 类型转换  
            Console.WriteLine("\n请输入年龄:");  
            person.age = uint.Parse(Console.ReadLine());   //强制类型转换  
            Console.WriteLine("您的姓名是:{0}\t您的性别是:{1}\t您的年龄是:{2}",person.name,person.sex,person.age);  
  
        }  
    }  
}

Run it:

C# Learning Diary 13---Declaration and Definition of Class After reading what is written above Code, I found that what is the difference between this and the Struct type except for the name?? There is indeed no difference in the above program (all members are Public), look at this:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{   //定义的一个类,类名叫People  
    public class People  
    {   
      //全部指定私有成员  
        private string name;      
        private char sex;          
        private uint age;  
  
        //定义public 方法作用是输入与输出  
        public void input()  
        {  
            Console.WriteLine("输入姓名: ");  
            name = Console.ReadLine();     
            Console.WriteLine("请输入性别:");  
            sex = Console.ReadKey().KeyChar;    //char 类型转换  
            Console.WriteLine("\n请输入年龄:");  
            age = uint.Parse(Console.ReadLine());   //强制类型转换  
          
        }  
        public void output()  
        {  
            Console.WriteLine("您的姓名是:{0}\t您的性别是:{1}\t您的年龄是:{2}",name, sex,age);  
          
        }  
      
    }  
  
    class Program  
    {  
         
        static void Main(string[] args)  
        {  
            People person = new People();        //实例化一个类,对象名叫 person  
  
            person.input();         //通过调用 public 方法访问privat 成员并赋值  
  
            person.output();    //此时无法使用直接使用 Console.WriteLine(person.name)输出,访问权限不够  
  
        }  
    }  
}

It runs exactly the same as above, in class We can define methods in the class (the input and output above are two methods), but methods cannot be defined in the Struct structure, and we specify the properties in the People class as private members. The external object person cannot be directly accessed, only Can be accessed through the Public method specified in People. For example: When we are on the phone, we do not communicate face to face, but exchange information with each other through the medium of mobile phones. At this time, the mobile phone can be regarded as one of your public methods, and you are a private method in a class. Member, I am an object instantiated by this class.

The above is the content of C# Learning Diary 13---Class declaration and definition. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn