zoukankan      html  css  js  c++  java
  • 点语法、property、self注意

    1.点语法(找出不合理的地方)
    #import <Foundation/Foundation.h>
    @interface Person : NSObject
    {
        int _age;
    }
    - (void)setAge:(int)age;
    - (int)age;
    @end

    @implementation Person
    {
        int _age;
    }
    - (void)setAge:(int)age
    {
        _age = age;
        // 会引发死循环
        // self.age = age;

    }
    - (int)age
    {
        return _age;
        // 会引发死循环
        // return self.age;

    }
    @end



    2.@property
    #import <Foundation/Foundation.h>
    @interface Person : NSObject
    {
        // no的set和get方法全部都手动实现了,因此系统就不会再生成_no这个成员变量了,只实现一个可产生
        int _no;
    }
    @property int age;
    @property int no;
    - (void)test;
    @end

    @implementation Person
    - (void)test
    {
        NSLog(@"年龄是%d, 号码是%d", _age, _no);
    }

    - (void)setAge:(int)age
    {
        _age = age;
    }

    // _no的set和get方法全部都手动实现了,因此系统就不会再生成_no这个成员变量了
    - (void)setNo:(int)no
    {
        _no = no;
    }

    - (int)no
    {
        return _no;
    }
    @end





    3.成员变量的作用域
    #import <Foundation/Foundation.h>
    @interface Person : NSObject
    {
        int _age;
        
    @public
        int _no;
        int _weight;
        
    @private
        int _height;
        
    @protected
        int _money;
    }
    @end

    @interface Student : Person
    - (void)test;
    @end

    @implementation Student
    {
        int _money;
    @public
        int _friendsCount;
    }
    - (void)test
    {
        _age = 10;
        _no = 1;
        
        // 私有的不能在子类中访问,即在implementation中声明的变量、或在声明中说明为私有的
        // _name = @"Jack";

        _money = 100;
        
        // 私有的不能在子类中访问
        // _height = 170;
    }
    @end

    int main()
    {
        Student *s = [Student new];
        s->_no = 1;
        //@propected类型的变量只能在当前类和子类中访问
        //s->_age = 20;
        // 在main函数后面的成员变量都不能直接访问
        //s->_name = @"Rose";
        // 在main函数后面的成员变量都不能直接访问
        //s->_color = 10;
        s->_friendsCount = 500;
        return 0;
    }

    @implementation Person
    {
        NSString *_name;
    @public
        int _color;
    }
    @end

  • 相关阅读:
    2.CCNA第二天-主机到主机通讯模型
    3.CCNA第三天-认识和操作思科IOS操作系统
    JAVA入门到精通-第67讲-sqlserver作业讲评
    JAVA入门到精通-第65讲-sql server JDBC
    JAVA入门到精通-第66讲-sql server-JDBC
    JAVA入门到精通-第64讲-sql server备份恢复
    linux 查看Apache Tomcat日志访问IP前10
    Linux进程通信方式
    Linux 运维常用命令
    线程池
  • 原文地址:https://www.cnblogs.com/Alling/p/3971918.html
Copyright © 2011-2022 走看看