"Object-oriented" first talk

Source: Internet
Author: User
Tags float double

First, why do you need object-oriented?
All things in life are objects, and to describe their convenience, we use object-oriented approach to solve them.

Second, what is object-oriented programming?
According to the actual life (business), 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, more convenient.

Three, object-oriented three major characteristics: encapsulation, inheritance, polymorphism.
**********************************************************
I. Basic concepts of class
(i) Basic understanding of the class.
Class: A complex type that is defined by itself.
Classes generally consist of two parts: field-member variable (noun or adjective), Method-member function (verb)
Class Keywords: class
(ii). Definition of the class:
Class name
{
Definition of member variables;
...
Definition of member functions;
...
}
Access modifiers are often added to members of a class (member variables, member functions) (Public private protected)
Public-publicly owned. For example:...
Private-privately. For example:...


(iii) Use of the class:
Dog d = new Dog (); Instantiation of
D.name = "Wang Choi";
D.owner = "Zhang San";
D.run ();

D-the reference to the object.
New Dog ()-Real dogs object.

(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 and heap space.
1. Use the model diagram to explain:
A a1 = new A ();
A a2 = new A ();
Console.WriteLine (a1== A2); Why is the result false?


2. Use the model to explain: above the dog example, why the last D. Run () will show up as a Li family dog?


3. Interpretation by model: int a = 100; int b= 100; Console.WriteLine (A = = B); Why is the result true?

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.

II. member of Class--attribute

Properties: The port on which the member variable is accessed.
(a) The definition of the attribute:
Public Type property Name
{
Get
{
}
Set
{
The keyword value. Represents the value to be assigned.
}
}

(ii) Use of attributes:
is used directly from the property name.
Console.WriteLine (R.height); -Call property's get department
R.height = 180; -invokes the set part of the property.

Methods of class--member function--method overloading
Methods have the same names, different parameters (different types, different numbers)--the overloads of the method are formed.
Overloaded methods are independent of the return type.
Benefits:
1. Reduce the definition of the method name without remembering too many method names.
2. Reduce code duplication, greater reuse, 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.

"Object-oriented" first talk

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.