03-Object-oriented Syntax 2

Source: Internet
Author: User

First,set method and get method

1. use of set method and get method

@public members can be arbitrarily assigned, you should use the set method and The Get method to manage access to members

2. Set method

1> function: Used to set member variables, you can filter out some unreasonable values in the method

2> naming specification:

The * method starts with set, followed by the member variable name, and the first letter of the member variable name is capitalized

* Parameter names do not have the same name as member variables

3. Get method

1> function: Returns the member variable inside the object

2> naming conventions: The name of the Get method is typically the same as the member variable

4. naming conventions for member variables

* member variables are preceded by the underscore _

* can be separated from the name of the Get method

* can be separated from other local variables, a variable to see the beginning of the underscore, is definitely a member variable

5. code Examples

#import<Foundation/Foundation.h>//Statement@interfacecar:nsobject{int_wheels;//Number of wheels}/*Set Method*/- (void) Setwheels: (int) Wheels;/*Get Method*/- (int) Wheels;@end@implementationCar//implementation of Set method- (void) Setwheels: (int) wheels{//filter the number of wheels that come in from outside    if(wheels<=0) {Wheels=1; } _wheels=Wheels;}//implementation of the Get method- (int) wheels{return_wheels;}@end 

Ii. Types of methods

1. Basic Concepts

Methods that can be executed directly with the class name

2. Comparison of class methods and Object methods

1> object Methods

* with minus - start

* can only be called by the object, no object, this method cannot be executed at all

* Object methods can access instance variables (member variables)

2> class Methods

* start with a plus + +

* can only be called with the class name, the object cannot be called

* instance variables (member variables) cannot be accessed in a class method

* Application: When you do not need to access member variables, try to use the class method

3> class Methods and object methods can have the same name

third,self keyword

1. member variables and local variables have the same name

* when the member variable and local variable have the same name, the nearest principle is taken, and the local variable is accessed.

* Access member variables with self to distinguish local variables with the same name

2. Usage Details

1> where it occurs: all OC Methods (object method \ class method), cannot appear in the function

2> role

* use "self-> member variable name " to access member variables of the current method call

* use "[Self Method name ];" to invoke the method (object method \ class method)

Iv. Inheritance

1. Basic usage of inheritance

* Design two classes of Bird,Dog

//Bird's statement@interfacebird:nsobject{@public    intweight;}- (void) eat;@end//definition of bird@implementationBird- (void) Eat {NSLog (@"eat-weight:%d", weight);}@end//the statement of dog@interfacedog:nsobject{@public    intweight;}- (void) eat;@end//definition of dog@implementationDog- (void) Eat {NSLog (@"eat-weight:%d", weight);}@end

* because of the same properties and behavior, you can extract a parent class Animal

 //  animal declaration    animal:nsobject{  @public  int   weight;}  -(void    @end  //  animal definition   @implementation   Animal -(void  ) Eat {NSLog ( @ "  eat-weight:%d     @end  

* subclass in the parent class based on the extension of the properties and methods

//Bird's statement@interfacebird:animal{@public    intheight;}- (void) Fly;@end//definition of bird@implementationBird- (void) Fly {NSLog (@"Flying Fly -height:%d", height);}@end//the statement of dog@interfacedog:animal{@public    intSpeed ;}- (void) run;@end//definition of dog@implementationDog- (void) Run {NSLog (@"Run Run -height:%d", speed);}@end

Attention:

* Subclass method and property Access procedure: If the subclass does not, go to the parent class's

* Parent class is inherited or can be used as usual

* subclasses and Parent classes cannot have the same member variable

* method can be overridden

2. Super keyword

* Call the parent class's object methods and class methods, respectively

3. Benefits of inheritance

* do not change the original model on the basis of the extension charge method

* establish a link between classes and classes

* extraction of public code

* Disadvantages: Strong coupling

4. application of inheritance

* all of its properties are what you want, generally inherited

* part of its properties is what you want, you can extract another parent class

v. polymorphism

1. The basic concept of polymorphism

* multiple forms of a certain class of things

* OC objects are polymorphic

2. polymorphism of the embodiment

New ];p; [ P walk];

* Subclass Object assigned to parent class pointer

* parent pointer access to corresponding properties and methods

3. polymorphism Benefits

* Accept parameters with parent class, save code

4. Polymorphism Limitations

* Properties of subclasses cannot be accessed (cast may be considered)

5. polymorphic Details

* Dynamic binding: Determines the method of dynamic invocation at run time based on the type of object  

03-Object-oriented Syntax 2

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.