zoukankan      html  css  js  c++  java
  • oc24--description

    //  Person.h
    
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    {
        int _age;
        double _height;
        double _weight;
        NSString *_name;
        NSString *_tel;
        NSString *_email;
    }
    
    
    - (void)setAge:(int)age;
    - (void)setHeigth:(double)height;
    - (void)setWeight:(double)weight;
    - (void)setName:(NSString *)name;
    - (void)setTel:(NSString *)tel;
    - (void)setEmail:(NSString *)email;
    
    - (int)age;
    - (double)height;
    - (double)weight;
    - (NSString *)name;
    - (NSString *)tel;
    - (NSString *)email;
    @end
    //  Person.m
    
    #import "Person.h"
    
    @implementation Person
    
    - (void)setAge:(int)age
    {
        _age = age;
    }
    - (void)setHeigth:(double)height
    {
        _height = height;
    }
    - (void)setWeight:(double)weight
    {
        _weight = weight;
    }
    - (void)setName:(NSString *)name
    {
        _name = name;
    }
    - (void)setTel:(NSString *)tel
    {
        _tel = tel;
    }
    - (void)setEmail:(NSString *)email
    {
        _email = email;
    }
    
    - (int)age
    {
        return _age;
    }
    - (double)height
    {
        return _height;
    }
    - (double)weight
    {
        return _weight;
    }
    - (NSString *)name
    {
        return _name;
    }
    - (NSString *)tel
    {
        return _tel;
    }
    - (NSString *)email
    {
        return _email;
    }
    
    // 可以重写description方法, 返回我们需要打印的内容
    // 只要利用%@打印对象, 就会调用description
    // 如果打印的是对象就会调用-号开头的description方法
    - (NSString *)description
    {
        
        /*
         访问属性有三种方式
         p->_age;  //属性
         [p age];  //get方法
         p.age;   //点语法
         
         self写在对象方法中就代表当前调用该方法的对象
         self.age; // [self age];
         self->age;
         [self age];
         */
        
        
        return @"xxoo";
        NSString *str = [NSString stringWithFormat:@"age = %i, name = %@, height = %f, weight = %f, tel = %@, email = %@", _age, _name, _height, _weight, _tel, _email];
        return str;
        
        NSLog(@"-------------");
        // 建议: 在description方法中尽量不要使用self来获取成员变量
        因为如果你经常在description方法中使用self, 可能已不小心就写成了 %@, self
        // 如果在description方法中利用%@输出self会造成死循环
        // self == person实例对象
        return [NSString stringWithFormat:@"%@", self];
    }
    
    // 仅仅作为了解, 开发中99%的情况使用的都是-号开头的description
    + (NSString *)description
    {
        return @"ooxx";
    }
    /*
     如果通过%@打印对象就会调用-号开头的
     如果通过%@打印类对象就会调用+号开头的
     */
    @end
    //
    //  main.m
    //  description
    
    #import <Foundation/Foundation.h>
    #import "Person.h"
    
    int main(int argc, const char * argv[]) {
        
        Person *p = [Person new];
        [p setAge:30];
        [p setName:@"lnj"];
        [p setHeigth:1.75];
        [p setWeight:65];
        [p setTel:@"13554499311"];
        [p setEmail:@"lnj@520it.com"];
        
        NSLog(@"age = %i, name = %@, height = %f, weight = %f, tel = %@, email = %@", [p age], [p name], [p height], [p weight], [p tel], [p email]);
        // %@是用来打印对象的, 其实%@的本质是用于打印字符串
        // 只要利用%@打印某个对象, 系统内部默认就会调用父类的description方法
        // 调用该方法, 该方法会返回一个字符串, 字符串的默认格式 <类的名称: 对象的地址>
        
        NSLog(@"%@", p);
        // class注意c是小写, 只要给类发送class消息, 就会返回当前类的 类对象
        // 1.获取Person对应的类对象
        Class c = [Person class];
        // 2.打印Person的类对象
        NSLog(@"当前对象对应的类 = %@", c);
        NSLog(@"当前对象的地址 = %p", p);
        return 0;
    }
  • 相关阅读:
    【转载】一键安装maven脚本
    secureCRT的自动登录设置
    vi相关内容
    windows上的命令telnet
    【转载】bash: ifconfig: command not found 解决办法
    【转载】Linux中profile、bashrc、bash_profile之间的区别和联系
    virgo-tomcat没有任务错误日志的停掉的解决办法
    【转载】Linux kill, killall, kill -9
    #!bin/sh是啥
    Mysql 查找表中的多组前n大元素
  • 原文地址:https://www.cnblogs.com/yaowen/p/7413634.html
Copyright © 2011-2022 走看看