oc 学习

来源:互联网 发布:2016年nba季后赛数据 编辑:程序博客网 时间:2024/06/06 07:17
#import<Foundation/Foundation.h>
#include<studio.h>
int main(int argc, char *argv[])
{
    NSLog(@"hello the Cruel world!");
    printf("hello world\n");
    return 0;
}


clang -framework Foundation first.m -o app
./app

2.
使用NSDate打印系统时间
类方法alloc
实例方法init,dealloc
#import<Foundation/Foundation.h>
#include<studio.h>
int main(int argc, char *argv[])
{
    NSDate *now = [NSDate alloc];
    now = [now init]
    NSLog(@"current time is %@", now);
    NSLog(@"hello the Cruel world!");
    printf("hello world\n");
    [now dealloc];
    return 0;
}

#import<Foundation/Foundation.h>
#include<studio.h>
int main(int argc, char *argv[])
{
    char * s1="jeeedu.com";
    char * s2="jeeedu.com";
    if (0 == strcmp(s1,s2))
        printf("equal\n");
    else
        printf("not equal\n");
    NSString *c1= @"hello Jeapedu.com";
    NSString *c2= @"hello Jeapedu.com";
    if (c1 isEqual c2)
        printf("equal\n");
    else
        printf("not equal\n");
    return 0;
}

#import<Foundation/Foundation.h>
#include<studio.h>
@interface Horse : NSObject
{
    float weight;
    NSString *color;
    float hegiht;
}
- (void) run;
- (void) carry;
@end
@implementation Horse
{
    float price;
    
}
- (void) run
{
    NSLog("a horse is running")
}
- (void) cray
{
    NSLog("a horse is cray")
}
@end
int main(int argc, char *argv[])
{
    Horse * ahorse = [Horse alloc];
    ahorse = [ahosrs init];
    [ahorse run];
    ahorse cray];
    return 0;
}

cat.h
#import <Foundation/Foundation.h>

@interface Cat : NSObject
{
    NSString * catname;
}
+(void) descript;
-(void) mew : (int) times;
-(void) eat :(float) weight;
@end

cat.m
#import <Foundation/Foundation.h>
#import "cat.h"
@interface Cat
{
    float price;
}
+(void) descript;
{
    NSLog(@"class cat");
}
-(void) mew : (int) times
{
    NSLog(@"class mew %d times", times);
}
-(void) eat :(float) weight
{
    NSLog(@"class cat %f", weight);
}
@end

clang -c cat.m

#import<Foundation/Foundation.h>
#import "cat.h"
int main(int argc, char *argv[])
{
    Cat * puppy = [Cat alloc];
    puppy = [puppy init];
    [Cat descript];
    [puppy mew : 3];
    [pup eat : 23.5];
    NSLog(@"hello the Cruel world!");
    printf("hello world\n");
    return 0;
}

clang -framework Foundation -o app fourth.m cat.m

@property (assign) int z;
0 0