C# Core Syntax - Classes and Objects

using System;

 

namespace lesson1_classes and objects

{

    #region Object Oriented Concept Review

    //everything is an object

    //Use the program to abstract (describe) the object

    //Programming with object-oriented thinking

 

    #endregion

 

    #region What is a class

    //basic concept

    // have the same characteristics

    // has the same behavior as

    // abstraction of a class of things

    // class is a template for objects

    // Objects can be created through the class

    // class keywords

    //class

 

    #endregion

 

    Where is the #region class declaration

    //The class is generally declared in the namespace statement block

    #endregion

 

    Syntax of #region class declaration

    class class name

    {

        //Features - member variables

        // Behavior - member method

        //Protection feature--member attribute

        

        //constructor and destructor

        // indexer

        // operator overloading

        // static members

    }

    #endregion

 

    #region class declaration instance

    //This class is used to describe humans

    // Naming: use Pascal nomenclature

    //Note: Different classes in the same statement block cannot have the same name

    class Person

    {

        //Features - member variables

        // Behavior - member method

        //Protection feature--member attribute

 

        //constructor and destructor

        // indexer

        // operator overloading

        // static members

    }

 

    class Machine

    {

        //Features - member variables

        // Behavior - member method

        //Protection feature--member attribute

 

        //constructor and destructor

        // indexer

        // operator overloading

        // static members

    }

    #endregion

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Classes and Objects");

 

            #region What is a (class) object

            //basic concept

            //Class declaration and class object (variable) declaration are two concepts

            //The declaration of a class is similar to the declaration of an enumeration and a structure. The declaration of a class is equivalent to declaring a custom variable type

            //The object is created by the class

            //Equivalent to declaring a variable of a specified class

            //The process of creating an object for a class is generally called instantiating an object

            //Class objects are all reference types

 

            #endregion

 

            #region Basic syntax for instantiating objects

            // class name variable name;

            //Class name variable name = null; (null means empty)

            //class name variable name = new class name();

            #endregion

 

            #region instantiated object

            Person p;

            Person p2 = null;//null means empty and does not allocate heap memory space

            Person p3 = new Person();//equivalent to a person object

            Person p4 = new Person();//Equivalent to another person object

            //Notice

            // Although they are instantiated objects from a class

            //But their characteristic behavior and other information are unique to them

            //Don't think that they are sharing data. Two people, you are you, I am me, and I have nothing to do with each other.

 

            Machine m = new Machine();

            Machine m1 = new Machine();

 

            //Object-oriented programming is to open the Nuwa mode creation mode. If you want to declare the object, you can declare the object with new

            //All objects are controlled by us

            //We are equivalent to the chief director of the entire program world

            #endregion

        }

    }

    //Summarize

    //Class declaration and class object declaration are two concepts

    //The declaration of the class is used to abstract (describe) the display of things by declaring the template of the object

    //The declaration of the class object is used to represent the individual object in reality

 

    //Class is a custom variable type

    // Instantiating a class object is declaring variables

}

 

Guess you like

Origin blog.csdn.net/weixin_61541885/article/details/128745084