//
// main.m
// yhj
//
// Created by yinhaijing on 2022/7/20.
//
#import <Foundation/Foundation.h>
#pragma mark -复合
@interface Engine : NSObject
@end
@implementation Engine
- (NSString *) description
{
return (@"i am an engine");
}//description
@end//Engine
@interface Tire :NSObject
@end
@implementation Tire
-(NSString *) description
{
return (@"i am a tire.i last a while.");
}//description
@end//Tire
@interface Car:NSObject
{
Engine *engine;
Tire *tire[4];
}
- (void) print;
@end
@implementation Car
- (id) init
{
if(self=[super init])
{
engine = [Engine new];
tire[0]=[Tire new];
tire[1]=[Tire new];
tire[2]=[Tire new];
tire[3]=[Tire new];
}
return self;
}
- (void) print
{
NSLog(@"%@",engine);
NSLog(@"%@",tire[0]);
NSLog(@"%@",tire[1]);
NSLog(@"%@",tire[2]);
NSLog(@"%@",tire[3]);
}//print
@end//Car
int main(int argc, const char * argv[]) {
Car *car;
car = [Car new];
[car print];
return 0;
}
#pragma mark - 间接
/*int main(int argc, const char * argv[]) {
/*if(argc==1){
NSLog(@"you need input file!");
return 1;
}/*
FILE *fp=fopen(argv[1],"r");
//FILE *fp=fopen("/tmp/test.txt","r");
FILE *fp=fopen("/usr/share/dict/words","r");
char word[100];
while(fgets(word,100,fp))
{
word[strlen(word)-1]='\0';
NSLog(@"%s is %lu characters long",word,strlen(word));
}
fclose(fp);
return 0;
}*/
OC基础——复合
最新推荐文章于 2025-05-27 20:20:57 发布