OC中類的使用注意事項

1 類方法的定義格式:+(返回值類型)方法名:(形參類型)形參名;調用類方法的格式:

[類名 方法名];使用類方法可以節省內存空間,提高效率。

2 定義類方法時,需注意以下事項:

<1>類方法可以與對象方法重名;

<2>類方法可以從父類中繼承,讓後在子類中重寫該方法;

<3>類方法和對象方法定義及實現方式相同;

<4>類方法中不能使用類中的成員變量;

<5>在類方法中可以調用其他類的方法,可以用self調用自身類中其他的類方法;

3 匿名對象的概念及使用

<1>使用匿名對象訪問對象變量時,只能訪問一次;

<2>使用匿名對象可以調用對象方法;

4 封裝的概念及實現步驟

<1>類中的實例變量默認只能被類的對象來訪問,通過對象的方法來設置對象的成員變量,這樣就能保證實例變量的安全;

<2>設置和訪問實例變量的方法分別是:setter和getter;

set方法命名規範:

(1)以set開頭;

(2)set後緊跟成員變量名,成員變量名的首字母大寫;

(3)返回值是void類型;

(4)參數類型與成員變量的類型一致;

(5)形參名是去掉下劃線的成員變量名;

get方法的命名規範:

(1)返回值類型和成員變量類型一致;

5 OC中類在定義時,A對象作爲B對象的局部變量或是方法參數的,稱爲B依賴於A,對象A和對象B稱爲依賴關係。

當一個類的定義中擁有另一個對象的時候,稱爲關聯關係。

6 OC中類方法定義時,不存在重載。

關於類是用注意事項的實例如下:

#import <Foundation/Foundation.h>

@interface Car : NSObject
{
    int _speed;
}
-(void)run;
@end
#import "Car.h"

@implementation Car
-(void)run{
    NSLog(@"車以%d的速度飛奔。。。",_speed);
}
@end
#import <Foundation/Foundation.h>
#import "Car.h"
@interface Person : NSObject
+(void)goHome:(Car *) car;
@end
#import "Person.h"

@implementation Person
+(void)goHome:(Car *) car{
    [car run];
}
@end
//  匿名類調用方法及作爲方法參數


#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [Person goHome:[Car new]];
    }
    return 0;
}

#import <Foundation/Foundation.h>
//定義並實現K線類
@interface KLine : NSObject
//K線的最高、最低、平均價格三種屬性
{
    float _minPrice;
    float _maxPrice;
    float _avePrice;
}
-(void)setMinPrice:(float)minPrice;//設置最低價格
-(float)minPrice;//獲取最低價格
-(void)setMaxPrice:(float)maxPrice;//設置最高價格
-(float)maxPrice;//獲取最高價格
-(float)avePrice;//獲取平均價格
@end
@implementation KLine

-(void)setMinPrice:(float)minPrice{
    _minPrice = minPrice;
    _avePrice = (_minPrice + _maxPrice)/2;
}//設置最低價格
-(float)minPrice{
    return _minPrice;
}//獲取最低價格
-(void)setMaxPrice:(float)maxPrice{
    _maxPrice = maxPrice;
    _avePrice = (_minPrice + _maxPrice)/2;
}//設置最高價格
-(float)maxPrice{
    return _maxPrice;
}//獲取最高價格
-(float)avePrice{
    return _avePrice;
}//獲取平均價格
@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        KLine *k = [KLine new];
        [k setMinPrice:20];
        float result = [k avePrice];
        NSLog(@"K線平均值爲:%.2f",result);
    }
    return 0;
}

#import <Foundation/Foundation.h>

@interface IPad : NSObject
{
    NSString *_name;
}
-(void)setName:(NSString *) name;//設置Ipad名字
-(void)playMusic;//IPad聽音樂
@end
#import "IPad.h"

@implementation IPad
-(void)setName:(NSString *) name{
    _name = name;
}//設置Ipad名字
-(void)playMusic{
    NSLog(@"%@正在播放音樂。。。",_name);
}//IPad聽音樂
@end
#import <Foundation/Foundation.h>
#import "IPad.h"
@interface Person : NSObject
{
    NSString *_name;
    IPad *_ipad;
}
-(void)setName:(NSString *)name;//設置姓名
-(void)setIPad:(IPad *)iPad;//設置IPAD
-(void)listenMusic;//聽音樂
@end
#import "Person.h"

@implementation Person
-(void)setName:(NSString *)name{
    _name = name;
}//設置姓名
-(void)setIPad:(IPad *)iPad{
    _ipad = iPad;
}//設置IPAD
-(void)listenMusic{
    [_ipad playMusic];
}//聽音樂
@end
//  關聯關係

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        IPad *p = [IPad new];
        [p setName:@"IPad5"];
        Person *person = [Person new];
        [person setIPad:p];
        [person listenMusic];
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章