OC learning 01-class and Object

Source: Internet
Author: User

OC learning 01-class and Object
Object-Oriented Programming

Basic:

Concept: object class encapsulation polymorphism main languages include C ++, Java, OC

Differences between process-oriented and object-oriented

  Process-oriented Object-oriented
Features Analysis Steps to implement functions Analyze the problem and find out the involved objects and their functions
Focus Implement Functions Object Design (specific function)
Example C Language OC, C ++, java

Basic code:

// OC print NSLog (@ Lanou); // NSInteger, CGFloat float, NSString NSInteger I = 100; NSLog (@ I = % ld, I ); CGFloat f = 3.14; NSLog (@ f = % g, f); // The OC string can contain the Chinese NSString * str = @; NSLog (@ % @, str ); // OC array // just like for loop traversal, you can directly use NSLog to traverse NSArray * arr = @ [@ 1, @ 2, @ 3]; NSLog (@ % @, arr); for (NSInteger I = 0; I <3; I ++) {NSLog (@%@, arr [I]);}
Class and object class and object are the core classes of object-oriented programming. Abstract objects with the same features and behavior are class instances. Classes are the types of objects. class definitions in OC are first defined. class, create an object and define the object in two parts. The interface and implementation (note separate writing) file :. h is the interface file ,. m is the implementation file
Interface
@interface Student : NSObject@end

Implementation

// Corresponding implementation file @ implementation Student
Create an object any object occupies space. Create an object in two steps.
① Allocate space ② initialize
// 1. allocate space Student * stu = [Student alloc]; // 2. initialize stu = [stu init]; // combine the previous two steps into a statement Student * stu = [[Student alloc] init];

Self returned during initialization refers to the self returned after completion.

-(Id) init {_ stuName = @; _ stuSex = @ female; _ stuholobby = @ male; _ stuAge = 20; _ stuScore = 100; return self ;}
Objects used
    Student *stu = [[Student alloc] init];    [stu sayHi];
Only a small number of settings are made when instance variables are initialized. the visibility of instance variables can be improved in the future.
@ Public @ private @ protected
@ Interface Student: NSObject {// visibility of member variables @ public // member variables, or instance variable NSString * _ stuName; // name NSInteger _ stuAge; // age NSString * _ stuSex; // gender CGFloat _ stuScore; // score NSString * _ stuholobby; // hobby} @ end

Operation Member

// Operation member variable // The NSLog (@ % ld, stu-> _ stuAge) of the member variable is accessed through-> ); // modify the age stu-> _ stuAge = 100; NSLog (@ % ld, stu-> _ stuAge); // modify the name, directly change the string to stu-> _ stuName = @; NSLog (@ % @, stu-> _ stuName );

Note: For public modified instance variables, you can directly use "->" to access

Some code

Main. m

Int main (int argc, const char * argv []) {// creates an object through the mobile phone class, modify the MobilePhone * mPhone = [[MobilePhone alloc] init]; mPhone-> _ phoneBrand = @ samsung; mPhone-> _ phoneColor = @ white; mPhone-> _ phoneModel = @ NAS; mPhone-> _ phonePrice = 4800; mPhone-> _ phoneSize = 1080; NSLog (@ brand: % @, color: % @, model: % @, price: % ld, screen: % ld, mPhone-> _ phoneBrand, mPhone-> _ phoneColor, mPhone-> _ phoneModel, mPhone-> _ phonePrice, mPhone-> _ phoneSize); return 0 ;}

MobilePhone. h

@ Interface MobilePhone: NSObject {@ public NSString * _ phoneBrand; NSString * _ phoneModel; NSInteger _ phoneSize; NSString * _ phoneColor; NSInteger _ phonePrice;}-(void) buyPhone; -(void) call;-(void) sendMessage;-(void) surfInternet;-(void) watchVideo; // write a function to print all information-(void) sayHi; @ end

MobilePhone. m

@ Implementation MobilePhone-(void) buyPhone {NSLog (@ bought a mobile phone);}-(void) call {NSLog (@ called);}-(void) sendMessage {NSLog (@ sent a text message);}-(void) surfInternet {NSLog (@ posted on the network);}-(void) watchVideo {NSLog (@ watched a video);} // call the initialization method-(id) init {_ phoneBrand = @ Apple; _ phoneColor = @ red; _ phoneModel = @ S5; _ phonePrice = 4000; _ phoneSize = 1080; return self;}-(void) sayHi {NSLog (@ % @, % ld, % ld, _ phoneBrand, _ phoneColor, _ phoneModel, _ phonePrice, _ phoneSize );}

The article summarizes the basic content of today's Learning

 

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.