本帖最后由 侯金龙 于 2014-4-15 17:00 编辑
#import
typedef enum {
SexMan,
SexWoman
} Sex;
typedef struct {
int year;
int month;
int day;
}Date;
ttypedef enum{
ColorBlack,
ColorRed,
ColorGreen,
}Color;
@interface Dog : NSObject //狗类
{
@public;
double weight; // 体重
Color curColor; //毛色
}
-(void) eat; -(void) run; @end
@implementation Dog
-(void) eat { // 每吃一次,体重就加1 、 weight +=1; NSLog (@"狗吃完这次后的体重是%f".weight ); } -(void) run
{
weight -= 1;
NSLog (@"狗跑完这次后的体重是%f".weight );
}
@end
@interface Student : NSObject { Sex sex; // 性别 Date birthday; //生日 double weight;//体重(kg) Coler favColor;//最喜欢的颜色 char *name; //什么数据类型都可以
//重点:狗 Dog *dog; } -(void) eat; -(void) run; -(void)print;
- (void)weidog; - (void)liudog; @end @implementation Student -(void) eat { // 每吃一次,体重就加1 、 weight +=1; NSLog (@"吃完这次后的体重是%f".weight ); } -(void) run
{
weight -= 1;
NSLog (@"跑完这次后的体重是%f".weight );
}
-(void)print
{
NSlog(@"性别=%d,喜欢的颜色=% d,姓名=%s.生日=%d-%d-%d",sex,favColor,name,birthday.
year,birthday.month,birthday.day);
}
- (void)weidog; { //让狗吃东西(调用狗eat 方法) [dog eat] } - (void)liudog; {
//让狗跑起来(调用狗的run方法) [dog run]; } @end
int main()
{
Student *s = [Student new ];
Dog *d = [Dog new];
d->curColor = ColorGreen;
d->weight = 20;
s->dog = d;
[s liudog];
[s weidog];
return 0;
}
void test( )
{
Student *s = [Student new ];
s->weight = 50;
// 性别
s->sex = SexMan;
// 生日
//s->birthday = {2011,9,10}; 错误写法
Date d = {2011,9.10};
s->birthday = d;
s->name = "jack"
// 喜欢的颜色
s->favColor = ColorBlavk;
[s print];
}
Student 的- (void)weidog;- (void)liudog;为什么可以调用-(void) eat;-(void) run;的方法,是不同类之间的方法就是可以互相调用,还是因为别的
|