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];
date b = {2001,02,01};
[s setBirthday:b];
[s birthday];
return 0;
}
Student.h
/*
類名:Student
屬性:性別任斋,生日媚赖,體重煮甥,喜歡的顏色,狗(體重昆淡,毛的顏色,吃法梯,跑)
方法:吃悼枢,跑步,喂狗综看,遛狗
*/
#impprt <Foundation/Foundation.h>
@class Dog;
typedef enum //枚舉
{
SexMan, //枚舉常量命名一般以枚舉類型的名稱開頭
SexWoman //枚舉每項以逗號隔開品腹,最后一項不寫
}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ù)點后
//? Color favColor;
Dog *_dog;
}
//? - (void) run;
//? - (void) eat;
//? - (void) print;
//? - (void) eatDog;
//? - (void) playDog;
- (void)setBirthday:(Date)birthday;
- (Date)birthday;
- (void)setDog:(Dog *)dog;
- (Dog *)dog;
@end
Student.m
#import "Student.h"
@implementation Student
//? - (void) run
//? {
//? weight -=1;
//? NSLog(@"學(xué)生成功減掉一斤肉");
//? }
//? - (void) eat
//? {
//? weight +=1;
//? NSLog(@"學(xué)生長了一斤肉");
//? }
//? - (void) print
//? {
//? NSLog(@"性別=%d,體重=%f红碑,最喜歡的顏色=%d珍昨,生日是=%d-%d-%d",sex,weight,favColor,birthday);
//? }
//? - (void) eatDog
//? {
// dog->weight +=1; 面向過程,非面向?qū)ο?/p>
//? [dog eat];
//? }
//? - (void) playDog
//? {
//? [dog run];
//? }
- (void)setBirthday:(Date)birthday
{
NSLog(@"調(diào)用了它的set方法");
_birthday = birthday;
}
- (date)birthday
{
NSLog(@"調(diào)用了它的get方法");
return _birthday;
}
;@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(@"狗長了一斤肉");
}
@end
@class A:只調(diào)用類
#import "A.h":調(diào)用類和方法