IOS之学习笔记四(类的实现和对象和id)
        【摘要】  1、简单构建类和对象和id使用的测试代码如下 
Person.h 
#ifndef Person_h#define Person_h #import <Foundation/Foundation.h>@interface Person : NSObject{ NSString* _name; int _age;}-(void)setName:(NSString *...
    
    
    
    1、简单构建类和对象和id使用的测试代码如下
Person.h
  
   - 
    
     
    
    
     
      #ifndef Person_h
     
    
- 
    
     
    
    
     
      #define Person_h
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      #import <Foundation/Foundation.h>
     
    
- 
    
     
    
    
     
      @interface Person : NSObject
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     
       NSString* _name;
     
    
- 
    
     
    
    
     
       int _age;
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      -(void)setName:(NSString *)name addAge:(int)age;
     
    
- 
    
     
    
    
     
      -(void)say:(NSString *)content;
     
    
- 
    
     
    
    
     
      -(NSString *)info;
     
    
- 
    
     
    
    
     
      +(void)foo;
     
    
- 
    
     
    
    
     
      @end
     
    
- 
    
     
    
    
     
      #endif /* Person_h */
     
    
 Person.m
  
   - 
    
     
    
    
     
      #import "Person.h"
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      @implementation Person
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     
       int _testAdd;
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      -(void)setName:(NSString *)name addAge:(int)age {
     
    
- 
    
     
    
    
     
       _name = name;
     
    
- 
    
     
    
    
     
       _age = age;
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      -(void)say:(NSString *)content
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     
       NSLog(@"content is %@", content);
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      -(NSString *)info
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     
       [self test];
     
    
- 
    
     
    
    
     
       return [NSString stringWithFormat:@"the persion is %@, and age is %d", _name, _age];
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      -(void)test
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     
       NSLog(@"this is test method");
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      +(void)foo
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
     
       NSLog(@"this is foo method");
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      @end
     
    
 main.m
  
   - 
    
     
    
    
     
      #import "Person.h"
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int main(int argc, char * argv[]) {
     
    
- 
    
     
    
    
     
       @autoreleasepool {
     
    
- 
    
     
    
    
     
       Person *person = [[Person alloc] init];
     
    
- 
    
     
    
    
     
       [person setName:@"chenyu" addAge:26];
     
    
- 
    
     
    
    
     
       NSString *info = [person info];
     
    
- 
    
     
    
    
     
       NSLog(@"%@", info);
     
    
- 
    
     
    
    
     
       [person say:@"chenyu"];
     
    
- 
    
     
    
    
     
       [Person foo];
     
    
- 
    
     
    
    
     
       //id类型可以代表所有对象的类型,id类型执行方法会动态绑定
     
    
- 
    
     
    
    
     
       //id p不是id *p;
     
    
- 
    
     
    
    
     
       id p = [[Person alloc] init];
     
    
- 
    
     
    
    
     
       [p setName:@"chenyu" addAge:26];
     
    
- 
    
     
    
    
     
       NSString *in = [p info];
     
    
- 
    
     
    
    
     
       NSLog(@"%@", in);
     
    
- 
    
     
    
    
     
       [p say:@"chenyu"];
     
    
- 
    
     
    
    
     
       }
     
    
- 
    
     
    
    
     
      }
     
    
 
 2、运行结果
  
   - 
    
     
    
    
     
      this is test method
     
    
- 
    
     
    
    
     
      the persion is chenyu, and age is 26
     
    
- 
    
     
    
    
     
      content is chenyu
     
    
- 
    
     
    
    
     
      this is foo method
     
    
- 
    
     
    
    
     
      this is test method
     
    
- 
    
     
    
    
     
      the persion is chenyu, and age is 26
     
    
- 
    
     
    
    
     
      content is chenyu
     
    
 
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/80933896
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)