OC學習第一天

還記得敲C代碼那是幾年前的事情,最近突發奇想一想學習一下IOS,因而找了一些網上資料來學習(主要參考該文http://www.cnblogs.com/mjios/archive/2013/04/06/3002814.html ),看了看,對OC的第一感受是太奇葩了,不想平時的那些語法同樣。還好網上資料比較細緻、齊全,說說今天學習走過的彎路(就是中括號的使用範圍,在變量聲明、方法聲明、方法定義中有無中括號):html

#import <Foundation/Foundation.h>
#import "Student2.h"
(頭文件,聲明變量以及方法)
@interface Student : NSObject
{//變量的聲明必定要在這個中括號內部
@public;
    int age;
    int no;
    int score;
    NSString *name;
    int Chinese;
    int Englist;
}
//方法的聲明則在中括號外側
-(int)sum;
-(int)sum:(int) Chinese andEnglish:(int)English;
@end

(實體文件,包含方法的實體)
@implementation Student
//此處無需中括號包含
    -(int)sum{
        return Chinese+Englist;
    }

    -(int)sum:(int) Chinese andEnglish:(int)English{
        return Chinese + Englist;
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *stu = [[Student alloc] init];
        stu->no = 10;
        stu->name=@"小明";
        NSLog(@"學號=%d,姓名=%@",stu->no,stu->name);
        
        stu->Englist = 23;
        stu->Chinese = 88;
        int sum = [stu sum];
        NSLog(@"總成績=%d",sum);
        
        int score = [stu sum:100 andEnglish:99];
        NSLog(@"總成績=%d",score);
        
        Student2 *stu2 = [[Student2 alloc] init];
        stu2->age = 20;
        NSLog(@"Student2 年齡=%d",stu2->age);
        
    }
    return 0;
}
相關文章
相關標籤/搜索