Dark Horse Programmer--the three characteristics of object-oriented programming

Source: Internet
Author: User

Dark Horse Programmer--the three characteristics of object-oriented programming

-------Android Training, iOS training, looking forward to communicating with you! ----------

Object-oriented Programming features: encapsulation, inheritance, polymorphism, (abstract)

Concept and principle of encapsulation * * *

Encapsulation is one of the object-oriented features

The benefits of encapsulation: reducing the likelihood of data being useless and ensuring data security

The drawback of not encapsulating: exposing your own attributes to the outside, losing management of that attribute

Encapsulation principle: the instance variable can only be accessed by the object method of the current class by default

* * Package Implementation * *

Define set instance variable (setter) methods and Access instance variable (getter) methods

1) Setter (set)

In the development process, taking into account the security requirements, generally do not use @public, @protected and other keyword decoration before the member variable, but use the Set method to provide the value of the member variable for the object. Some unreasonable assignments can be filtered within the Set method.

Set method Action: Provides a way for the outside world to set the value of member variables

Naming conventions:

1) method name must start with set

2) set followed by the member variable name, First Capital: _name setName

3) The return value must be void and cannot have a return value

4) Be sure to accept a parameter, and the parameter type requires the same type of member variable

5) The formal parameter name cannot be the same as the member variable name (the official recommended member variable name plus _)

For set, the member variable sets the value to use the Set method

2) getter (accessor)

Naming conventions:

1) There must be a return value, the return value type and the member variable are consistent

2) The method name is the same as the member variable (minus the underscore)

3) No parameters to accept

4) The Get method implementation must return the value of the instance variable

5) is an object method

Get values later, use the Get method uniformly

1#import <Foundation/Foundation.h>2 @interface Person:nsobject3 {4    //Age5    int_age;6    //Height7    float_height;8 }9 //define two methods for each instance variable: Set the variable's method (set), get the variable worth method (get)Ten //Set Specification: One /* A 1) must be an object method - 2) The return value must be void and cannot have a return value - 3) method name must start with set the 4) Set followed by member instance variable name, first letter capitalized - 5) There must be parameters, and parameter types require the same type of member variable - 6) Formal parameter name is usually the name of the instance variable to remove the underscore - 7) Parameter name cannot be the same as instance variable + 8) In the implementation of the Set method, be sure to assign values to the instance variables with the formal parameters - */ +-(void) Setage: (int) age; A-(void) SetHeight: (float) height; at /*Get naming conventions: - 1) There must be a return value, the return value type and the member variable are consistent - 2) The method name is the same as the member variable (minus the underscore) - 3) No parameters to accept - 4) The Get method implementation must return the value of the instance variable - 5) is an object method in */ --(int) age; to-(float) height; + @end - @implementation Person the //Set method Implementation * //in the implementation of the Set method, it is important to assign values to the instance variables using formal parameters $-(void) Setage: (int) age{Panax Notoginseng_age=Age ; -    } the-(void) SetHeight: (float) height{ +_height=height; A    } the //Get method Implementation + //in the Get method implementation, the value of the instance variable must be returned --(int) age{ $    return_age; $    } --(float) height{ -    return_height; the    } - @endWuyi intMain (intargcConst Char*argv[]) the { -NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];  Wu     //Enter the OBJECTIVE-C code here -Person *p=[personNew]; About[P Setage: -]; $[P SetHeight:2.12f]; -NSLog (@"%d,%.2f", [P age],[p height]); - [Pool drain]; -     return 0; A}

Inheritance and derivation of classes

Inherit * * *

Concept: Class B Inherits Class A, so Class B has all the properties and methods of Class A. A derives a B

Advantages and Disadvantages

Benefits of Inheritance: Subclasses have all member variables and methods of the parent class, reducing duplicate code. Establish a relationship between classes.

Disadvantages of Inheritance: code coupling is too strong (the relationship between classes is too tight, and if a class is broken, another class is affected).

NSObject is a base class, and almost all classes eventually inherit from it.

Inheritance considerations:

1. A subclass cannot define a variable with the same name as the parent class, but can inherit a variable from the parent class

2. OC class supports single inheritance, does not support multiple inheritance, can only have one parent class

3, OC Support Multilayer inheritance

polymorphic * * *

Concept: Polymorphism is the multi-form of a class of things, the ability of different objects to respond to the same name method in their own way becomes polymorphic

Conditions: There are inheritance relationships, there are methods to rewrite,

The pointer of the parent class points to the object of the subclass function

Dog *g=[dog New];

Zeo *z=[dog New];

Pros: Simplifies programming interfaces, allowing for the reuse of some customary naming between classes and classes

@interface Animal:nsobject

-(void) run;

@end

@implementation Animal

-(void) run{

NSLog (@ "The animal is running");

}

@end

Dog *******

Subclass inheritance No more statements.

@interface Dog:animal

-(void) run;//(can not write)

@end

@implementation Dog

-(void) run{

NSLog (@ "The dog is Running");

}

@end

Cat *******

@interface Cat:animal

-(void) run;//(can not write)

@end

@implementation Cat

-(void) run{

NSLog (@ "The cat is Running");

}

@end

Do not use polymorphic ************

Animal *ani=[animal New];

[Ani run];

Dog *dog=[dog New];

[Dog run];

Cat *cat=[cat New];

[Cat Run];

Using polymorphic **********

Parent class pointer to child class object

Animal *a1=[dog New];

[Ani run];//Dog is running

Animal *a2=[cat New];

[Ani run];//Cat is running

Polymorphic Note:

1, if there is polymorphism, the parent class is able to access the child class-specific methods

However, the compiler will make an error and need to cast: [(Subclass name *) pointer name subclass-specific properties];

2. If there is no polymorphism, the parent class cannot access the subclass-specific method

-------Android Training, iOS training, looking forward to communicating with you! ----------

Dark Horse Programmer--the three characteristics of object-oriented programming

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.