3. Objective-C for encapsulation inheritance and Polymorphism

Source: Internet
Author: User

Three Characteristics of object-oriented encapsulation, inheritance, and Polymorphism

Encapsulation: Hiding internal implementations and stabilizing external interfaces.

Encapsulation is to define the class definition property definition method.

Property encapsulates the setter get Method

@ Property (nonatomic, retain) nsstring * Name, * sex;

Class encapsulates instance variables and Methods

. H file

# Import <Foundation/Foundation. h>

@ Interface person: nsobject

{

Nsstring * _ name;

Nsstring * _ sex;

Int _ age;

}

@ Property (nonatomic, retain) nsstring * Name, * sex;

@ Property (nonatomic, assign) int age;

-(ID) initwithname :( nsstring *) Name sex :( nsstring *) sex age :( INT) age;

-(Person *) work;

@ End

. M file

# Import "person. H"

@ Implementation person

@ Synthesize name = _ name, sex = _ sex, age = _ age;

-(ID) initwithname :( nsstring *) Name sex :( nsstring *) sex age :( INT) Age

{

Self = [Super init];

If (Self ){

Self. Name = Name;

Self. Sex = sex;

Self. Age = age;

}

Return self;

}

-(Person *) Work

{

Nslog (@ "% @ working", self. Name );

Return 0;

}

@ End

Inheritance: Child classes can directly reuse members in the parent class. Child classes inherit the declaration of all methods of the parent class and implement non-private instance variables and protocols.

Inheritance is to declare in. H that inheritance has a single and pass-through inheritance, that is, the common part of code optimization is handed over to the parent class.

# Import "person. H"

@ Interface worker: person // subclass: parent class

@ End

# Import "person. H"

@ Interface King: person

@ End

Inheritance in OC: Single-core (one sub-class can only have one super class)

Passed (a-> B-> C A-> C)

Polymorphism: different objects have different responses to the same message. sub-classes can override the parent class method.

Polymorphism means that the parameter or returned value of the method can be input or returned as a parent type.

Worker * worker = [[Worker alloc] init];

Worker. Name = @ "worker ";

[Worker work];

King * King = [[King alloc] init];

King. Name = @ "King ";

[King work];

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.