OC Visibility, method

Source: Internet
Author: User

Main.m

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "Student.h"

#import "AudiCar.h"

Enumeration in OC

Ns_enum (Nsuinteger, Nsseason) {

Spring,

Summer,

Autumn = 1<<4,

Winter = 1<<3

};

int main (int argc, const char * argv[]) {

Create an Object

Student *stu = [[Student alloc] init];

[Stu Sayhi];

//

NSLog (@ "%@", stu->_stuhobby);

Methods are divided into two, one is the + number method, called the class method, this method general class to use, such as Alloc

The second method is the-number method, called the instance method, which is called by the general object, for example, we write our own sayhi,init

Student *stu =[[student alloc] init];

[Stu setstuname:@ "Liushan" setstuage:29 setstuscore:77.5 setstuhobby:@ "sister" setstusex:@ "male"];

[Stu Sayhi];

//

[Stu setstuhobby:@ "Play"];

NSLog (@ "%@", [Stu Stuhobby]);

//

//

Audicar *car =[[audicar alloc] init];

[Car setcolor:@ "Red" setsize:@ "in" setmoney:10];

[Car sayhi];

NSLog (@ "%@,%@,%ld", [car color],[car size], [car money]);

[Car setcolor:@ "Red" setsize:@ "in" setmoney:10];

NSLog (@ "%@,%@,%ld", [car color],[car size], [car money]);

//

Student *stu=[[student alloc]initwithstuname:@ "Handsome" stuage:20 stuscore:90 stusex:@ "male" stuhobby:@ "smug"];

[Stu Sayhi];

//

Audicar *car =[[audicar alloc]initwithcolor:@ "Yellow" size:@ "money:100";

[Car sayhi];

Message mechanism

[What the recipient of the message has to do]

NSLog (@ "%ld", Winter);

NSLog (@ "%ld", Autumn | Winter);

Nsinteger A = ten, B = 20;

A = a | b

NSLog (@ "%ld", a);

return 0;

}

Person.h

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Person:nsobject

Characteristics

{

@public

NSString *_name;

NSString *_sex;

@protected

Nsinteger _age;

@private

NSString *_hobby;

}

The setting device

-(void) SetName: (NSString *) name;

Accessors

-(NSString *) name;

Assignment statements

-(void) SetName: (NSString *) name

Setage: (Nsinteger) age;

Custom initialization methods

@end

Person.m

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Person.h"

@implementation person

The setting device

-(void) SetName: (NSString *) name{

_name =name;

}

Accessors

-(NSString *) name{

return _name;

}

If the declaration is written but not implemented, a warning appears in the. m file telling us that the method should be complete

-(ID) Initwithname: (NSString *) name

Age: (Nsinteger) Age

{

_name =name;

_age =age;

return self;

}

@end

AudiCar.h

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Audicar:nsobject

Three member variables, default visibility

{

NSString *_color;

NSString *_size;

Nsinteger _money;

}

-(void) sayhi;

An assignment statement that assigns a uniform value to three member variables

-(void) SetColor: (NSString *) color

SetSize: (NSString *) size

Setmoney: (Nsinteger) money;

Three set of member variables

-(void) SetColor: (NSString *) color;

-(void) SetSize: (NSString *) size;

-(void) Setmoney: (Nsinteger) money;

Accessors

-(NSString *) color;

-(NSString *) size;

-(Nsinteger) money;

Custom initialization methods

-(ID) Initwithcolor: (NSString *) color

Size: (NSString *) size

Money: (Nsinteger) money;

@end

Audicar.m

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import "AudiCar.h"

@implementation Audicar

-(void) sayhi{

NSLog (@ "%@,%@,%ld", _color, _size, _money);

}

-(void) SetColor: (NSString *) color

SetSize: (NSString *) size

Setmoney: (Nsinteger) money

{

_color =color;

_size =size;

_money=money;

}

The setting device

-(void) SetColor: (NSString *) color{

_color =color;

}

-(void) SetSize: (NSString *) size{

_size =size;

}

-(void) Setmoney: (Nsinteger) money{

_money =money;

}

Accessors

-(NSString *) color{

return _color;

}

-(NSString *) size{

return _size;

}

-(Nsinteger) money{

return _money;

}

-(ID) Initwithcolor: (NSString *) color

Size: (NSString *) size

Money: (Nsinteger) money

{

_color =color;

_size =size;

_money = money;

return self;

}

@end

Student.h

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Student:nsobject

Characteristics

The visibility of member variables

@public level is the lowest, you can find the member variable in the same way as the object, unsafe

If there is no write visibility, the default is protected, and the protected member variable can be used in the current class and its subclasses

@private private: Only the current class can be used, with a smaller scope

@package This is the system some files to use, the system to use, if you see later, jump directly over the line

{

NSString *_stuname;

NSString *_stusex;

NSString *_stuhobby;

CGFloat _stuscore;

Nsinteger _stuage;

}

-(void) sayhi;

-(void) eat;

-(void) play;

Assignment statements for all member variables

As long as there are parameters, you must see the colon, the function of the colon is equal to the indicator parameter

When invoking the method, we know the parameters to be filled by the parameter adjectives, and the star is used when the method name is implemented.

-(void) Setstuname: (NSString *) stuname

Setstuage: (Nsinteger) Stuage

Setstuscore: (cgfloat) Stuscore

Setstuhobby: (NSString *) stuhobby

Setstusex: (NSString *) stusex;

Both the set and accessor are stored and read for a member variable, which is called an assignment statement when multiple member variables are being manipulated.

The setting device

-(void) Setstuname: (NSString *) stuname;

-(void) Setstusex: (NSString *) stusex;

-(void) Setstuhobby: (NSString *) Stuhobby;

-(void) Setstuage: (Nsinteger) stuage;

-(void) Setstuscore: (cgfloat) Stuscore;

Accessors

-(NSString *) stuname;

-(NSString *) stusex;

-(NSString *) Stuhobby;

-(Nsinteger) stuage;

-(cgfloat) Stuscore;

Custom initialization methods

Method must start with INIT, followed by a with,w must be capitalized

-(ID) Initwithstuname: (NSString *) stuname

Stuage: (Nsinteger) Stuage

Stuscore: (cgfloat) Stuscore

Stusex: (NSString *) stusex

Stuhobby: (NSString *) Stuhobby;

@end

Student.m

Oc02_ Visibility, method

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Student.h"

@implementation Student

-(void) sayhi{

NSLog (@ "%@,%@,%@,%g,%ld", _stuname, _stuhobby, _stusex, _stuscore, _stuage);

}

-(void) eat{

NSLog (@ "Can eat");

}

-(void) play{

NSLog (@ "to play");

}

The setting device

-(void) Setstuname: (NSString *) stuname{

_stuname =stuname;

}

-(void) Setstusex: (NSString *) stusex{

_stusex =stusex;

}

-(void) Setstuhobby: (NSString *) stuhobby{

_stuhobby =stuhobby;

}

-(void) Setstuage: (Nsinteger) stuage{

_stuage =stuage;

}

-(void) Setstuscore: (cgfloat) stuscore{

_stuscore =stuscore;

}

Accessors

-(NSString *) stuname{

return _stuname;

}

-(NSString *) stusex{

return _stusex;

}

-(NSString *) stuhobby{

return _stuhobby;

}

-(Nsinteger) stuage{

return _stuage;

}

-(CGFloat) stuscore{

return _stuscore;

}

-(ID) init

{

_stuname [email protected] "Zhang San";

_stuscore = 90;

_stuhobby [email protected] "male";

_stusex [email protected] "female";

_stuage = 23;

return self;

}

-(void) Setstuname: (NSString *) stuname

Setstuage: (Nsinteger) Stuage

Setstuscore: (cgfloat) Stuscore

Setstuhobby: (NSString *) stuhobby

Setstusex: (NSString *) stusex

{

_stuname =stuname;

_stuage = Stuage;

_stuscore = Stuscore;

_stusex = Stusex;

_stuhobby = Stuhobby;

}

-(ID) Initwithstuname: (NSString *) stuname

Stuage: (Nsinteger) Stuage

Stuscore: (cgfloat) Stuscore

Stusex: (NSString *) stusex

Stuhobby: (NSString *) stuhobby

{

_stuage =stuage;

_stuname =stuname;

_stuscore =stuscore;

_stusex =stusex;

_stuhobby =stuhobby;

return self;

}

@end

OC Visibility, method

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.