赖和英文水平有限,百度了好半天,知道了,意思,才解决的.
就是object的类对象,必须有一个指针指向他.
就是必须要指针指向对象,
错误代码为:
typedef enum {
SexMan,
SexWoman
} Sex;
typedef struct {
int year;
int mothe;
int day;
} Born;
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
@public
char *name;
int tizhong;
}
-(void)run;
@end
@implementation Dog
-(void) run
{
NSLog(@"%s 遛狗 后 体重:%d",name,tizhong);
}
@end
@interface Student : NSObject
{
@public
char *name;
Born born;
Sex sex;
int age;
int tizhong;
int count;
//狗
Dog *dog;
}
-(void) eat;
-(void) run;
-(void) info;
-(void) liugou;
@end
@implementation Student
-(void)liugou
{
[dog run];
NSLog(@"%s 狗遛了后,体重 %d",dog->name,dog->tizhong);
}
-(void)eat
{
++tizhong;
NSLog(@"吃了后体重:%d",tizhong);
}
-(void) run
{
--tizhong;
NSLog(@"跑了步后体重:%d",tizhong);
}
-(void) info
{
NSLog(@"name:%s,born:%d-%d-%d,sex:%d,age:%d,tizhong:%d",name,born.year,born.mothe,born.day,sex,age,tizhong);
}
@end
//类 必须声明,成员变量
@interface Car : NSObject
{
@public
char name;
int year;
}
- (void) run;
@end
int main()
{
/*
Car *c1 = [Car new];
c1->name='n';
c1->year=13;
[c1 run];
*/
Student *stu = [Student new];
stu->tizhong=50;
stu->sex=SexWoman;
Born b={1999,9,9};
stu->born=b;
stu->born.day=10;
stu->name="tom";
stu->age=15;
Dog g = [Dog new];
g->name="diandian";
g->tizhong=20;
stu->dog=g;
[stu liugou];
[stu info];
[stu eat];
[stu eat];
[stu run];
[stu run];
return 0;
}
//test函数,属于文件的,
//注意不能放到类的声明中
void test()
{
NSLog(@"调用了test函数。");
}
@implementation Car
//对象函数,必须写 -
-(void) run
{
test();
test2();
NSLog(@"%c run,%d year",name,year);
}
void test2()
{
NSLog(@"调用了test2函数。");
}
@end
其实就修改一句
car.m:102:9: error: interface type cannot be statically allocated
Dog g = [Dog new];
//改为
Dog *g = [Dog new];