"Big Talk design mode" Note-Basics 2: Object-oriented Basics

Source: Internet
Author: User
Tags uppercase letter

The authors of this book describe each design pattern by using a class diagram (the UML class diagrams, the basics of which are described in addition to the object-oriented language applet + character dialogue interpretation knowledge points. This article is the introduction of "Big talk design mode" used in the object-oriented basic knowledge (based on C # language), easy to read the book code.

    1. Classes and instances

(1) Object

All things are objects, the object is to see, feel, hear, touch and other tangible things. To be precise, an object is a self-contained entity identified with a set of recognizable attributes and behaviors. Object-oriented programming is programming for objects.

(2) class

A class is an abstract collection of objects that have the same properties and functionality. Examples are as follows

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5B/7F/wKiom1UKSS_Qq_EqAAC4sURVlQk434.jpg "title=" Qq picture 20150319115724.png "alt=" Wkiom1ukss_qq_eqaac4survlqk434.jpg "/>

"class" is the keyword that defines the class, "Cat" is the name of the class, "shout" is the method of the class, and "public" means it is exposed.

(3) Example and instantiation

An instance is a real object . instantiation is the process of creating an object , which is created using the new keyword. Examples are as follows

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5B/7F/wKiom1UKSu_SHKRwAAF1Rk5G1Zc436.jpg "title=" QQ picture 20150319120441.png "alt=" Wkiom1uksu_shkrwaaf1rk5g1zc436.jpg "/>

2. Construction method

The constructor method is called the constructor , which is the initialization of the class. The constructor method has the same name as the class, has no return value, and does not require void to be called at New. All classes have a constructor method, and if you do not encode then the system generates an empty constructor by default, and if you have a defined constructor, the default construction method will fail.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5B/7A/wKioL1UKVxDDhXLcAALpZtml-Ok856.jpg "title=" QQ picture 20150319124914.png "alt=" Wkiol1ukvxddhxlcaalpztml-ok856.jpg "/>


3. Method overloading

method Overloading provides the ability to create multiple methods with the same name, but these methods require different parameter types. The method overload can be added on the basis of not changing the original method.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5B/7A/wKioL1UKV7OiScGtAAF2gpsclpE149.jpg "title=" QQ picture 20150319125406.png "alt=" Wkiol1ukv7oiscgtaaf2gpsclpe149.jpg "/>

4. Attributes and modifiers

(1) Properties

A property is a method or a pair of methods, but in the code that calls it, it is a field, that is, a property that is appropriate for using a method call in a field. The property has two methods get and set. The get accessor returns the same data type as the declared property, meaning that the value or reference of the internal field can be obtained when the call is made, and the set accessor does not display the setting parameters. But it also has an implicit argument with the keyword value, which is used to assign values to internal fields or references when the property is called.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5B/80/wKiom1UKWOyRljkaAAGjC4xE5_M736.jpg "title=" QQ picture 20150319130402.png "alt=" Wkiom1ukwoyrljkaaagjc4xe5_m736.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5B/80/wKiom1UKWVPhMqqWAAH22hsvlUI715.jpg "title=" QQ picture 20150319130551.png "alt=" Wkiom1ukwvphmqqwaah22hsvlui715.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5B/80/wKiom1UKWZyxds3MAACawsWIjfU494.jpg "title=" QQ picture 20150319130710.png "alt=" Wkiom1ukwzyxds3maacawswijfu494.jpg "/>

(2) modifier

Public indicates that the class member that it modifies can allow any other class to be accessed, commonly known as publicly owned.

Private means that only members of the same class are allowed access, and other subclasses that include it are inaccessible, commonly known as private.

Protected indicates that the subclass can have full access to the parent class when inheriting (see "6. Inheritance").

There are 2 other (later additions)

5. Encapsulation

Each object contains all the information it needs to operate, which is called encapsulation , so the object does not have to rely on other objects to do its work. Encapsulation has many advantages: first, good encapsulation can reduce coupling; second, the implementation of the class can be freely modified; third, the class has a clear external interface.

6. Inheritance

The inheritance of an object represents a ' is-a ' relationship, and if two objects A and B can be described as ' B is a ', then B can inherit a. Inheritors can also be understood as a specialization of the inherited person. Because it has the characteristics of the successor, but also has its own unique personality. If the subclass inherits from the parent class, the first, the subclass has the property and function of the parent class non-private, and the second, the child has its own property and function, that is, the subclass can extend the property and function that the parent class does not have, and the subclass can implement the function of the parent class in its own way (method rewriting ). In C #, a subclass inherits members from its parent class with methods, fields, properties, events, index indicators, but it cannot be inherited for construction methods and can only be called. You can use the base keyword for calling members of the parent class.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/7A/wKioL1UKXJLT3Xd_AAIbwRFNxQ0223.jpg "style=" float: none; "title=" QQ picture 20150319131354.png "alt=" Wkiol1ukxjlt3xd_aaibwrfnxq0223.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/80/wKiom1UKW2ig8_-HAAFt9UB59Zc194.jpg "style=" float: none; "title=" QQ picture 20150319131444.png "alt=" Wkiom1ukw2ig8_-haaft9ub59zc194.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/7A/wKioL1UKXdTyLFD0AAA3kvY5SdY772.jpg "style=" float: none; "title=" QQ picture 20150319131924.png "alt=" Wkiol1ukxdtylfd0aaa3kvy5sdy772.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/7A/wKioL1UKXdSRF0kaAAFxdy4UUvU742.jpg "style=" float: none; "title=" QQ picture 20150319131949.png "alt=" Wkiol1ukxdsrf0kaaafxdy4uuvu742.jpg "/>

The advantage of inheritance is that the public parts of all subclasses are placed in the parent class, so that the code is shared and the duplication is avoided, making it easier to modify or extend inherited implementations.

The disadvantage of inheritance is that the parent class has to be changed, the wrapper is broken, and the parent implementation details are exposed to the subclass.

7. polymorphic

polymorphic Representations of different objects can perform the same actions, but they are performed by their own implementation code. Note: First, subclasses appear as parent classes, and second, subclasses are implemented in their own way when they work; third, when a subclass appears as a parent, the properties and methods that are unique to the subclass are not available. In order for an instance of a subclass to completely replace a class member from a parent class, the parent class must declare the member as virtual. is implemented by adding the virtual keyword before the return type of the member. Subclasses can choose to use the override keyword to replace the parent class implementation with its own implementation, which is the method override override, or method overwrite. The principle of polymorphism is that when a method is called, no matter whether the object is converted to its parent class, only the method implementation at the end of the object's inheritance chain is called. That is, a virtual method is called dynamically by its run-time type, not by the compile-time type.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/80/wKiom1UKXqSxJgTeAACADL5LBtU696.jpg "style=" float: none; "title=" QQ picture 20150319132719.png "alt=" Wkiom1ukxqsxjgteaacadl5lbtu696.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/80/wKiom1UKXqfB3w9lAAIKyZq2euk046.jpg "style=" float: none; "title=" QQ picture 20150319132755.png "alt=" Wkiom1ukxqfb3w9laaikyzq2euk046.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/80/wKiom1UKXqmDEed_AAAxR_kkPwc279.jpg "style=" float: none; "title=" QQ picture 20150319132824.png "alt=" Wkiom1ukxqmdeed_aaaxr_kkpwc279.jpg "/>

8. Abstract class

C # allows classes and methods to be declared abstract, that is, abstraction classes and abstract methods . Note: First, abstract classes cannot be instantiated ; second, abstract methods are methods that must be overridden by the quilt class , and thirdly, if the class contains abstract methods, then the class must be defined as a draw class , whether or not it contains other general methods.

Abstract classes have as much common code as possible and have as little data as they can. Abstract classes typically represent an abstract concept that provides a starting point for inheritance, which must be inherited when designing a new abstract class.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5B/80/wKiom1UKYI7iXJlqAADt3sXa2PA552.jpg "title=" QQ picture 20150319133637.png "alt=" Wkiom1ukyi7ixjlqaadt3sxa2pa552.jpg "/>

9. Interface

An interface is the combination of implicit public methods and properties to encapsulate a collection of specific functions. Once the class implements an interface, the class can support all properties and members specified by the interface. Declaring an interface is syntactically identical to declaring an abstract class, but does not allow the execution of any member of the interface to perform the ancient way. The name of the interface, preceded by an uppercase letter ' I '. The interface is declared with interface, the method or property in the interface cannot precede a modifier, and the method has no method body.

Abstract class and interface differences: Abstract class can give some of the implementation of the member, the interface does not include the implementation of the member, abstract class of abstract members can be part of the implementation of the class, the interface members need to implement the full implementation of classes, a class can only inherit an abstract class, but can implement a number of interfaces, etc. class is an abstract object Abstract class is the abstraction of the class, the interface is the abstraction of the behavior, from the design point of view, abstract class is found from the subclass of the common thing, the generalization of the parent class, and then the subclass inherits the parent excrement, and the interface is not aware of the existence of subclasses, how the implementation of the method is not confirmed, pre-defined.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/80/wKiom1UKYhPhirN5AAFZJRWB3rA462.jpg "style=" float: none; "title=" QQ picture 20150319134235.png "alt=" Wkiom1ukyhphirn5aafzjrwb3ra462.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/7B/wKioL1UKY0GyWZ06AAHoVx690Rs021.jpg "style=" float: none; "title=" QQ picture 20150319134256.png "alt=" Wkiol1uky0gywz06aahovx690rs021.jpg "/>

10. Collection

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/80/wKiom1UKYvrAK55sAAI6nEkcGzA342.jpg "style=" float: none; "title=" QQ picture 20150319134548.png "alt=" Wkiom1ukyvrak55saai6nekcgza342.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/7B/wKioL1UKZCWhF8YBAAMVdymu8kc310.jpg "style=" float: none; "title=" QQ picture 20150319134618.png "alt=" Wkiol1ukzcwhf8ybaamvdymu8kc310.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/80/wKiom1UKYv_yn_PyAAPMWkidrYY331.jpg "style=" float: none; "title=" QQ picture 20150319134649.png "alt=" Wkiom1ukyv_yn_pyaapmwkidryy331.jpg "/>

11. Generics

Generics are classes, structs, interfaces, and methods that have placeholders (type parameters), which are placeholders for one or more types that are stored or used by classes, structs, interfaces, and methods. A generic collection class can use a type parameter as a placeholder for the type of object it stores, and the type parameter appears as the type of its field and the type of the parameter of its method. In general, it is recommended that you use a generic collection, because this gives you a direct advantage of type safety without the need to derive from a base collection type and implement a type-specific member. In addition, if the collection element is a value type, the performance of the generic collection type is generally better than the corresponding non-generic collection type (and is better than the type derived from the non-generic base collection type), because the element does not have to be boxed when the generic type is used.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5B/7B/wKioL1UKZJ6Af4y-AALYATCwijU777.jpg "title=" QQ picture 20150319134858.png "alt=" Wkiol1ukzj6af4y-aalyatcwiju777.jpg "/>

12. Delegates and Events

A delegate is a wrapper over a function that can be used as a name for a method's characteristics. An event is a special form of a delegate that handles the notification process when something meaningful happens.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/7B/wKioL1UKZhaAyQdLAAQmPVLnvd4613.jpg "style=" float: none; "title=" QQ picture 20150319135213.png "alt=" Wkiol1ukzhaayqdlaaqmpvlnvd4613.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/80/wKiom1UKZOzDrCt0AAGydbaCzSA339.jpg "style=" float: none; "title=" QQ picture 20150319135257.png "alt=" Wkiom1ukzozdrct0aagydbaczsa339.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/7B/wKioL1UKZhnD5N79AAEgm1J8CFc411.jpg "style=" float: none; "title=" QQ picture 20150319135318.png "alt=" Wkiol1ukzhnd5n79aaegm1j8cfc411.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/80/wKiom1UKZPGAiAX0AAHMfLNh4tk307.jpg "style=" float: none; "title=" QQ picture 20150319135351.png "alt=" Wkiom1ukzpgaiax0aahmflnh4tk307.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/80/wKiom1UKZWCCDIwRAAIWk8c984A754.jpg "style=" float: none; "title=" QQ picture 20150319135610.png "alt=" Wkiom1ukzwccdiwraaiwk8c984a754.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/7B/wKioL1UKZo2jR7taAAHunqWXtgQ752.jpg "style=" float: none; "title=" QQ picture 20150319135632.png "alt=" Wkiol1ukzo2jr7taaahunqwxtgq752.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5B/80/wKiom1UKZWXykIIhAAGOTiSE8b4793.jpg "style=" float: none; "title=" QQ picture 20150319135650.png "alt=" Wkiom1ukzwxykiihaagotise8b4793.jpg "/>


"Big Talk design mode" Note-Basics 2: Object-oriented Basics

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.