. NET OOP
First, the meaning of object-oriented programming
1. Functional (procedural) programming often results in a single application, where all functions are contained in several code modules (often a code module)
With OOP, many code modules are often used, each of which provides specific functionality, each of which is isolated and even completely isolated from other modules. This modular programming approach provides a lot of versatility, and the opportunity to reuse code is increased by playing.
(Put more effort into the design phase of the project)
2. The object is created from a type, and the type of the object is called a class. You can instantiate an object with the definition of the class. Class is equivalent to a car template or a plan for building a car, the car itself is an example of these plans and can be seen as an object.
3. (1) The data contained in the object can be accessed through properties and fields. This object data can be used to distinguish different objects, because different objects of the same class store different values in the properties and fields.
(2) The attribute does not provide direct access to the data (more control over the behavior) if the field, the value range of the field is limited only by the type of storage to which the information is changed.
(3) Accessibility: Usually set the field to private, access them through public properties, so that the code in the class can directly access the data stored in the field, while public properties prohibit external users from accessing the data in case they place invalid content in it.
4. Methods (functions in objects)
Method parameters with an identifier in (not using out and inout cases). Out. InOut (ref)
5. All objects
Object life cycle (in use, construction phase, destructor phase)
(1) (instance) constructor: called with the New keyword. Cupofcoffee Mycup = new Cupofcoffee ();() can be filled in parameters
(2) Destructors: Cleaning up objects
6. Static and Strength class members (not yet understood)
(1) Static constructors
(2) Static class
Second, OOP technology
1. Interface: Combine public instance (non-static) methods and properties to encapsulate a feature of a particular collection. Once an interface is defined, it can be implemented in a class. In this way, the class can support all the properties and members specified by the interface. Interface names usually start with I
Consider the interface as the contract between the creator and the user of the class ...
2. Objects that can be deleted:
IDisposable interface. Objects that support the IDisposable interface must implement their Dispose () method, that is, they must provide code for this method. But an object is no longer needed, and the calling method frees important resources.
The Using keyword can initialize objects that use important resources in a code block. The Dispose () method is called automatically at the end of the code block
<ClassName> <VariableName> = new <ClassName> ();
...
using (<VariableName>)
{
...
}
Or
using (<ClassName> <VariableName> = new <ClassName> ());
{
...
}
3. Inheritance
The inherited (derived) class also becomes the parent class (base class) objects in C # can only be derived directly from a base class
Inheritance can extend or create more specific classes from a more generic base class.
For example, a class animal that represents farm livestock, with methods such as Eatfood () or breed (), we can create a derived class cow, support all of these methods, and it also has its own method, into Moo () and Supplymilk (). You can also create another derived class, chicken, which has the cluck () and Layegg () methods.
Derived classes cannot access private members of the base class, but they can access public members.
Third accessibility protected, only derived classes can access protected members
External members cannot access protected and private members
A member of a base class can be virtual, and a member can be overridden by a class that inherits it. Virtual members cannot be private members
A base class can be defined as an abstract class. Abstract classes cannot be instantiated directly. To use an abstract class, you must inherit the class, and abstract classes can have abstract members that do not have implementation code in the base class that must be provided in the derived class.
All objects have a common base class of object.
4. Polymorphism
8.2.3 polymorphism
One of the inherited structures is that classes that derive from the base class have some overlap in methods and properties, so you can use the same syntax
Handles an object instantiated from the same base class. For example, if the base class animal has a Eatfood () method, it is derived from the
This method is called in the class cow and chicken, and its syntax is similar:
Cow Mycow = new Cow ();
Chicken Mychicken = new Chicken ();
Mycow.eatfood ();
Mychicken.eatfood ();
Polymorphism is one step further. You can assign a variable of a derived type to a variable of the base type, for example:
Animal myanimal = Mycow;
You can call a method of the base class from this variable without forcing the type conversion:
Myanimal.eatfood ();
The result is an implementation code that calls Eatfood () in a derived class. Note that the parties defined on the derived class cannot be called in the same way
method, the following code cannot be run:
Myanimal.moo ();
However, you can convert a variable of a primitive type to a derived class variable, calling the method of the derived class as follows:
Cow Mynewcow = (Cow) myanimal;
Mynewcow.moo ();
(All objects instantiated from a derived class can be thought of as instances of their parent class)
5. The polymorphism of the interface
Although you cannot instantiate an interface like an object, you can create a variable of the interface type, and then you can
On the image, use this variable to access the methods and properties provided by the interface.
For example, suppose you do not use the base class animal to provide the Eatfood () method, but instead place the method on the Iconsume interface. Cow
And the chicken class also support this interface, the only difference is that they must provide the implementation code of the Eatfood () method (because the interface does not package
Implementation code), you can then access the method using the following code:
Cow Mycow = new Cow ();
Chicken Mychicken = new Chicken ();
Iconsume Consumeinterface;
Consumeinterface = Mycow;
Consumeinterface.eatfood ();
Consumeinterface = Mychicken;
Consumeinterface.eatfood ();
This provides an easy way to access multiple objects in the same way, without relying on a common base class. For example, this connection
Ports can be implemented by the Venusflytrap class derived from vegetable, rather than by animal:
Venusflytrap myvenusflytrap = new Venusflytrap ();
Iconsume Consumeinterface;
Consumeinterface = Myvenusflytrap;
Consumeinterface.eatfood ();
In this code, the result of calling Consumeinterface.eatfood () is to call cow, Chicken, or Venusflytrap
The Eatfood () method of the class, depending on which instance is assigned the variable of the interface type.
Note that derived classes inherit the interfaces that their base classes support. In the first example above, either animal supports Iconsume,
Either cow and chicken support the Iconsume. Classes that have common base classes do not necessarily have a common interface, and vice versa.
6. Relationships between objects (inclusive, collection)
? Include relationship: One class contains another class. This is similar to the world relationship, but the containing class can control the
Even before using members of the included class.
? Set Relationship: A class is used as a container for multiple instances of another class. This is similar to an array of objects, but the collection has other functions
including indexing, sorting, and resizing.
7. Operator overloading
Write code for the overloaded operator, which is used as part of the class definition, and the operator acts on this class
8. Events
Use events to create event-driven applications
9. Reference types and value types
In C #, data is stored in a variable in one of two ways, depending on the type of the variable. The types of variables are divided into two kinds:
Reference types and value types are distinguished by the following:
? Value types store themselves and their contents in one place in memory.
? A reference type stores a reference to another location in memory (called a heap) and stores the content in another location.
In fact, when you use C #, you don't have to think too much about this issue. So far, the string variable used (this is the reference
Type) is exactly the same way you would use other simple variables, most of which are value types, such as int.
A major difference between a value type and a reference type is that a value type always contains a value, whereas a reference type can be NULL, indicating
They do not contain values. However, you can create a value type with a nullable type (which is a form of a generic) so that the value type
aspects behave in a manner similar to reference types (that is, can be null). This is a high-level topic, see Chapter 12th.
Only string and object simple types are reference types, but arrays are also implicit reference types. Each class that we create
is a reference type, which is why this is explained here.
The important difference between a struct type and a class is that the struct type is a value type.
8.3 OOP in Windows applications
The 2nd chapter describes how to create a simple Windows application in C #. Windows applications rely heavily on OOP technology
, this section will discuss OOP techniques and illustrate some of the arguments in this chapter. The following is illustrated by a simple example.
Give it a try: the applicable object
(1) Create a new Windows application ch08ex01 in the C:\BegVCSharp\Chapter08 directory.
(2) Use the Toolbox to add a New button control to the center of the Form1, as shown in 8-12.
(3) Double click the button, add code for mouse click event, modify code as follows:
private void Button1_Click (object sender, EventArgs e)
{
((Button) sender). Text = "clicked!";
Button Newbutton = New button ();
Newbutton.text = "New button!";
Newbutton.click + = new EventHandler (Newbutton_click);
Controls.Add (Newbutton);
}
private void Newbutton_click (object sender, System.EventArgs e)
{
((Button) sender). Text = "clicked!!";
}
}
Eighth Introduction to C # Object-oriented programming (object-oriented Programming,oop)