C# classes and objects

object

An object is a specific object, such as "table" is not an object, and "this table" is an object

Properties: Each property of each object has a specific value.

Attributes: gender, first name

Methods: The behavior, actions, operations performed by an object.

Method: eat, sleep

class definition

Classes are abstract concepts, just templates, eg: "human"

[Access modifier] class class name

{

member;

}

public class Person //This is a class, which can contain variable definitions or methods
    {
    }

A class can contain variable definitions or methods

There are four types of access modifiers commonly used

private: private members, which can only be accessed inside the class.

protected: protected members, accessible within this class and in inherited classes.

pubilc: public member, full disclosure, no access restrictions.

internal: The current assembly can be accessed.

instantiation of the class

keyword new syntax class instance name=new class()

Class member access instancename.property instancename.method

Tivket t = new Tivket;

Attributes

Attribute definition: get; set;

Attributes are used to protect the fields corresponding to them. Ensure that the reading and assignment of fields meet the requirements.

Attributes can be divided into: read-write, read-only, write-only

//Attributes
        public string Name
        {
            get { return _name; }//读
            set { _name = value; }//写
        }

Constructor

Write a class first, and then write a method in the class. The method name is the class name.

Write a class, if you don't write any constructor, then this class has a default no-argument constructor.

public Tivket(int distance)
        {
            if (distance <0)
            {
                distance = 0;
            }
            this._distance =distance ;
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325424407&siteId=291194637