蓝胖子

上帝和人的故事-案例
//本案例含有多个文件,请注意分割线区分 //main.m /* 有1个类:叫做上帝类. ...
扫描右侧二维码阅读全文
07
2019/01

上帝和人的故事-案例

//本案例含有多个文件,请注意分割线区分   
//main.m

/*
 
  有1个类:叫做上帝类.
 
      属性: 姓名 年龄. 性别.
      行为: 杀人.
 
 
  人类:
      属性: 姓名 年龄 性别 剩余的寿命.
      行为: 展示.
 
 
 
 
 */

#import <Foundation/Foundation.h>
#import "God.h"

int main(int argc, const char * argv[])
{
  
    God *g1 = [God new];
    g1->_name = @"耶稣";
    g1->_age = 99999;
    g1->_gender = GenderMale;
    
    
     Person *p1 =   [g1 makePersonWithName:@"小二" andAge:0 andGender:GenderMale andLeftLife:100];
    [p1 show];
    
    
    Person *p2 =  [g1 makePersonWithName:@"大狗" andAge:10 andGender:GenderMale andLeftLife:1];
    
    
    
//    Person *p1 = [Person new];
//    p1->_name = @"小东";
//    p1->_age = 21;
//    p1->_leftLife = 10;
//    p1->_gender = GenderMale;
//    
//    
//    [g1 killWithPerson:p1];
//    
//    
//    NSLog(@"p1->_leftLife = %d",p1->_leftLife);
//    
//    
//    Person *p2 =  [g1 makePerson];
//    Person *p3 =  [g1 makePerson];
//    
//    [p2 show];
    
    
    
    
    
    
    
    
    return 0;
}




//
//  God.h
//  Day02-类与对象
//
//  Created by  on 20/7/2.
//  Copyright (c) 2020年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Gender.h"
#import "Person.h"

@interface God : NSObject
{
    @public
    NSString *_name;
    int _age;
    Gender _gender;
}

- (void)killWithPerson:(Person *)per;

- (Person *)makePerson;

- (Person *)makePersonWithName:(NSString *)name andAge:(int)age andGender:(Gender)gender andLeftLife:(int)leftLife;


@end




//
//  God.m
//  Day02-类与对象
//
//  Created by  on 20/7/2.
//  Copyright (c) 2020年 itcast. All rights reserved.
//

#import "God.h"

@implementation God
- (void)killWithPerson:(Person *)per
{
    NSLog(@"喝下这杯毒药,凡人....");
    per->_leftLife = 0;
    NSLog(@"名字叫做%@的人,已经死了",per->_name);
}

- (Person *)makePerson
{
    Person *p1 = [Person new];
    p1->_name = @"夏娃";
    p1->_age = 10;
    p1->_gender =GenderFeMale;
    p1->_leftLife = 10000;
    
    
    return p1;
    
}


- (Person *)makePersonWithName:(NSString *)name andAge:(int)age andGender:(Gender)gender andLeftLife:(int)leftLife
{
    Person *p1 = [Person new];
    p1->_name = name;
    p1->_age = age;
    p1->_gender = gender;
    p1->_leftLife = leftLife;
    return p1;
}

@end




//
//  Person.h
//  Day02-类与对象
//
//  Created by  on 20/7/2.
//  Copyright (c) 2020年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Gender.h"



@interface Person : NSObject
{
    @public
    NSString *_name;
    int _age;
    Gender _gender;
    int _leftLife;
}


- (void)show;

@end



//
//  Person.m
//  Day02-类与对象
//
//  Created by  on 20/7/2.
//  Copyright (c) 2020年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person
- (void)show
{
    NSLog(@"我叫%@,我还有%d年可以活.",_name,_leftLife);
}
@end



//
//  Gender.h
//  Day02-类与对象
//
//  Created by  on 20/7/2.
//  Copyright (c) 2020年 itcast. All rights reserved.
//

typedef enum
{
    GenderMale,
    GenderFeMale
} Gender;
最后修改:2019 年 01 月 07 日 07 : 10 PM

发表评论