您好,登錄后才能下訂單哦!
一、實(shí)例變量可見度
可見度 特點(diǎn)
public 實(shí)例變量可以在類的外部和內(nèi)部操作
protected 實(shí)例變量只能在該類和其子類內(nèi)操作 默認(rèn)
private 實(shí)例變量只能在該類內(nèi)訪問
內(nèi)部:相應(yīng)類的@implementation和@end之間
@interface Person : NSObject
{
@public
NSString*_name;
@protected
NSString*_gender;
int_age; //年齡
@private
NSString*_age;
}
mian.m文件中使用
Person * p = [[Person alloc] init];
p->_name = @"zhangyixing";
// p->_gender = @'m'; // 警告
// p->_age = 18; // 警告
為什么不使用@public 暴露細(xì)節(jié) 不符合封裝
在其他文件訪問@protected修飾的實(shí)例變量-----方法
提示:添加兩個(gè)方法。
賦值:通過方法傳入?yún)?shù),在類的內(nèi)部實(shí)現(xiàn)實(shí)例變量賦值。
取值:通過方法獲取實(shí)例變量的值。
二、方法--方法類型、返回值類型、方法名、參數(shù)列表
OC中的方法------類方法、實(shí)例方法
同一個(gè)中,所有的類方法中不能出現(xiàn)重名 所有的實(shí)例方法中不能出現(xiàn)重名
1、 類方法:只能類使用 [類名 類方法名]
在類方法中不能使用實(shí)例變量 調(diào)用類方法時(shí)沒有創(chuàng)建對象
聲明-----Person.h
+ (void)sayHi;
實(shí)現(xiàn)-----Person.m
+ (void)sayHi
{
NSLog(@"hi");
}
2、實(shí)例方法:只能對象使用 [對象 實(shí)例方法名]
聲明-----Person.h
- (void)sayHello;
實(shí)現(xiàn)-----Person.m
- (void)sayHello
{
NSLog(@"hello");
}
3、聲明方法 (返回值類型)沒有參數(shù)只寫方法名
(1)無返回值、無參數(shù)
- (void)sleep;
實(shí)現(xiàn)
- (void)sleep
{
NSLog(@"sleep");
}
使用
[p sleep];
(2)無返回值、有參數(shù)
語法--- :(參數(shù)類型)參數(shù)名
參數(shù)以:開頭 ":"標(biāo)識(shí)參數(shù),不能省略。有冒號必須有參數(shù)
///// 一個(gè)參數(shù)
- (void)eat:(NSString *)food;
實(shí)現(xiàn)
- (void)eat:(NSString *)food
{
NSLog(@"%@",food);
}
使用
[p eat:@"beef"];
///// 兩個(gè)參數(shù)
- (void)setName:(NSString *)name age:(NSInteger)age; // 賦值
實(shí)現(xiàn)
- (void)setName:(NSString *)name age:(NSInteger)age
{
_name = name;
_age = age;
// name和age是方法的形參,接收從外部傳入的值
}
使用
[p setName:@"zhangyixing" age:18];
(3) 有返回值、無參數(shù)
- (NSString *)getName; // 取值
實(shí)現(xiàn)
- (NSString *)getName
{
return _name;
}
使用
NSLog(@"name=%@",[p getName]);
// 方法名使用駝峰命名法---方法名: setName:age:
首字母小寫;第一個(gè)參數(shù)修飾符首字母大寫;
第二個(gè)首字母小寫;都使用駝峰命名法
4、方法使用----[person sayHi];
在OC中沒有”student調(diào)用sayHi“這種表述。
在OC中使用消息發(fā)送機(jī)制:[receiver message]。
正確表述:給student對象發(fā)送sayHi消息。
1)student接收到消息,即方法sayHi;
2)student找到sayHi方法,并執(zhí)行。
三、setter、getter
1、簡述
在OC里,為實(shí)例變量賦值的方法稱作setter(設(shè)置器)。
讀取實(shí)例變量值的方法稱作getter(訪問器)。
我們之前寫的那些賦值取值的方法都可以稱作setter和getter。
2、實(shí)例
(1)設(shè)置_name實(shí)例變量的值
// setter方法是實(shí)例方法,沒有返回值,只有一個(gè)參數(shù)
// 方法名 set + 實(shí)例變量名(忽略_,使用駝峰命名法)
// 參數(shù) 跟操作的實(shí)例變量有關(guān) 類型跟操作的實(shí)例變量一致 實(shí)例變量名(忽略_)
- (void)setName:(NSString *)name;
實(shí)現(xiàn)
- (void)setName:(NSString *)name
{
_name = name;
// name是方法的形參,接收從外部傳入的值 _name實(shí)例變量
}
(2)訪問實(shí)例變量
// getter是實(shí)例方法 沒有參數(shù) 返回值類型與操作的實(shí)例變量保持一致
// 方法名 實(shí)例變量名(忽略_) 切記:不要以get開頭
- (NSString *)name;
實(shí)現(xiàn)
- (NSString *)name
{
return _name;
}
(3)使用
// 使用setter方法給Person對象的實(shí)例變量_name賦值
[p setName:@"zhang"];
// 使用getter方法讀取實(shí)例變量的值
NSLog(@"name = %@", [p name]);
3、與實(shí)例變量的關(guān)系
無論setter還是getter內(nèi)部操作的是實(shí)例變量
每個(gè)實(shí)例變量都需要一對setter和getter方法
4、main.m文件中使用
// 使用setter方法給Person對象的實(shí)例變量_name 和_age賦值
[p setName:@"zhang"];
[p setAge:23];
// 使用getter方法讀取實(shí)例變量的值
NSLog(@"name = %@, age = %ld", [p name], [p age]);
四、自定義初始化方法
-(id)init這個(gè)初始化方法只能給實(shí)例變量設(shè)置默認(rèn)值,不靈活
- (id)init
{
return self;
}
-(id)initWithName:自定義初始化法,根據(jù)需求定義初始化方法
// 自定義初始化方法 初始化對象時(shí)給_name和_age賦值
- (id)initWithname:(NSString *)name age:(NSInteger)age;
實(shí)現(xiàn)
- (id)initWithname:(NSString *)name age:(NSInteger)age
{
// 對象初始化時(shí)可以傳入姓名和年齡 將傳入的值賦值給實(shí)例變量
_name = name;
_age = age;
return self;
}
main.m文件中使用
Person * p3 = [[Person alloc] initWithname:@"zhangyixing" age:18];
Person * p4 = [[Person alloc] initWithname:@"liyifeng" age:20];
NSLog(@"name=%@",[p3 name]);
[p4 setName:@"liminhao"];
NSLog(@"name=%@",[p4 name]);
五、導(dǎo)入文件
1、#import導(dǎo)入頭件,即:導(dǎo)入頭件中的內(nèi)容到當(dāng)前類。
#import “”導(dǎo)入自定義類,自己創(chuàng)建的文件 從當(dāng)前工程目錄開始查詢
#import <> 導(dǎo)入類庫中的頭文件,自己創(chuàng)建的文件 從當(dāng)前工程目錄開始查詢
功能類似C語言中的#include,但是可以避免頭文件被重復(fù)導(dǎo)入。
容易出現(xiàn)循環(huán)導(dǎo)入頭文件問題。
2、#include 和 #import的區(qū)別
(1)include不能重復(fù)導(dǎo)入同一個(gè)文件
(2)import可以重復(fù)導(dǎo)入同一個(gè)文件
3、@class
#import可能會(huì)造成循環(huán)導(dǎo)入頭文件問題,需要使用@class解決
@class的作用,告知編譯器系統(tǒng)class后面的標(biāo)識(shí)符可以作為一個(gè)類名使用;并沒有導(dǎo)入這個(gè)類的內(nèi)容
使用@class可以解決循環(huán)導(dǎo)入頭文件的問題。
如果需要在類的接口部分使用其他類型:
1)在.h文件中,使用@class聲明需要使用的類型;
2)在.m文件中,使用#import導(dǎo)入需要使用的類型
實(shí)例
創(chuàng)建Man、Woman、Child三個(gè)類并完成聲明與實(shí)現(xiàn)部分
新創(chuàng)建Family類
Family.h文件
#import <Foundation/Foundation.h>
// 使用@class“導(dǎo)入”文件
@class Man;
@class Woman;
@class Child;
@interface Family : NSObject
// 定義實(shí)例變量
{
Man * _father;
Woman * _mother;
Child * _kid;
}
// 初始化方法設(shè)置father和mother
- (id)initWithFather:(Man *)father mother:(Woman *)mother;
// 聲明setter、getter方法
- (void)setFather:(Man *)father;
- (Man *)father;
- (void)setMother:(Woman *)mother;
- (Woman *)mother;
- (void)setKid:(Child *)kid;
- (Child *)kid;
// 聲明一般方法
- (void)introduce;
Family.m文件
#import "Family.h"
導(dǎo)入文件
#import "Man.h"
#import "Woman.h"
#import "Child.h"
@implementation Family
// 初始化方法設(shè)置father和mother
- (id)initWithFather:(Man *)father mother:(Woman *)mother
{
_father = father;
_mother = mother;
return self;
}
// 實(shí)現(xiàn)setter、getter方法
- (void)setFather:(Man *)father
{
_father = father;
}
- (Man *)father
{
return _father;
}
- (void)setMother:(Woman *)mother
{
_mother = mother;
}
- (Woman *)mother
{
return _mother;
}
- (void)setKid:(Child *)kid
{
_kid = kid;
}
- (Child *)kid
{
return _kid;
}
// 實(shí)現(xiàn)一般方法
- (void)introduce
{
NSLog(@"father:%@,mother:%ld,kid:%@", [_father name], [_mother age], [_kid gender]);
}
main.m文件
#import <Foundation/Foundation.h>
// 導(dǎo)入文件
#import "Man.h"
#import "Woman.h"
#import "Child.h"
#import "Family.h"
Man * m = [[Man alloc] initWithName:@"zhangjiashuai"];
Woman * w = [[Woman alloc] initWithName:@"zhangjiamei" age:18];
Family * f = [[Family alloc] initWithFather:m mother:w];
Child * c = [[Child alloc] initWithName:@"zhangyi" gender:@"m"];
[f setKid:c];
[f introduce];
六、總結(jié)
1、實(shí)例變量有3種常見的可見度:@public、@protected、@private。
@public違背了封裝特性,面向?qū)ο箝_發(fā)中很少使用;
@protected默認(rèn)可見度,自己和子類中能使用—>訪問實(shí)例變量;
@private自己類中能使用—>訪問實(shí)例變量。
2、方法是OC的核心,采用消息機(jī)制:[receiver message]。
“-”message由對象來調(diào)用;
“+”message由類來調(diào)用。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。