oc--three main features of object-oriented

Source: Internet
Author: User

Object-oriented three major features: package, inheritance, and polymorphic encapsulation of member variables:/*Set Method: 1. Function: Provides a method for setting member variables to the outside World 2. Naming specification: The 1> method name must begin with set 2>set followed by the name of the member variable, the The first letter must be capitalized 3> The return value must be void 4> must accept a parameter, and the parameter type is consistent with the member variable type 5> the name of the formal parameter cannot be the same as the member variable name*/- (void) Setage: (int) NewAge;/*Get method: 1. Function: Returns the member variable within the Object 2. Naming specification: 1> must have a return value, the return value type must be consistent with the member variable type 2> The method name is the same as the member variable name 3> does not need to accept the naming conventions of any parameter member variables: Be sure to start with the following lines: 1. Make the name of the member variable and the Get method separate from 2. Can be separated from local variables, see A variable that begins with a dash, usually a member variable*/- (int) age;/*Object Method 1> minus-start 2> can only be called by the object to invoke the 3> object method to access the current object's member variable (instance variable) class method 1> add Good + The benefit of the 2> class method, which can only be called by the class (name), cannot be accessed by the 3> class method, and the application 1> does not depend on the object, the execution efficiency is high 2> can use the class method , try to use the class method 3> the occasion: When the method does not need to use the member variable, it can be changed to class methods can allow class methods and object methods with the same name of the tool class: basically no member variable, the method is basically a class method*/Self : (pointer) points to the method caller, which represents the purpose of the current object self:1>who calls the current method, self represents who*Self appears in the object method, and self represents the object*Self appears in the class, and self represents the class2> can use the self->member variable "to access member variables inside the current object3>[selff method Name] can call other object methods/class method Inheritance/*1. Benefits of Inheritance: 1> extracting duplicate Code 2> establishes a relationship between classes 3> subclasses can have all member variables and Method 2 in the parent class. Note 1> basically all classes of The root class is NSObject 2> the parent class must be declared before the subclass 3> does not allow the child class and the parent class to have the same name as the member variable override: Subclasses re-implement a method in the parent class, overriding the previous practice of the parent class 4> subclasses can have A method with the same name as the parent class, the call takes precedence over the subclass to find the method, if not found, go to the parent Class 3. Disadvantages: Coupling is too strong 4. The use of inheritance 1> when two have the same properties and methods, they can say the same thing to a parent class 2&G            T; When Class A has some of the properties and methods in class B, consider letting Class B inherit a class A {int _age;         int _no;         } b:a {int _weight;              }//Inherit: xx is xxx//combination: XXX has XXX 2. combination A {int _age;         int _no;             } B {A *_a;         int _weight; }*/the role of Super1call a method in the parent class directly2. Super is in an object method, then the object method of the parent class is called the super is in the class method, then the class method of the parent class is called3. Usage scenarios: Subclasses overriding the parent class's methods when they want to preserve some behavior polymorphism (multiple patterns) of the parent class1No inheritance, no polymorphism.2the embodiment of the code: a pointer to a parent class type pointing to a subclass object3Benefits: If you are using a parent class type in a function \ method parameter, you can pass in the parent class, the child class object4. Limitations:1> A variable of the parent class type cannot directly invoke a subclass-specific method, and must be strongly converted to a subclass-type variable before invoking the subclass-specific method directly

oc--three main features of 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.