#import <Foundation/Foundation.h>
#import "Human.h"
#import "Person.h"
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Human *human = [[Human alloc] init];
[human buyWater];
[human sayHi];
[human lentOfMoney:100];
[human drinkOfWater:@"农夫山泉" tea:@"铁观音"];
NSString *tea = [human drinkTea];
NSLog(@"tea:%@",tea);
NSString *newString = [human drinkOfOtherWater:@"矿泉水" tea:@"绿茶"];
NSLog(@"new:%@",newString);
NSInteger sum = [human sumOfNumber:100 otherNumber:200];
NSLog(@"sum = %ld",sum);
[Human test];
Human *human1 = [[Human alloc] init];
[human1 setName:@"lili"];
[human1 setAge:11];
human1.name = @"lili";
NSLog(@"%@",human1.name);
human1.age = 11;
NSLog(@"human1 = %@",human1);
Student *student = [[Student alloc] init];
student.name = @"tom";
[student hello];
[student test];
[Student test];
Person *person = [[Student alloc] init];
[person hello];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Human : NSObject
{
NSString *_name;
NSInteger _age;
}
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
+ (Human *)humanWithName:(NSString *)name age:(NSInteger)age;
- (void)buyWater;
- (void)sayHi;
- (void)lentOfMoney:(NSInteger)money;
- (void)drinkOfWater:(NSString *)water tea:(NSString *)tea;
- (NSString *)drinkTea;
- (NSString *)drinkOfOtherWater:(NSString *)water tea:(NSString *)tea;
- (NSInteger)sumOfNumber:(NSInteger)number otherNumber:(NSInteger)otherNumber;
+ (void)test;
+ (Human *)human;
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setAge:(NSInteger)age;
- (NSInteger)age;
@end
#import "Human.h"
@implementation Human
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age
{
self = [super init];
if (self) {
_name = name;
_age = age;
}
return self;
}
- (void)buyWater{
NSLog(@"我想喝水,你帮我买!");
}
- (void)sayHi{
NSLog(@"早上好");
}
- (void)lentOfMoney:(NSInteger)money{
NSLog(@"请你借%ld",money);
}
- (void)drinkOfWater:(NSString *)water tea:(NSString *)tea{
NSLog(@"我想喝%@泡的%@茶",water,tea);
}
- (NSString *)drinkTea{
return @"红茶";
}
- (NSString *)drinkOfOtherWater:(NSString *)water tea:(NSString *)tea{
NSString *string = [NSString stringWithFormat:@"我想喝%@泡的%@茶",water,tea];
return string;
}
- (NSInteger)sumOfNumber:(NSInteger)number otherNumber:(NSInteger)otherNumber{
return number + otherNumber;
}
+ (void)test{
NSLog(@"类方法test");
}
+ (Human *)human{
Human *human1 = [[Human alloc] init];
return human1;
}
+ (Human *)humanWithName:(NSString *)name age:(NSInteger)age{
Human *human = [[Human alloc] initWithName:name age:age];
return human;
}
- (void)setName:(NSString *)name{
_name = name;
NSLog(@"_____%@",_name);
}
- (NSString *)name{
return _name;
}
- (void)setAge:(NSInteger)age{
if (age < 0) {
_age = 18;
return;
}else{
_age = age;
}
}
- (NSInteger)age{
return _age;
}
- (NSString *)description{
NSString *string = [NSString stringWithFormat:@"name:%@,age = %ld",_name,_age];
return string;
}
@end
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
NSInteger _age;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setAge:(NSInteger)age;
- (NSInteger)age;
- (void)hello;
@end
#import "Person.h"
@implementation Person
- (void)setName:(NSString *)name{
_name = name;
}
- (NSString *)name{
return _name;
}
- (void)setAge:(NSInteger)age{
_age = age;
}
- (NSInteger)age{
return _age;
}
- (void)hello{
NSLog(@"person hello");
}
@end
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Student : Person
{
NSString *_code;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setAge:(NSInteger)age;
- (NSInteger)age;
- (void)setCode:(NSString *)code;
- (NSString *)code;
+ (void)test;
- (void)test;
- (void)hello;
@end
#import "Student.h"
@implementation Student
- (void)setName:(NSString *)name{
_name = name;
}
- (NSString *)name{
return _name;
}
- (void)setAge:(NSInteger)age{
_age = age;
}
- (NSInteger)age{
return _age;
}
- (void)setCode:(NSString *)code{
_code = code;
}
- (NSString *)code{
return _code;
}
- (void)hello{
NSLog(@"student hello");
}
- (void)test{
NSLog(@"---:%@",self);
}
+ (void)test{
NSLog(@"+++++:%@",self);
}
@end