iOS Learning Journey--oc Object relationships

Source: Internet
Author: User

1. Anonymous object: There is no moniker
1. Anonymous objects are used only once
Usage scenarios:
1. Anonymous objects can be used when we want to invoke only one method of an object at a time

2. The anonymous object can be used as the actual parameter of the function

 #import <Foundation/Foundation.h> #import "CZPerson.h" #import "CZBook.h" @ Interface czbook:nsobject@end@implementation czbook@end@interface czperson:nsobject{int _age;//age NSString *_na me;//name}//Write method setter-(void) Setage: (int) age;//Read method getter-(int) age;-(void) SetName: (NSString *) name;-(NSString *) NA me;-(void) Readbook: (Czbook *) book; @end @implementation czperson//Write Method setter-(void) Setage: (int) age{_age = age;} Read method getter-(int) age{return _age;} -(void) SetName: (NSString *) name{_name = name;} -(NSString *) name{return _name;}    -(void) Readbook: (Czbook *) book{} @endint main (int argc, const char * argv[]) {[[Czperson new] setage:10]; /* 1, first create an anonymous person object 2, call the Readbook Method 3 of the person object that you just created, and pass in an anonymous Czbook object [Czperson new] To create an anonymous human object [Czbook        New] Creates an anonymous book object */[[Czperson New] Readbook:[czbook new];            NSLog (@ "%d", [[Czperson new] age]); return 0;} 
2. Dependency Relationship:
A object is a local variable of the B object or a method, B depends on a, and we call A and B a dependency relationship.
In object-oriented design:
Coupling degree: When changing an object. The degree of influence on another object
Low coupling: When changing an object's impact on other objects is relatively small
High cohesion: An object that only does its own related things
Object-oriented design principles: a single responsibility principle

#import <Foundation/Foundation.h> @interface czbeauty:nsobject{nsstring * _name;//name}-(void) SetName: (nsstring *) name;-(NSString *) name;/** * Call local tyrants */-(void) Calltuhaowithphone: (Czphone *) phone;-(void) sendmessagetodiaosi;@e Nd@implementation czbeauty-(void) SetName: (NSString *) name{_name = name;} -(NSString *) name{return _name;}    /** * to local tyrants call, the object as the form of the */-(void) Calltuhaowithphone: (Czphone *) phone{NSLog (@ "to local tyrants called"); [Phone call:@ "Local tyrants's phone number"];}       -(void) sendmessagetodiaosi{NSLog (@ "Send a message to the cock");//object as a local variable of another class Czphone *phone = [Czphone new]; [Phone sendmessage:@] Oh, do you want me to watch a movie? "];} @end @interface czphone:nsobject/** * Texting * * @param message content */-(void) SendMessage: (NSString *) message;/** * Power Word */-(void) Call: (NSString *) phonenum; @end @implementation czphone/** * Text message * * @param message message content */-(void) sendmess Age: (NSString *) message{NSLog (@ "Send a%@ message", message); /** * Calling */-(void) Call: (NSString *) phonenum{NSLog (@ "GivePhone number for%@ people call ", Phonenum);} @endint Main (int argc, const char * argv[]) {//Create a beauty czbeauty *beauty = [czbeauty new];//Create a phone czphone *phone        = [Czphone new];     [Beauty setname:@ "FBB"];        [Beauty Calltuhaowithphone:phone];        [Beauty Sendmessagetodiaosi]; return 0;}

3. Association relationship (multiple files)
Hasa relationship: When an object has another object,
When the A object is a member variable of the B object,
An association relationship exists between a object and a B object


Association relationships are most often used in object-oriented relationships
---------------------------------------------------below to learn about
The coupling degree of correlation relation is higher than the dependence relation

One-on-one: unilateral dependence, bilateral dependence
One-to-many relationship: classes and students, a class can have more than one student
Many-to-many relationships: students and courses. A student can learn multiple courses, a course is taught by multiple students


#import <Foundation/Foundation.h> #import "CZBeauty.h" #import "CZNewBeauty.h" int main (int argc, const char * argv[    ]) {Czbeauty *beauty = [Czbeauty new];        Czipad *ipad = [Czipad new];    Cznewbeauty *newbeauty = [Cznewbeauty new];        [NewBeauty Setipad:ipad];    [NewBeauty Lookatmovie];    [NewBeauty Listentomusic];    [NewBeauty PlayGame];        [NewBeauty Takeselfphoto]; return 0;} #import <Foundation/Foundation.h> #import "CZIPad.h"/* When we always use an object in a class as a formal parameter, we will extract the parameter as a member variable */@interface czbeauty:nsobject//use ipad to watch movies-(void) Lookatmoviewithipad: (Czipad *) ipad;//use ipad to listen to music-(void) Listentomusicwithipad: ( Czipad *) ipad;/** * Beauty play game */-(void) Playgamewithipad: (Czipad *) ipad;/** * beauty selfie */-(void) Takeselfphotowithipad: (Czipa d *) ipad, @end #import "CZBeauty.h" @implementation czbeauty//to watch movies with ipad-(void) Lookatmoviewithipad: (Czipad *) ipad{NSL OG (@ "Beauty wants to see a movie!")

"); [ipad Playmovie];} Use ipad to listen to music-(void) Listentomusicwithipad: (Czipad *) ipad{NSLog (@ "Beauty to listen to music!") "); [ipad playmusic];} /** * Beauty play game */-(void) Playgamewithipad: (Czipad *) ipad{NSLog (@ "Beauty wants to play the game!")

"); [ipad playGame];} /** * Beauty selfie */-(void) Takeselfphotowithipad: (Czipad *) ipad{NSLog (@ "Beauty is going to take a selfie!")

"); [ipad Takephoto];} @end #import <Foundation/Foundation.h> @interface czipad:nsobject//play Movie-(void) playmovie;//play music-(void) playmusic;//Play Game-(void) playgame;//photography-(void) Takephoto; @end #import "CZIPad.h" @implementation czipad//play Movie-(void) playmovie{NSLog (@ "ipad play movie"); Play music-(void) playmusic{NSLog (@ "ipad play Music");} Play games-(void) playgame{NSLog (@ "ipad play Game");} Photo-(void) takephoto{NSLog (@ "ipad selfie");} @end #import <Foundation/Foundation.h> #import "CZIPad.h" @interface cznewbeauty:nsobject{//let Belle have an ipad Czipad * _ipad;} Writing method. Buy a ipad-(void) Setipad: (Czipad *) ipad;//Watch movie-(void) lookatmovie;//listen to music-(void) listentomusic;/** * Beauty play Games */-(void) PLAYG ame;/** * Beauty selfie */-(void) Takeselfphoto; @end #import "CZNewBeauty.h" @implementation cznewbeauty//Write method, buy a ipad-(void) Setipad: (Czipad *) ipad{_ipad = IPad;} See the movie-(void) lookatmovie{NSLog (@ "Beauty watch movie"); [_ipad Playmovie];} Listen to music-(void) listentomusic{NSLog (@ "Beauty listens to Music"); [_ipad Playmusic];} /** * Beauty Play game */-(vOID) playgame{NSLog (@ "Beauty play Game"); [_ipad playGame];} /** * Beauty selfie */-(void) takeselfphoto{NSLog (@ "beauty selfie"); [_ipad Takephoto];} @end

Learning experience:

Through the corresponding examples, proficiency in object-oriented, for future learning to lay a good foundation

iOS Learning Journey--oc Object relationships

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.