1.OC第一個工程,類的設計,重構方法,構造函數

1.

 OC:objective-c:面向對象的c

 面向對象:把事務的過程分解到對象,強調對象的行爲;

 面向過程:強調解決問題的步驟,強調業務邏輯;

 把大象關進冰箱:

    1:冰箱門打開

    2:把大象塞進去

    3:把冰箱門關上  ----->強調解決問題的一個過程;面向過程

 

 面向對象:

 冰箱:開門,存儲,關門三個功能;

 

 人類社會是面向對象的;只是確定框架,但是具體行爲是對象個體調用;

 

 

 

 什麼是對象,

 

 :人類,可以看成是數據類型--->具有相同特徵的事務的抽象;

 對象:可以看成是由數據類型創建的變量;例如喬布斯就是對象;強調個體;

 

 

  人類,           同學,

 對象:喬布斯,我家的那隻黑貓,小李子,

 

 

 

 封裝:

 函數:功能性代碼的封裝;

 結構體:數據類型的封裝;

 

 :數據類型+函數;

 狗類:

    數據類型:char name[]; float weight; int age;

    函數(行爲): bark;撒歡;;


#import <Foundation/Foundation.h>
/*
 和include的區別:
 import包含的頭文件不需要條件編譯,保證只導入一次;
 Foundation/Foundation.h 框架主頭文件,包含了常用的數據類型和處理方法;
 */
int main(int argc, const char * argv[])
{
    @autoreleasepool//自動釋放資源池
    {
        // insert code here...
        NSLog(@"Hello, World!");//是一個打印函數;包含執行時間,工程名;會自動換行;
        //@表示對象;
        //@"Hello, World!"表示字符串對象;
        NSLog(@"i am in class %d",1518);
    }
    return 0;//return要在池子外面;
}

2.

/*

 需求:描述狗

 屬性:名字,年齡,重量

 行爲:,,

 */

@interface Dog : NSObject
{
    NSString *_name;//char name[20]   成員變量加 _  和普通的局部變量區分;  _name 字符串對象,定義對象前面加 *
    int _age;
    float _weight;
}
-(void)bark;
@end


//描述類的行爲;
//@implementation <#class#>//類名
//
//<#methods#>//函數方法
//
//@end
/*
 void dogBark()
 {
    printf("won won...")
 }
 
 */
@implementation Dog

-(void)bark//類型加().  -表示對象方法,表示對象來調用;
{
    NSLog(@"won . won...");
}

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

        //Dog是個類,數據類型,定義一個變量
        Dog *xiaohua = [[Dog alloc]init];
        /*
         xiaohua是個對象,是個棧裏的局部變量
         *表示指針標識
         Dog alloc 表示去堆裏面申請 Dog這個類型需要的內存;並清零
         init 表示初始化;調用根類的初始化
         */
        [xiaohua bark];
        /*
         在對象中調用函數:[對象指針 對象方法]
                       [接收者   消息]
         */
    }
    return 0;
}

3.

重構方法;

#import "Dog.h"

@implementation Dog
-(void)bark;
{
    NSLog(@"won.won...");
}
//傳入參數;
// - 表示對象方法; 對象的成員方法是可以直接訪問對象的成員變量;
//成員變量的設置函數 setter  函數遵循駝峯式命名;   set+成員變量名;

//成員變量的獲取函數 getter:一般和成員變量同名,把前面_去掉;
-(NSString *)name
{
    return _name;
}
//setAge
-(void)setAge:(int)newAge
{
    _age = newAge;
}
-(int)age
{
    return _age;
}

-(void)setWeight:(float)newWeight
{
    _weight = newWeight;
}
-(float)weight
{
    return _weight;
}

//函數名 -(void)setName:
-(void)setName:(NSString *)newName
{
    _name = newName;
    //_name 表示成員變量
}
//同時設置名字和年齡
//函數名  -(void)setName: andAge:
// : 前面相當於提示標籤
-(void)setName:(NSString *)newName andAge:(int) newAge
{
    _name = newName;
    _age = newAge;
}
-(void)setName:(NSString *)newName andAge:(int) newAge andWeight:(float) newWeight
{
    _name = newName;
    _age = newAge;
    _weight = newWeight;
}
-(void)show
{
    NSLog(@"name:%@,age:%d,weight:%f",_name,_age,_weight);
}
@end

函數申明;
#import <Foundation/Foundation.h>

@interface Dog : NSObject
{
    NSString *_name;
    int       _age;
    float     _weight;
}
-(void)bark;
-(void)setName:(NSString *)newname;
-(NSString *)name;
-(void)setAge:(int)newAge;
-(int)age;
-(void)setWeight:(float)newWeight;
-(float)weight;
-(void)setName:(NSString *)newName andAge:(int) newAge;
-(void)setName:(NSString *)newName andAge:(int) newAge andWeight:(float) newWeight;
-(void)show;
@end

main函數;
#import <Foundation/Foundation.h>
#import "Dog.h"
//新建類:cmd+n osx->cocoa class -->生成interface,implementation;
int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Dog *xiaohua = [[Dog alloc]init];
        [xiaohua bark];
        [xiaohua setName:@"xiaohua"];
//        [xiaohua name];獲取名字
        NSLog(@"Dog name:%@",[xiaohua name]);
        [xiaohua setAge:1];
        NSLog(@"my age is %d",[xiaohua age]);
        [xiaohua setWeight:8.8];
        NSLog(@"my weight is %f",[xiaohua weight]);
        
        [xiaohua setName:@"wangcai" andAge:3];
        NSLog(@"name:%@,age:%d",[xiaohua name],[xiaohua age]);
        [xiaohua setName:@"mad" andAge:6 andWeight:4.6];
        NSLog(@"name:%@,age:%d,weight:%f",[xiaohua name],[xiaohua age],[xiaohua weight]);
        [xiaohua show];
        
    }
    return 0;
}

4.構造方法

方法實現:
#import "Dog.h"

@implementation Dog
-(id)init//重寫初始化函數(構造函數),不需要申明;是對象方法,對象來調用的時候,只調用重寫的函數;
//id 是個任意對象類型的指針 相當於c中的void *
{
    //self 指向當前對象的指針;
    //super 是父類,表示調用父類的方法;
    //[super init] 表示調用父類的初始化,成功返回非空,失敗返回空;只有調用父類的初始化,才能正確初始化對象;
    if(self = [super init])
    {
        //給成員變量一個默認的值;
        _name = @"一個普通的小狗";
        _age = 1;
        _weight = 1.11;
    }
    return self;//self 的類型是Dog *
}

//構造函數,必須以init開頭用initWithXXXXX
-(id)initWithName:(NSString *)name
{
  if(self = [super init])
  {
      _name = name;
      _age = 1;
      _weight = 1.11;
  }
    return self;
}
-(id)initWithName:(NSString *)name andAge:(int)age
{
    if(self = [super init])
    {
        _name = name;
        _age = age;
        _weight = 1.22;
    }

    return self ;
}

-(id)initWithName:(NSString *)name andAge:(int)age andWeight:(float)weight
{
    if(self = [super init])
    {
        _name = name;
        _age = age;
        _weight = weight;
    }
    
    return self ;
}
-(void)setName:(NSString *)name
{
    _name = name;
}
-(NSString *)name
{
    return _name;
}

-(void)show
{
    NSLog(@"name:%@,age:%d,weight:%f",_name,_age,_weight);
}

//+表示類方法,類才能調用類方法;
+(void)test
{
    NSLog(@"類方法");
    Dog *xiaobai = [[Dog alloc]initWithName:@"xiaobai" andAge:2 andWeight:5.7];
    [xiaobai show];
    
}
@end

函數申明:
#import <Foundation/Foundation.h>

@interface Dog : NSObject
{
    NSString *_name;
    int _age;
    float _weight;
}
-(id)initWithName:(NSString *)name;
-(id)initWithName:(NSString *)name andAge:(int)age;
-(id)initWithName:(NSString *)name andAge:(int)age andWeight:(float)weight;

-(void)setName:(NSString *)name;
-(NSString *)name;
-(void)show;

+(void)test;
@end

main函數:
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        //[Dog alloc]結果是個Dog *
//        Dog *wangcai = [[Dog alloc]init];
        Dog *wangcai = [Dog alloc];
        wangcai = [wangcai init];
        /*
         init NSObject 也有提供這個接口
         如果我們自己沒有實現這個接口,就調用NSObject的init接口;
         */
        [wangcai show];
        [wangcai setName:@"哮天犬"];
//        NSLog(@"name:%@",[wangcai name]);
        [wangcai show];
        
        Dog *snoopy = [[Dog alloc]initWithName:@"snoopy"];
        [snoopy show];
        Dog *bug = [[Dog alloc]initWithName:@"bug" andAge:5];
        [bug show];
        Dog *snow = [[Dog alloc]initWithName:@"snow" andAge:3 andWeight:4.6];
        //alloc 是申請空間;init 是初始化,原則上是隻能調用一次;
        [snow show];

        
        Dog *xiaoq = [Dog new];//[[Dog alloc]init]  不能調用定製的帶參的構造函數
        [xiaoq show];
        
        
        //類方法[類 方法]
        [Dog test];
        
    }
    return 0;
}


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