Objective-c class method declaration initialization and invocation

Source: Internet
Author: User


 
//
//  Student.h
//  OOP
//
//  Created by acgity on 16/5/16.
//  Copyright © 2016年 acgity. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum {
    male,female
} SEX;

@interface Student : NSObject
{
    @public
    NSString *_name;
    int _age;
    SEX _sex;
}

+(void)walk;
-(void)talk;
-(instancetype)init;
-(instancetype)initWithName:(NSString *)name withAge:(int)age withSex:(SEX)sex;

@end
 
//
//  Student.m
//  OOP
//
//  Created by acgity on 16/5/16.
//  Copyright © 2016年 acgity. All rights reserved.
//

#import "Student.h"

@implementation Student

-(instancetype)init{
    self = [super init];
    if(self != nil){
        self -> _name = @"acgity";
        self -> _age = 23;
        self -> _sex = female;
    }
    return self;
}

-(instancetype)initWithName:(NSString *)name withAge:(int)age withSex:(SEX)sex{
    self = [super init];
    if(self != nil){
        self->_name = name;
        self->_age = age;
        self->_sex = sex;
    }
    return self;
}

-(void)talk {
    NSString *flag = self->_sex == male ? @"girl" : @"boy";
    NSLog(@"Hi,I‘m a %@ named %@ %d years old...",flag,_name,_age);
}

+(void)walk {
    NSLog(@"I‘m walking now...");
}
@end
 
//
//  main.m
//  OOP
//
//  Created by acgity on 16/5/16.
//  Copyright © 2016年 acgity. All rights reserved.
//

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

int main(int argc, const char * argv[]) {
    
    Student *df = [[Student alloc] init];
    [df talk];
    [Student walk];
    
    Student *ini = [[Student alloc] initWithName:@"tony" withAge:32 withSex:male];
    [ini talk];
    [Student walk];
    
    return 0;
}


2016-05-16 18:46:34.287 oop[3140:120047] hi,i ' m a boy named Acgity all years old ...



2016-05-16 18:46:34.288 oop[3140:120047] I ' m walking now ...



2016-05-16 18:46:34.288 oop[3140:120047] hi,i ' m a girl named Tony years old ...



2016-05-16 18:46:34.288 oop[3140:120047] I ' m walking now ...



Program ended with exit code:0



Objective-c class method declaration initialization and invocation


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.