Process oriented -- object oriented C# programming record

1. Process oriented: it is oriented to the process of completing this thing and emphasizes the action of completing this thing.

Put the elephant in the fridge

1. Open the refrigerator door

2. Put the elephant in

3. Close the refrigerator door

If the process oriented idea is used to solve this problem, when the people executing this problem are different, the method to solve the problem needs to be tailored for each different person.

Objects need to be visible and touchable

Lights: properties and methods

Properties:
Shape: long

Brightness: 500w

Color: white

Brand: XX

Method: luminescence

Electric fans: properties and methods

attribute

Profile: three blades

Color: white

Brand: XX

Method: rotate and fan

We further encapsulate objects with the same attributes and methods and abstract the concept of class. Class is a mother and child. Determines the properties and methods of the object.

Objects are created from classes.

Class is the drawing of building, and the object is the building.

2. Class

Syntax:
[public] class name

{

        Field;

        Attributes;

         method;

}

To write a class, you need to create a class object. The process of creating the class object becomes the instantiation of the class, using the keyword new

When a non static function is called, it is directly called with the created object.

Classes do not occupy memory, and objects occupy memory.

3. Properties

Property is used to protect the field and limit the field assignment and value.

The essence of attributes is get () and set ()

Attributes only play an excessive role, and the most important assignment is fields.

Both get() and set() are called read-write attributes

Only get() without set() is called a read-only property

No get() only set() is called write only property
4. Difference between static and non static

In a non static class, you can have both static and non static members (instance members)

When calling an instance member, you need to use the object name. Instance member

When calling a static member, you need to use the class name

Note: static members must use the class name to call, and instance members need to use objects to call

In static functions, only static members can be accessed, and instance members cannot be accessed.

In instance functions, you can use either static members or instance members.

  Instance members cannot be declared in a static class. There can only be static members.

For a static class, there is no need to call the object, so you can't create a static object.

use:

1) If you want to use the class as a tool class, you can consider writing the class as a static class at this time.

2) Static classes share resources throughout the project. No memory

The static class will release resources only after the program is completed. In other words, as long as the program is running, the static class will never release resources.

heap      Stack      Static storage area

Free resources: (GC)Garbage Collection garbage collector

using System.Text;

namespace lei
{
    public class Person
    {
        private static string _name;
        public static string Name
        {
            get{return Person._name;}
            set{Person._name=value;}
        }
        private char _gender;
        public char Gender
        {
            get{return _gender;}
            set{_gender=value;}
        }
        public void M1()
        {
            Console.WriteLine("I am a non static method");
        }
        public static void M2()
        {
            Console.WriteLine("I'm a static method");
        }
    }
    public class Program
    {
        public static void Main()
        {
            Person p=new Person();
            p.M1();//Example method
            Person.M2();//Static method
            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

Constructor:

Function: help us initialize objects and assign values to objects in turn.

Constructor is a special method:

1) The constructor does not return a value and can't even write void

2) The constructor name must be the same as the class name

When an object is created, that is, when new, the constructor is executed.

Constructors can have overloads.

The class itself will have a default parameterless constructor. If the constructor is defined by itself, the default parameterless constructor will be replaced regardless of whether there are parameters or not. If the original parameterless constructor is replaced, the parameterless constructor can be redefined, but it is not the same as the original function.

new keyword:
Person zsStudent=new Person(...);

1. Open up a space in memory

2 create objects in the open space

3. Call the constructor of the object for initialization

using System.Text;

namespace lei
{
    public class Student
    {
        private string _name;
        public string Name
        {
            get{return _name;}
            set{_name=value;}
        }
        private int _age;
        public int Age
        {
            get{return _age;}
            set{
                if(value<0||value>100)
                {
                    value=0;
                }
                _age=value;
                }
        }
        private char _gender;
        public char Gender
        {
            get{
                if(_gender!='n'&&_gender!='f')
                {
                    return _gender='n';
                }   
            return _gender;}
            set{_gender=value;}
        }
        private int _english;
        public int English 
        {
            get{return _english;}
            set{_english=value;}
        }
        private int _chinese;
        public int Chinese
        {
            get{return _chinese;}
            set{_chinese=value;}
        }
        private int _math;
        public int Math
        {
            get{return _math;}
            set{_math=value;}
        }
        public void SayHello()
        {
            Console.WriteLine("My name is{0},I this year{1}Years old, I am{2}living",this.Name,this.Age,this.Gender);
        }
        public void ShowScore()
        {
            Console.WriteLine("My name is{0},My total score is{1},The average score is{2}",this.Name,this.Chinese+this.Math+this.English,(this.Chinese+this.Math+this.English)/3);
        }
    }
    public class Program
    {
        public static void Main()
        {
            Student zsStudent=new Student();
            zsStudent.Name="Zhang San";
            zsStudent.Age=18;
            zsStudent.Gender='n';
            zsStudent.Chinese=100;
            zsStudent.Math=100;
            zsStudent.English=100;
            zsStudent.SayHello();
            zsStudent.ShowScore();

            Student xlStudent=new Student();
            xlStudent.Name="Xiaolan";
            xlStudent.Age=16;
            zsStudent.Gender='f';
            zsStudent.Chinese=100;
            zsStudent.Math=100;
            zsStudent.English=100;
            zsStudent.SayHello();
            zsStudent.ShowScore();
            Console.ReadLine();
        }
    }
}

After introducing constructor

using System.Text;

namespace lei
{
    public class Student
    {
        //Fields, properties, methods, constructors
        public Student(string name,int age,char gender,int chinese,int math,int english)
        {
            this.Name=name;
            this.Age=age;
            this.Gender=gender;
            this.Chinese=chinese;
            this.Math=math;
            this.English=english;
        }
        public Student(string name,int chinese,int math,int english):this(name,0,'c',chinese,math,english)
        {
            // this.Name=name;
            // this.Chinese=chinese;
            // this.Math=math;
            // this.English=english;
        }
        private string _name;
        public string Name
        {
            get{return _name;}
            set{_name=value;}
        }
        private int _age;
        public int Age
        {
            get{return _age;}
            set{
                if(value<0||value>100)
                {
                    value=0;
                }
                _age=value;
                }
        }
        private char _gender;
        public char Gender
        {
            get{
                if(_gender!='n'&&_gender!='f')
                {
                    return _gender='n';
                }   
            return _gender;}
            set{_gender=value;}
        }
        private int _english;
        public int English 
        {
            get{return _english;}
            set{_english=value;}
        }
        private int _chinese;
        public int Chinese
        {
            get{return _chinese;}
            set{_chinese=value;}
        }
        private int _math;
        public int Math
        {
            get{return _math;}
            set{_math=value;}
        }
        public void SayHello()
        {
            Console.WriteLine("My name is{0},I this year{1}Years old, I am{2}living",this.Name,this.Age,this.Gender);
        }
        public void ShowScore()
        {
            Console.WriteLine("My name is{0},My total score is{1},The average score is{2}",this.Name,this.Chinese+this.Math+this.English,(this.Chinese+this.Math+this.English)/3);
        }
    }
    public class Program
    {
        public static void Main()
        {
            Student zsStudent=new Student("Zhang San",18,'n',100,100,100);
            // zsStudent.Name = "Zhang San";
            // zsStudent.Age=18;
            // zsStudent.Gender='n';
            // zsStudent.Chinese=100;
            // zsStudent.Math=100;
            // zsStudent.English=100;
            zsStudent.SayHello();
            zsStudent.ShowScore();

            Student xlStudent=new Student("Xiaolan",16,'f',100,100,100);
            // xlStudent.Name = "Xiaolan";
            // xlStudent.Age=16;
            // zsStudent.Gender='f';
            // zsStudent.Chinese=100;
            // zsStudent.Math=100;
            // zsStudent.English=100;
            zsStudent.SayHello();
            zsStudent.ShowScore();
            Console.ReadLine();
        }
    }
}

Destructor:

When the program ends, the destructor executes

1. Write a Ticket class. It has a distance attribute (this attribute is read-only and assigned in the construction method), which cannot be negative. It has a Price attribute. The Price attribute is read-only, and the Price is calculated according to the distance.

You can calculate the price in a single function.

using System;

namespace Name
{
    public class Ticket
    {
        private double _distance;
        public double Distance
        {
            get{return _distance;}//Read only property settings
        }
        public Ticket(double distance)
        {
            if(distance<0)
            {
                distance=0;
            }
            this._distance=distance;
        }
        private double _price;
        public double Price
        {
            get{
                if(_distance>0&&_distance<100)
                {
                    return _distance*1.0;
                }else if(_distance>=101&&_distance<200)
                {
                    return _distance*0.95;
                }else if(_distance>=201&&_distance<300)
                {
                    return _distance*0.9;
                }
                else 
                {
                    return _distance*0.8;
                }
                }
        }
        public void ShowTicket()
        {
            Console.WriteLine("{0}Kilometer needs{1}element",Distance,Price);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Ticket t=new Ticket(150);
            t.ShowTicket();
            Console.ReadLine();
        }
    }
}

Keywords: C# Back-end

Added by akuji36 on Mon, 22 Nov 2021 01:13:53 +0200