main.m
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Dog.h"
int main(int argc, const char * argv[])
{
Student *s = [Student new];
Dog *dog = [Dog new];
s->weight = 60;
s->sex = SexWoman;
s->favColor = ColorGreen;
s->birthday.year = 1995;
s->birthday.month = 2;
s->birthday.day = 1;
[s run];
[s eat];
[s print];
dog->weight = 20;
dog->curColor = ColorGreen;
[s eatDog];
[s playDog];
[dog run];
[dog eat];
return 0;
}
Student.h
/*
類名:Student
屬性:性別,生日邮弹,體重舀瓢,喜歡的顏色廷雅,狗(體重,毛的顏色,吃榜轿,跑)
方法:吃幽歼,跑步,喂狗谬盐,遛狗
*/
#impprt <Foundation/Foundation.h>
@class Dog;
typedef enum //枚舉
{
SexMan, //枚舉常量命名一般以枚舉類型的名稱開頭
SexWoman //枚舉每項(xiàng)以逗號(hào)隔開甸私,最后一項(xiàng)不寫
}Sex;
typedef struct //結(jié)構(gòu)體
{
int year;
int month;
int day;
}Date;
typedef enum
{
ColorBlack,
ColorRed,
ColorGreen
}Color;
@interface Student : NSObject
{
@public
Sex sex;
Date birthday;
double weight; //包括小數(shù)點(diǎn)后
Color favColor;
Dog *dog;
}
- (void) run;
- (void) eat;
- (void) print;
- (void) eatDog;
- (void) playDog;
@end
Student.m
#import "Student.h"
@implementation Student
- (void) run
{
weight -=1;
NSLog(@"學(xué)生成功減掉一斤肉");
}
- (void) eat
{
weight +=1;
NSLog(@"學(xué)生長(zhǎng)了一斤肉");
}
- (void) print
{
NSLog(@"性別=%d,體重=%f飞傀,最喜歡的顏色=%d皇型,生日是=%d-%d-%d",sex,weight,favColor,birthday);
}
- (void) eatDog
{
// dog->weight +=1; 面向過(guò)程,非面向?qū)ο?/p>
[dog eat];
}
- (void) playDog
{
[dog run];
}
@end
Dog.h
#import <Foundation/Foundation.h>
//狗(體重砸烦,毛的顏色弃鸦,吃,跑)
typedef enum
{
ColorBlack,
ColorRed,
ColorGreen
}Color;
@interface Dog : NSObject
{
@public
double weight;
Color curColor;
}
- (void) run;
- (void) eat;
@end
Dog.m
#import "Dog.h"
@implementation Dog
- (void) run
{
weight -=1;
NSLog(@"狗成功減掉一斤肉");
}
- (void) eat
{
weight +=1;
NSLog(@"狗長(zhǎng)了一斤肉");
}
@end
@class A:只調(diào)用類
#import "A.h":調(diào)用類和方法