C # Object-oriented

Source: Internet
Author: User
Tags float double

One, object-oriented

(i) Why the need for object-oriented

All things in Life are objects (objects of all things), in order to describe their convenience, we use an object-oriented approach to solve

(ii), what is object-oriented programming

Depending on the actual life, the design of the corresponding class or object, according to the life of the process or logic to manipulate the program class or object, so more intuitive and convenient.

(iii) The three main features of object-oriented

1, encapsulation: encapsulated class = data + operation on this data (i.e. algorithm)

2, Inheritance: This feature can abstract real things into classes, and can also simulate the derivation of the relationship between things. It's also a way to reuse existing code without breaking existing code.

3, Polymorphic: A property owned by a base class, a subclass must have; but subclasses can have attributes that the base class does not have

Two, class

(i) Basic understanding of the class

Class: A complex type defined according to the actual situation

Include two parts: field--member variable (noun or adjective) method--member function (verb)

(ii) Definition of class

Class Keywords: class

[Public/private] class name
{
[public/private] Variable type variable name; Definition of a member variable; (definition of a field)
Public Property Type property name//attribute definition;

get{}
set{}

[Public/private] Returns the definition of a type function name (parameter list)//member function; (definition of method)


}

Access modifiers are often added to members of a class (member variables, member functions) (Public private protected)

Public: Publicly available, all classes are accessible

Private: Only accessible in this class

Protected: Protected and can only be called in subclasses

Class encapsulation: Do not arbitrarily set member variables to public, generally need to be set to private or protected, pass member methods or member properties to achieve the legal access to member variables.

(iii) Use of the class:
The instantiation of the class-generates the object.
Class Name Reference name = new class name ();

Call the members of the object:
Reference name. Member Variable name
Reference name. Property name
Reference name. Member Method Name ()

For example: Dog D=new dog ();

D.name = "Wang Choi";
D.owner = "Zhang San";
D.run ();

(iv) Additional

Types of variables in C #:
1. Value type:
int float double char bool struct DATETIME
2. Reference type:
A string array of custom classes.


Model diagram: Heap space: Variables of all value types are allocated in the stack space

Stack space: The objects referenced by all reference type variables, whose memory is allocated in the heap space
Explained by model diagram:
A a1 = new A ();
A a2 = new A ();
Console.WriteLine (a1== A2); Why is the result false?

Third, a member of a class-property: A port that accesses a member variable for secure assignment to a private member variable

(a) Definition:
Public Property Type property name
{
get//read part.
{
return member variable name.
}
set//Settings section
{
member Variable name = value;
}
}
public int Height
{
Get
{
return _height;
}
Set
{
if (value>0 && value<300)
{
_height = value;
}
Else
{
Console.WriteLine ("Error");
}
}
}

(ii) Use of attributes

Console.WriteLine (R.height); -Call the get part of the property
R.height = 180; -Property invokes the set part of the property on the left side of the equal sign.

Four, class method--member function--method overloading

Method names are the same, with different parameters (different parameter types, different number of parameters)--The method's overloads are formed

Benefits of Overloading: Method naming and memory convenience, improved code reuse, reduced duplication, and more reasonable code structure

When multiple overloaded methods are called, first find the method name, and then find the corresponding overloaded method according to the parameters.

Five, constructor: The function that the object is called automatically when it is instantiated. You cannot explicitly invoke the constructor of the current object using code.

Typically used in constructors to initialize an object (assigning an initial value to a member variable)

Defined

[Public/private] class name ()


Features: 1, objects are automatically invoked when instantiated;

2, which is the first member function to be called. The new class name ()-is actually a call to the constructor

3, constructors can overload

How do I call other constructors in the same class?

Class Chibang

{

public string _type;

public string _count;

}

Class Feixingqi

{

Public Feixingqi (string name)
{
name = name;
}
Public Feixingqi (String name,int count,string type): this (name)
{
_chibang = new Chibang ();
_chibang._type = Type;
_chibang._count = Count;
}

}

This represents the current object. This () invokes the other constructors of the current object. This.xxxx calls the other members of the current object.
Note: Call the This () of other constructors and do not write in the {} of the constructor function. Written after the declaration of the constructor function.

4. If you do not explicitly declare a constructor, the system automatically adds a default constructor. Once the constructor is explicitly declared, the default constructor is no longer automatically generated by the system.

Six, static members

In an object-oriented program, an instance of the class and a static member have the following access rules:

1, instance methods in the same class can be called directly to each other

2, the fields of the class (including instance fields and static fields) can be accessed directly by all instance methods in the same class

3, static methods in a class can only access the class static field directly (to access the instance data in a static method, you can instantiate the object by instantiating it)

Note: Static members belong to the class, no matter how many objects we build, static members in memory will always occupy only one area

Instance members are objects, and each object will have a storage area corresponding to the instance members

Invoke instance Member: Object name. Instance member name

Call static Member: Class name. static member name

Note: Only static members can be called in a static method. Instance methods can invoke instance members, or they can call static members.

Seven, inheritance

(a) What is inheritance

1, the related members in the parent class are automatically derived to the subclass.

2, subclasses can extend related members of the parent class

3, subclasses can be used in place of the parent class, but the parent class cannot replace the subclass to use

(ii) The syntax of inheritance

Class Subclass: Parent Class

{

}

(iii) The law of Succession

1, inheritance of member variables

(1) can be defined as public or protected, the subclass can access

(2) If the same name is hidden. Subclasses and parent classes each use their own members

2, Inheritance of attributes

(1) Attributes are generally only decorated with public

(2) If the same name is hidden. Subclasses and parent classes each use their own members

3. Inheritance of methods

(1) can be defined as public or protected, the subclass can access

(2) If the same name is hidden. Subclasses and parent classes each use their own members

(3) Rewrite: I, the method of the parent class is set to virtual method, virtual--This means that this parent method can be overridden

II, the method of overriding the parent class with override in the subclass--subclasses confirm that you want to override

Note: If virtual is not declared in the parent class method, there is an override in the subclass that overrides it, and an error is made.

II. If virtual is declared in the parent class, there is no override in the subclass to rewrite it, but it will not be registered but is hidden.

Iii. virtual method One virtual end. Subclasses this method is still a virtual method, even if the virtual method of the parent class is override. Subclasses of subclasses can also override the virtual method

4, inheritance of constructors

(1) If the parent class does not have a write constructor, or if the parent constructor has no arguments, the constructor for the subclass has no special requirements.

(2) If the constructor of the parent class has parameters, the subclass constructor must pass the value to the parent class constructor. Base ();

Note: When you call a method of a parent class in a subclass, you can use Base.xxxx to invoke it. Methods that are generally overridden by calling the parent class.

Eight, polymorphic: mainly implemented on the basis of inheritance

1, polymorphic classification:

(1) static polymorphic (compiled polymorphic): implemented by overloading of methods

(2) Dynamic polymorphism (running polymorphism): implemented by inheritance and rewriting

2, abstract class: A high degree of abstraction of things, abstract to a certain extent, can not be described in detail. Abstract classes are classes that cannot be instantiated and cannot be created by new

Abstract method: Only the declaration of the method, there is no implementation of the method. That is, only the function name, input parameters, return type, no function body (keyword abstract)

The relationship between abstract classes and abstract methods:
(1) Classes containing abstract methods must be abstract classes
(2) Abstract classes do not necessarily include abstract methods.

There are two ways to deal with abstract classes: (1) Find the derived class of the abstract class, use its derived class (2) to inherit this abstract class, implement its derived class

For abstract methods, subclasses must also use override to implement the abstract methods in the parent class.

C # Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.