C # notes - [] object-oriented basis (a) with an object class attributes and concepts

It is the basic unit procedure;
class includes attributes and methods;
class is a template, is an object program runs;

common problem:

Cognitive FAQ

(1) Three object-oriented features

1) up package (developed by a)
  1. Hiding the internal implementation details, module developers only care about the internal implementation and excuses requirements, the caller only concerned with the interface module calls;
  2. Benefits: Security assurance (avoid code leak), rapid application (direct application), teamwork (more than the same work);
  3. Application: the program is the basic unit, the basic unit is packaged, but may equally be class module package;
2) Inheritance
  1. Reuse existing code;
  2. Benefits: an update, update everywhere; drawbacks: easy related complex;
  3. Application: object, Form
3) Polymorphism
  1. Let the interface of an object according to different requests made different responses;
  2. Benefits: The program is easy to expand, easy to maintain;
  3. Application: polymorphism inheritance, polymorphism interfaces (simple plant, abstract factory);
Note: polymorphisms described later in detail

OPP principle (2) the development of

1) Single Responsibility Principle (Object clear responsibility principle)
  • Requirements: An object only do one thing, we must focus on responsibilities too easily cause changes in the more, the program is unstable (high cohesion, low coupling extension);
2) principle (the core principles of open closed)
  • Requirements: demand changes as little as possible when the modified class design, but is done by the extension class; modify i.e. closed, open extension;
3) Dependency Inversion Principle (OPP essence)
  • Requirements: interface-based programming, high-level module call interface module bottom implements the interface from the underlying changes directly affect the level;
4) Interface Segregation Principle
  • Requirements: as much as possible using a dedicated small interface instead of using the total interface to avoid the interface is too complex;
5) Richter substitution principle
  • Requirements: inheritance, the subclass can replace the parent class, a virtual machine can be dynamically find specific subclass objects based parent class variables, in order to achieve polymorphism;

(3) objects and classes

Object:

Any entity that exists is an object, it is really exist, including static and dynamic features two characteristics;
static features: represents an attribute of an object for storing the data object itself;
dynamic feature: representing an object method, for explaining What objects do;

The role of the object:
the attribute of the object in the data storage 1;
2. Software functions are achieved by a method;

class:

The total of the same type, properties, methods, abstract, organized in a unit called the class;
the object actually is an individual of the same class of things;

The difference between classes and objects
  • The class definition is actually an abstract model, such as "car", in this model, the definition of "attribute" and "method";
  • Object is actually in accordance with the model of this class, to create a specific entities, each property has specific "value";

Using the definition of the object (4) Class

1) the definition of class specifications:

Here Insert Picture Description

Look at an example:
namespace Demo
{
    /// <summary>
    /// 学生类
    /// </summary>
    class Student
    {
        // 私有字段:学员
        private int studentId;
        // 私有字段:学员姓名
        private string studentName = "";
        //属性:学号
        public int StudentId
        {
            get { return studentId; }
            set { studentId = value; }
        }

        //属性:学生姓名
        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }

        //获取学员信息
        public string GetStudent()
        {
            //这里直接使用的是字段
            string info = string.Format("姓名:{0} 学号{1}", studentName, studentId);
            return info;
        }
    }
}
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建对象
            Student objStudent = new Student();

            //给对象属性赋值
            objStudent.StudentId = 10001;
            objStudent.StudentName = "guobaoyan";

            //调用对象方法
            string info = objStudent.GetStudent();
            Console.WriteLine(info);
            //类的外面对字段的访问都是通过get和set方法
            Console.WriteLine(objStudent.StudentName);

            Console.ReadKey(true);
        }
    }
}
2) the class attributes and methods

Here Insert Picture Description

  • In class template to create one or more objects, attributes or an object by calling class;
  • Assigned to the object, value: Object Name property name;.
  • Call the method: the object name the method name;

(5) field attribute

1) access modifier

Access modifiers:
action: limiting object property or method to access a range (outside of the inner classes, and the like)
Type: Private (private: externally visible) public (public: the externally visible)

Note: The default private (internal use only class); hope in the form of access to the object needs to be defined for the public
2) field

Field called "member variables"; methods generally different class is for internal use; therefore use private modifier;
field naming nomenclature generally used camel
Here Insert Picture Description

The popular understanding of the field:
  • Privatization: Field similar to private property, for personal use; use of private modification;
  • Add Standard: to add a class in several fields, programs written to decide;
Attributes

Attributes are manipulated corresponding private field (get, set);

Role attributes:

Here Insert Picture Description

  • Action: The main use of the static attributes describe the object in object-oriented design features;
  • Requirements: Pascal naming method is generally used, and the same type of data fields, using the modified public;
Properties understanding:
  • Property entrance is actually outside access to the private fields, the property itself does not store any data;
  • Assigns them to actually private property points to a field assignment;
  • Reading the value of the property, the value of actually getting an attribute of the private field points;
Avoid illegal use of property data

In the get and set can be added in any logical that we need, so as to effectively prevent illegal data;
Here Insert Picture Description

Use the read-only attribute is set, write-only

There is no set method
Here Insert Picture Description

No corresponding private fields properties:

Without the corresponding private fields, but rather a logical calculation process
Here Insert Picture Description

Comparative attribute fields:

Fields (member variables):
  1. Mainly for internal use of the class for the data exchange, is generally Private field;
  2. Fields can be assigned, the value may be; (inner class)
  3. Field provides data to external time, set an attribute field of packaging, instead of using the total field, which is advocated by the object-oriented;
Attributes:
  1. Attribute data generally is to provide an outer, static characteristic is mainly used to describe the object, often it is public;
  2. According to their own logic, is set to read-only, write-only, improve security;
  3. Internal attributes may be added to the business logic required to prevent unauthorized transactions;
Automatic properties:

Private field can not be directly manipulated; not add business logic; can not set the read-only, write-only; applies only to standard forms;
Here Insert Picture Description

Published 34 original articles · won praise 0 · Views 998

Guess you like

Origin blog.csdn.net/forever_008/article/details/104069905