Three main features of OC objects: encapsulation, inheritance and polymorphism, and oc

Source: Internet
Author: User

Three main features of OC objects: encapsulation, inheritance and polymorphism, and oc

I. Encapsulation

Encapsulation: hides the attributes and implementation details of an object, and only exposes the interface to control the read and modify access levels of attributes in the program.

Person. h:

1 # import <Foundation/Foundation. h> 2 3 @ interface Person: NSObject4/** age */5 @ property (nonatomic, assign) int age; 6 7 @ end

 

Person. m:

1 # import "Person. h "2 3 @ implementation Person 4 5 # pragma mark-Rewrite set Method 6-(void) setAge :( int) age {7 if (age <0) {8 NSLog (@ "age cannot be negative"); 9 return; 10} else {11 _ age = age; 12 return; 13} 14} 15 16 @ end

 

Advantages:

1. Hiding internal implementation details and setting access permissions improves data security.

2. Any incoming and outgoing data must flow through the interface, and the set method can be rewritten to filter data.

 

Ii. Inheritance

Inheritance: an object directly uses the attributes and methods of another object.

Person. h:

1 # import <Foundation/Foundation. h> 2 3 @ interface Person: NSObject4/** age */5 @ property (nonatomic, assign) int age; 6/** method of eating */7-(void) eat; 8 9 @ end

 

Person. m:

1 # import "Person. h "2 3 @ implementation Person 4 5 # pragma mark-Rewrite set Method 6-(void) setAge :( int) age {7 if (age <0) {8 NSLog (@ "age cannot be negative"); 9 return; 10} else {11 _ age = age; 12 return; 13} 14} 15 16 # pragma mark-Method of eating 17-(void) eat {18 NSLog (@ "Person"); 19} 20 21 @ end

 

GoodPerson. h:

1 # import "Person. h "2 3 // inherit the parent class Person4 @ interface GoodPerson: Person5/** name */6 @ property (nonatomic, strong) NSString * name; 7 8 @ end

 

 

GoodPerson. m:

1 # import "GoodPerson. h" 2 3 @ implementation GoodPerson4 5-(void) eat {6 NSLog (@ "GoodPerson dinner"); 7} 8 9 @ end

 

Main. m:

1 # import <Foundation/Foundation. h> 2 # import "Person. h "3 # import" GoodPerson. h "4 5 6 int main (int argc, const char * argv []) {7/** Person */8 Person * p = [[Person alloc] init]; 9 p. age = 20; 10 [p eat]; 11 12/** good guy */13 GoodPerson * goodP = [[GoodPerson alloc] init]; 14 goodP. age = 30; 15 [goodP eat]; 16 17 // good name 18 goodP. name = @ "Lkun"; 19 NSLog (@ "name = % @", goodP. name); 20 21 22 return 0; 23}

 

Compile the running result:

17:53:37. 484 01-discription [634: 455001] Person meals 17:53:37. 485 01-discription [634: 455001] GoodPerson dinner 17:53:37. 485 01-discription [634: 455001] Good guy name = LkunProgram ended with exit code: 0

 

 

Note:

1. Most classes inherit from the NSObject parent class, so they also inherit the attributes and methods of this class.

2. Although GoodPerson inherits the property age and method eat of the parent class Person, GoodPerson has its unique method name and overwrites the method eat of the parent class.

3. When the subclass object receives the method message, it searches layer by layer and finds and executes the message. For example, the setName method can be found in the GoodPerson parent class, And the setAge class can be found only in the Person parent class.

Advantages: extracting duplicate codes and establishing connections

Disadvantage: Strong Coupling

Inheritance: Dogs are animals, so dogs inherit animals.

Combination: students own dogs, so dogs are an attribute of students.

 

Iii. Polymorphism

Polymorphism: the parent class Pointer Points to the subclass object. The parent class type parameter can receive incoming values of the subclass type parameter.

Person. h:

1 # import <Foundation/Foundation. h> 2 # import "Animal. h "3 4 @ interface Person: NSObject5 // Method 6-(void) feedAnimal :( Animal *) animal; 7 8 @ end

 

Person. m:

1 #import "Person.h"2 3 @implementation Person4 5 - (void)feedAnimal:(Animal *)animal {6     [animal eat];7 }8 9 @end

 

Animal. h:

1 # import <Foundation/Foundation. h> 2 3 @ interface Animal: NSObject4/** how to eat */5-(void) eat; 6 7 @ end

 

Animal. m:

1 #import "Animal.h"2 3 @implementation Animal4 5 - (void)eat {6     NSLog(@"Animal eat");7 }8 9 @end

 

Cat. h:

1 #import "Animal.h"2 3 @interface Cat : Animal4 5 @end

 

Cat. m:

1 # import "Cat. h "2 3 @ implementation Cat4 # pragma mark-override the parent class eating Method 5-(void) eat {6 NSLog (@" Cat eat "); 7} 8 9 @ end

 

Dog. h:

1 #import "Animal.h"2 3 @interface Dog : Animal4 5 @end

 

Dog. m:

1 # import "Dog. h "2 3 @ implementation Dog4 # pragma mark-override the parent class eating Method 5-(void) eat {6 NSLog (@" Dog eat "); 7} 8 9 @ end

Main. m:

1 # import <Foundation/Foundation. h> 2 # import "Person. h "3 # import" Animal. h "4 # import" Cat. h "5 # import" Dog. h "6 7 8 int main (int argc, const char * argv []) {9/** persons */10 persons * p = [[Person alloc] init]; 11 12/** Animal */13 Animal * a = [[Animal alloc] init]; 14 15/** Cat */16 Cat * c = [[Cat alloc] init]; 17 18 // The parent class Pointer Points to the Dog Instance Object 19 Animal * d = [[Dog alloc] init]; 20 21 22 // dynamic detection of the real object is Dog23 [d eat]; 24 25 // parameters of the parent class pointer type can also receive parameters of the subclass pointer type 26 [p feedAnimal: a]; 27 [p feedAnimal: c]; 28 29 30 return 0; 31}

 

Compile the running result:

2016-08-18 19:42:59.467 01-discription[759:669325] Dog eat2016-08-18 19:42:59.468 01-discription[759:669325] Animal eat2016-08-18 19:42:59.468 01-discription[759:669325] Cat eatProgram ended with exit code: 0

Note:

1. The oc language dynamically detects and calls real objects during compilation, regardless of the type of pointer.

2. When the parent class sub-needle is used as the parameter type, its sub-class sub-needle parameters can be passed in

3. The parent class Pointer Points to a subclass object and cannot use the unique method of the subclass. However, you can use the forced conversion type to call

 

Related Article

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.