類和對象

/*編程

      面向過程編程思想: 以事件爲中心,關心的是解決問題的步驟,實現函數依次調用 (一步一步)iphone

      面向對象編程思想(OOP): 以事物爲中心,關心的是參與問題的對象有哪些,而完成這些問題只是對象全部功能中的一個小功能函數

 */對象

 

類的接口部分: 以@interface 開頭 以@end結尾繼承

@interface + 類的名字 :(表示繼承) + 父類名接口

在@interface 和 @end 中間 是類實例變量 和 方法 的聲明事件

只要符合這種形式 就能夠定義一個類的接口部分ip

@interface Person : NSObjectit

//類的靜態特徵(實例變量)io

{

    //NSString  這是一個類,要加*

    //NSInteger 基本數據類型,不用加*

    NSString *_name;  //姓名

    NSString *_sex;   //性別

    NSInteger _age;   //年齡

    CGFloat _weight;  //體重

}

//行爲(方法)

//void sayHi();

- (void)sayHi; //- (返回值)方法名

 

- (void)eat;

 

- (void)walk;

@end

//類的實現部分: @implementation 開頭 以@end結尾

//@implementation + 類名

//@implementation 和 @end中間 寫,類方法的實現

//只要符合這種形式 就能夠完成類的實現部分

@implementation Person

-(void)sayHi

{

    NSLog(@"你好!");

}

 

- (void)eat

{

    NSLog(@"吃飯!");

}

 

-(void)walk

{

    NSLog(@"走路!");

}

@end

 

        //建立Person 對象

        Person *heXin = [[Person alloc] init];

        NSLog(@"==%p==",heXin);

        [heXin sayHi];

        [heXin eat];

        [heXin walk];

        

        

        

        

        Phone *iphone = [[Phone alloc] init];

        [iphone calling];

        

        //建立Dog 對象

        Dog *jinMao = [[Dog alloc] init];

        //調用方法

        [jinMao run];

//        [jinMao wangWang];

        //[recevier message];消息發送機制

        

        

        //建立Cat 對象

        Cat *boShi = [[Cat alloc] init];

        //調用方法

        [boShi run];

相關文章
相關標籤/搜索