Objective-c学习(Xcode)

//  BOOL Party.m

//  20180703

//objective-c基础教程

//  Created by yjcao-imac on 2018/7/3.

//  Copyright © 2018年 yjcao-imac. All rights reserved.



#import <Foundation/Foundation.h>


BOOL areIntDifferent (int things1, int things2)

{

    if(things1==things2)

    {

        return (NO);

    }else {

        return (YES);

    }

}


 NSString *boolString (BOOL yesNo)

{

    if(yesNo == NO)

    {

        return(@"NO");

    }else

    {

        return (@"YES");

    }

}


 int main(int argc ,const char *argv[])

{

    BOOL areTheyDifferent;

    areTheyDifferent = areIntDifferent(5,5);

    NSLog(@"are %d and %d different?%@",5,5,boolString(areTheyDifferent));

    

    areTheyDifferent = areIntDifferent(23,42);

    NSLog(@"are %d and %d different?%@",23,42,boolString(areTheyDifferent));

    return (0);

}



3.1.1 间接与变量

Count-1


#import <Foundation/Foundation.h>

int main( int argc ,const char *argv[])

{

    NSLog(@"The numbers from 1 to 5:");

    

    int i;

    for (i=1;i<=5;i++){

        NSLog (@"%d\n",i);

    }

    return (0);

}



#import <Foundation/Foundation.h>

int main( int argc ,const char *argv[])

{

    // NSLog(@"The numbers from 1 to 5:");

    

    int i;

    for (i=1;i<=5;i++){

        NSLog (@"The numbers from 1 to 5:%d\n",i);

    }

    return (0);

}

3.1.2使用文件名的间接

#import <Foundation/Foundation.h>

int main (int argc,const char * argv[])

{

    const char *words[4] ={"dsddas","dad","dadddw","ddawww"};

    int wordCount =4;

    int i;

    for (i=0;i<wordCount;i++){

        NSLog(@"%s is %d characters long",words[i],strlen(words[i]));

    }

    return(0);

}



#import <Foundation/Foundation.h>

int main (int argc,const char * argv[])

{

    FILE *wordFile =fopen("/Users/yjcao-imac/Desktop/words.txt","r");

    char word[100];

    while(fgets(word,100,wordFile)){

        word[strlen(word)- 1]='\0';

        NSLog(@"%s is %d characters long",word,strlen(word));

    }

    fclose(wordFile);

    return (0);

}

3.2.1过程式编程

#import <Foundation/Foundation.h>

typedef enum{//枚举

    kCircle,

    kRectangle,

    kOblateSpheroid

}ShapeType;


typedef enum{

    kRedColor,

    kGreenColor,

    kBlueColor

}ShapeColor;

//对绘制形状以及颜色进行定义


typedef struct{//结构体

    int x,y,width,height;

}ShapeRect;

//对结构进行定义


typedef struct {

    ShapeType type;

    ShapeColor fillColor;

    ShapeRect bounds;//界限

}Shape;

//对所有结构体定义

//声明函数体

void drawCirele(ShapeRect bounds,ShapeColor fillColor);

void drawRectangle(ShapeRect bounds,ShapeColor fillColor);

void drawOblateSpheroid(ShapeRect bounds,ShapeColor fillColor);

void drawShapes(Shape shapes[], int count);

NSString *colorName(ShapeColor colorName);


NSString *colorName(ShapeColor colorName){

    switch (colorName){

        case kRedColor:

            return @"red";

            break;

            

        case kBlueColor:

            return  @"blue";

            break;

        case  kGreenColor:

            return @"green";

            break;

        default:

            break;

    }

    return @"no color";

}

//输出有边框和传递给它的颜色

void drawCircle(ShapeRect bounds,ShapeColor fillColor){

    NSLog(@"drawing a cirecle at (%d %d %d %d)in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));

}//圆

void drawRectangle(ShapeRect bounds,ShapeColor fillColor){

    NSLog(@"drawing a Rectangle at (%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));

}//长方形


void drawOblateSpheroid(ShapeRect bounds,ShapeColor fillColor){

    NSLog(@"drawing a OblateSpheroid at (%d %d %d %d) in %@" ,bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));

}//椭圆



void drawShapes(Shape shapes[],int count){

    int i;

    for(i=0;i<count;i++){

        switch(shapes[i].type){

            case kCircle:

                drawCircle(shapes[i].bounds,shapes[i].fillColor);

                break;

            case kRectangle:

                drawRectangle(shapes[i].bounds,shapes[i].fillColor );

                break;

            case kOblateSpheroid:

                drawOblateSpheroid(shapes[i].bounds, shapes[i].fillColor);

                break;

            default:

                break;

        }

    }

}


int main(int argc ,const char *argv[])

{

    Shape shapes[3];

    

    ShapeRect rect0 ={0,0,10,30};

    shapes[0].type=kCircle;

    shapes[0].fillColor=kRedColor;

    shapes[0].bounds=rect0;

    

    ShapeRect rect1={30,40,50,60};

    shapes[1].type=kRectangle;

    shapes[1].fillColor=kGreenColor ;

    shapes[1].bounds=rect1;

    

    ShapeRect rect2={15,18,37,29};

    shapes[2].type=kOblateSpheroid;

    shapes[2].fillColor=kBlueColor;

    shapes[2].bounds=rect2;

    

    drawShapes(shapes,3);

    return (0);

}

3.4 Object-c语言中的OOP

//  main.m

//  Carparts

//

//  Created by yjcao-imac on 2018/7/5.

//  Copyright © 2018年 yjcao-imac. All rights reserved.

//

//@intesface 定义类的公共接口API

//@implementation 实际运行的内容代码

#import <Foundation/Foundation.h>


@interface Tire :NSObject

@end//Tire


@implementation Tire

-(NSString *) description{

    return (@" I am a tire.");

}//description

@end //Tire


@interface Engine : NSObject

@end//engine


@implementation Engine


-(NSString *) description{

    return (@"I am a Engine");

}//engine

@end


@interface Car: NSObject

{

    Engine *engine;

    Tire * tires[4];

}


-(Engine *) engine;

-(void) setEngine: (Engine *) newEngine;

-(Tire *)tireAtIndex: (int) index;

-(void) setTire: (Tire *) tire atIndex: (int) index;

-(void) print;

@end//Car


@implementation Car

  -(id) init

{

    if(self =[super init]){

        engine = [Engine new];

        tires[0]=[Tire new];

        tires[1]=[Tire new];

        tires[2]=[Tire new];

        tires[3]=[Tire new];

    }

    return (self);

}


-(void) print{

    NSLog(@"%@",engine);

    NSLog(@"%@",tires[0]);

    NSLog(@"%@",tires[1]);

    NSLog(@"%@",tires[2]);

    NSLog(@"%@",tires[3]);

}


-(Engine *) engine;

{

    return (engine);

}//read

-(void) setEngine: (Engine *) newengine;

{

    engine =newengine;

}//setengine write



-(Tire *)tireAtIndex:(int) index

{

    if(index<0 || index>3)

    {

        NSLog(@"bad index (%d) in tireAtIndex:",index);

        exit(1);

    }

    return (tires[index]);

}//tireAtIndex

-(void) setTire: (Tire *) tire atIndex:(int) index

{

    if(index<0||index>3)

    {

        NSLog(@"bad index(%d) in setTire:atindex:",index);

        exit(1);

    }

    tires[index]=tire;

}

@end

int main( int argc ,const char*argv[])

{   int count=4;

    Car *car;

    car = [Car new];

    Engine *engine =[Engine new];

    [car setEngine:engine];

    

    for(int i=0;i<count;i++){

        Tire *tire =[Tire new];

        [car setTire: tire atIndex: i];

    }

    [car print];

    

    return (0);

}//mai


5、 继承与复合

#import <Foundation/Foundation.h>


@interface Tire :NSObject

@end//Tire


@implementation Tire

-(NSString *) description{

    return (@" I am a tire.");

}//description

@end //Tire


@interface Engine : NSObject

@end//engine


@implementation Engine


-(NSString *) description{

    return (@"I am a Engine");

}//engine

@end


@interface Car: NSObject

{

    Engine *engine;

    Tire * tires[4];

}


-(Engine *) engine;

-(void) setEngine: (Engine *) newEngine;

-(Tire *)tireAtIndex: (int) index;

-(void) setTire: (Tire *) tire atIndex: (int) index;

-(void) print;

@end//Car


@implementation Car

  -(id) init

{

    if(self =[super init]){

        engine = [Engine new];

        tires[0]=[Tire new];

        tires[1]=[Tire new];

        tires[2]=[Tire new];

        tires[3]=[Tire new];

    }

    return (self);

}


-(void) print{

    NSLog(@"%@",engine);

    NSLog(@"%@",tires[0]);

    NSLog(@"%@",tires[1]);

    NSLog(@"%@",tires[2]);

    NSLog(@"%@",tires[3]);

}


-(Engine *) engine;

{

    return (engine);

}//read

-(void) setEngine: (Engine *) newengine;

{

    engine =newengine;

}//setengine write



-(Tire *)tireAtIndex:(int) index

{

    if(index<0 || index>3)

    {

        NSLog(@"bad index (%d) in tireAtIndex:",index);

        exit(1);

    }

    return (tires[index]);

}//tireAtIndex

-(void) setTire: (Tire *) tire atIndex:(int) index

{

    if(index<0||index>3)

    {

        NSLog(@"bad index(%d) in setTire:atindex:",index);

        exit(1);

    }

    tires[index]=tire;

}

@end


@interface Slan:Engine

@end

@implementation Slan

-(NSString *) description

{

    return(@"I am Slan");

}//Slan

@end


@interface AllweatherRadial:Tire

@end


@implementation AllweatherRadial

-(NSString *) description

{

    return(@"I am allweatherRadial tire");

}//All tire

@end


int main( int argc ,const char*argv[])

{   int count=4;

    Car *car;

    car = [Car new];

  //Engine *engine =[Engine new];

    Engine *engine =[Slan new];

    [car setEngine:engine];

    

    for(int i=0;i<count;i++){

    //  Tire *tire =[Tire new];

        Tire *tire =[AllweatherRadial new];

        [car setTire: tire atIndex: i];

    }

    [car print];

    

    return (0);

}//main


猜你喜欢

转载自blog.csdn.net/qiceng/article/details/80911117