走上探索Objective-C废墟之路

一直转移动开发,可是没有成功。最近下重本买了rMBP,决心踏上ios开发之路。本来是想跳过oc,直接swift,但是走了一段路,发现还是安心学oc吧。 

有java基础之后,oc学习还是比较容易的,我计划三个周开发一个小程序上线,哇嘎嘎。 

对xCode还是相当不爽,对intellij idea情有独钟的我,马上装上了AppCode,上第一天的成果。 

一共三个类。main.m,Student.h,Student.m。 

Student.h是头文件……这个概念java没有,只有在远古的c中依稀见过……

#import <Foundation/Foundation.h>

@interface Student : NSObject
@property int age;
@property NSString *name;
@property int score;
-(BOOL) isGood;
-(BOOL) isGoodThen: (Student *) anther;
+(instancetype) initWithName: (NSString *) iname withAge: (int) iage withScore: (int) iscore;
@end

Student.m是Student.h的实现类。从java的角度来看,Student.h就是多余的……

#import "Student.h"

@implementation Student
@synthesize name,score,age;

- (BOOL)isGood {
    if(score >= 80){
        return YES;
    }
    return NO;
}

- (BOOL)isGoodThen:(Student *)anther {
    if(score > anther.score){
        return YES;
    }
    return NO;
}

+ (instancetype)initWithName:(NSString *)iname withAge:(int)iage withScore:(int)iscore {
    Student *stu = [Student new];
    stu.score = iscore;
    stu.name = iname;
    stu.age = iage;
    return stu;
}
@end

main.m是主方法。

#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *sa = [Student initWithName:@"张翠" withAge:12 withScore:99];
        Student *sb = [Student initWithName:@"贺大" withAge:14 withScore:88];
        Student *sc = [Student initWithName:@"二月河" withAge:13 withScore:55];
        if([sc isGood]){
            NSLog(@"我是合格的%@%i分",sc.name, sc.score);
        }else{
            NSLog(@"需要努力%@%i分",sc.name, sc.score);
        }

        if([sa isGoodThen:sb]){
            NSLog(@"%@%i分比%@%i分好", sa.name,sa.score,sb.name,sb.score);
        }else{
            NSLog(@"%@%i分比%@%i分好", sb.name,sb.score,sa.name,sa.score);
        }
    }

    return 0;
}

你可能感兴趣的:(走上探索Objective-C废墟之路)