C # object-oriented programming concepts and design detailed design class

First, the design of object-oriented programming foundation

1.1 Object-oriented programming

(1) What is object-oriented programming

C language, designed the application is too simple, because all functions are contained in several or even a block of code, and the code block can only serve a single program. Accordingly, in order to increase the opportunities for reuse code blocks to accomplish more programs, it is necessary to use an object-oriented programming methods.

Object-oriented programming allows each computer application by a single one, can play a role in the object program word (or units) combination.

Concept (2) and object class

Object Oriented Design program is an object-based, event-driven programming. Objects are the basic elements of the program, and the event handler established a correlation between objects.

A, Class

Life there are an infinite number of entities such as people, vehicles, plants, each entity has a range of properties and behavior. For example, a car can define the nature of its color, model, etc., but also to define its forward, backward and other acts. In object-oriented design, the specific definition of the nature and behavior for a certain entity is called the class (class).

B, the object

The object is the core of object-oriented programming techniques, constitute the basic elements of the application. In the class, each object is an instance of the class. For example, the human person is the entity classes, and each person is different from the human subject. Everyone has properties different height, weight, etc., and standing, and walking behavior. In object-oriented programming, the nature of the object called properties, events occurring on the object called an event, an event produced for the behavior called methods.

Properties, methods and events that make up the three elements of the object.

(3), predefined classes

Just beginning to learn the controls, forms, user-defined data types are provided by the .NET Framework, called predefined classes.

1.2, encapsulation and hiding

Packaging refers to organic tissue members in a class. C #, members of the class includes data members, properties, methods and events. Class is a tool to achieve the package, the package ensures a good class independence.

(1) the definition of the class

C #, classes are defined by the class. For example, when a Windows Forms application to establish a form is created in Form1 class keyword to define a class.

Since general format class is defined as follows:

class 类名
{
    //定义数据成员
    //定义属性
    //定义方法
    //定义事件
}

(2) members of the defined class

In the class definition, but also it provides definitions for all members of the class, including data, properties, events and methods. Modifiers can be defined by the following:

Modifiers significance
public Class members can be accessed by any code
private Class members can only be made when the code to access the class, you define the members, using private default
protected Class members are accessible only by a class or a derived class (i.e., subclass) code

A, define data members

Data members of the class declaration statement is defined by the standard variable, combined with access level access modifier to specify the data members. To play a protective role, data members generally private and protected modifiers statement.

    class vehicle
    {
        private int wheels;     //车轮数
        private float weight;   //车重量
    }

B, defined method

By standard methods of the class defined function declaration statement, combined with access levels to specify the method of access modifiers.

    public class Vehicle
    {
        private int wheels;
        private float weights;
        public void SetVehicle(int wheels,float weights)  //定义方法SetVehicle设置车轮数和车重量
        {
            this.wheels = wheels;
            this.weights = weights;
        }
        public void GetVehicle()     //定义方法GetVehicle()获得车轮数和车重量
        {
            MessageBox.Show("车轮数:" + this.wheels.ToString() + "\n车重量:" + this.weights.ToString());
        }
    }
        private void button1_Click(object sender, EventArgs e)
        {
            int wheels;
            float weight;
            wheels = int.Parse(textBox1.Text);
            weight = float.Parse(textBox2.Text);
            Vehicle v = new Vehicle();
            v.SetVehicle(wheels, weight);
            v.GetVehicle();
        }

In SetVehicle () method, there are two sets of wheels and a variable weight. A set of data for the two members of the Vehicle of wheels and weight assignment, and GetVehicle

(3) define the properties

C # class attributes can be defined: one for setting attribute values ​​defined by the keyword set; another for acquiring attribute values, defined with the keyword get. Wherein a code block may be ignored for setting read-only or write-only property.

访问修饰符 数据类型 属性名
{
    set
    {
         ...  //属性的set代码块
    }
    get
    {
         ...  //属性的get代码块
    }
}
namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float x, y;
            string msg1, msg2;
            x = float.Parse(textBox1.Text);
            y = float.Parse(textBox2.Text);
            Point p = new Point();
            p.MyX = x;
            p.MyY = y;
            msg1 = "您输入的坐标为(" + p.MyX.ToString() + "," + p.MyY.ToString() + ")";
            label3.Text = msg1;
            msg2 = p.ReadXY;
            MessageBox.Show("您输入的坐标为" + msg2 + "!");
        }
    }
    class Point
    {
        private float x;    //x坐标
        private float y;    //y坐标

        public float MyX     //定义属性MyX
        {
            set
            {
                x = value;   //提供对数据成员x的赋值
            }
            get
            {
                return x;     //提供对x的访问
            }
        }
        public float MyY
        {
            set
            {
                y = value;
            }
            get
            {
                return y;
            }
        }
        public string ReadXY
        {
            get
            {
                return "(" + x + "," + y + ")";
            }
        }
    }
}

Property actually provides a way to access private data members of the class. Define attributes not only solve the problem of access to data members, but also provides a strong operational control.

1.3, access to the object and its members

In object-oriented programming, must comply with the "define, after use," the principle that any predefined or custom classes must be instantiated before you can use later known as object.

Statement (1) object

类型 对象名=new 类名();

Access (2) members of the

Members of the class defined by generally required to access the object, the members of the different types of data, its access different forms.

  Data members: object data members

  Property: Object Properties

  Methods: object methods

  Event: more complex, can be found in the official MSDN help documentation

Example: to establish a student class method can read data Read (), the data output by the write method.

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            string id, name;
            float score;

            Console.Write("请输入学号:");
            id = Console.ReadLine();
            Console.Write("请输入姓名:");
            name = Console.ReadLine();
            Console.Write("请输入得分:");
            score = float.Parse(Console.ReadLine());

            student s = new student();
            s.SetInfo(id, name);
            s.MyScore = score;

            Console.WriteLine("\n======以下是输出=======\n");
            Console.WriteLine(s.OutPut());
            Console.WriteLine("\n按下任意按键结束程序:");
            Console.ReadKey();
        }
    }
    class student
    {
        private string sId; //学号
        private string sName; //姓名
        private float score;  //分数

        public void SetInfo(string sId,string sName)  //定义方法SetInfo()设置学号和姓名
        {
            this.sId = sId;
            this.sName = sName;
        }

        public float MyScore    //定义属性MyScore
        {
            set
            {
                score = value;
            }
            get
            {
                return score;
            }
        }
        public string OutPut()    //定义方法OutPut,提柜对所有数据成员的格式化输出
        {
            return "学号:" + sId + " 姓名:" + sName + " 得分:" + score;
        }
    }
}

1.4 constructor and destructor

(1) Constructor

The constructor can be assigned to data members in a statement that object. When used in conjunction with the new keyword to declare an object.

 

Published 95 original articles · won praise 331 · views 500 000 +

Guess you like

Origin blog.csdn.net/qq_35379989/article/details/102324527