属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter

一、普通的get 和set 属性。

一般的get 和set 属性。就是在get 中返回一个变量的值,而在set 属性中给一个变量赋值,一般的我们也把他们称之为接口,用来访问类的私有(private)或者保护(protected)成员。

Circle.h文件
#import <Foundation/Foundation.h>
@interface Circle : NSObject{
    int radius;
    int x;
    int y;
}
-(int)getX;
-(int)getY;
-(int)getRadius;
-(void)setR:(int)_r andX:(int)_x andY:(int) _y;
-(void)setR:(int)_r;
-(void)setY:(int)_y;
-(void)print;
@end

Circle.m文件

#import <Foundation/Foundation.h>
#import "Circle.h"
@implementation Circle
@synthesize  radius,x;
@synthesize  y;
-(int)getX{
    return x;
}
-(int)getY{
    return y;
}
-(int)getRadius{
    return radius;
}
-(void)setR:(int)_r andX:(int)_x andY:(int) _y{
    radius=_r;
    x=_x;
    y=_y;
}
-(void)setR:(int)_r{
    radius=_r;
}
-(void)setY:(int)_y{
    y=_y;
}
-(void)print{
     NSLog(@"radius = %d ,x = %d , y = %d",[self getRadius],[self getX],[self getY]);
}
@end
二、自动生成 get 和 set 方法

自动生成 get 和 set 方法:使用关键字property 。格式:@property type  variablesName,...; 一次可以声明多个但是必须是同一种类型。这样在声明之后我们就可以使用 对象.变量名 的格式
来作为 get 和 set 方法,其中有右值得话就是set方法,没有右值就是get方法。
例如

Circle.h文件

#import <Foundation/Foundation.h>
@interface Circle : NSObject{
    int radius;
    int x;
    int y;
}
-(int)getX;
-(int)getY;
-(int)getRadius;
-(void)setR:(int)_r andX:(int)_x andY:(int) _y;
-(void)setR:(int)_r;
-(void)setY:(int)_y;
-(void)print;
//@property (nonatomic)int radius,x,y;  //nonatomic的意思是禁止多线程,保护变量,提高性能。
@property (nonatomic)int radius,x;
@property (getter = gy,setter = sy:)int y;
@end

Circle.m文件

#import <Foundation/Foundation.h>
#import "Circle.h"
@implementation Circle
@synthesize  radius,x;
@synthesize  y;
-(int)getX{
    return x;
}
-(int)getY{
    return y;
}
-(int)getRadius{
    return radius;
}
-(void)setR:(int)_r andX:(int)_x andY:(int) _y{
    radius=_r;
    x=_x;
    y=_y;
}
-(void)setR:(int)_r{
    radius=_r;
}
-(void)setY:(int)_y{
    y=_y;
}
-(void)print{
     NSLog(@"radius = %d ,x = %d , y = %d",self.radius,self.x,[self gy]);
}
@property (nonatomic)int radius,x,y;
@end

main.m文件

#import <Foundation/Foundation.h>
#import "Circle.h"
#import "Person.h"
 
int main (int argc, const char * argv[])
{
    @autoreleasepool {
       Circle* c=[[Circle alloc]init];
        [c setR:2 andX:3 andY:5];
        [c print];
        c.radius=10;
        c.x=10;
        c.y=10;
        [c print];
        NSLog(@"radius = %d ,x = %d , y = %d ",c.radius,c.x,c.y);
        NSLog(@"radius = %d ,x = %d , y = %d",c.radius,c.x,c.y);
        c.y=40;
        [c y:50];  

NSLog(@"y = %d",c.y);

    }
    return 0;
}
三、getter 和 setter 

getter 和 setter的作用是指定get 和set 属性的名字,给get 和set 属性重命名。在使用getter 和 setter 之后,就不可以继续使用系统自动生成的get 和 set 方法

Circle.h文件

#import <Foundation/Foundation.h>
 
@interface Circle : NSObject{
    int radius;
    int x;
    int y;
}
-(int)getX;
-(int)getY;
-(int)getRadius;
-(void)setR:(int)_r andX:(int)_x andY:(int) _y;
-(void)setR:(int)_r;
-(void)setY:(int)_y;
-(void)print;
//@property (nonatomic)int radius,x,y;
@property (nonatomic)int radius,x;
@property (getter = gy,setter = sy:)int y;
@end
Circle.m文件
#import <Foundation/Foundation.h>
#import "Circle.h"
@implementation Circle
@synthesize  radius,x;
@synthesize  y;
-(int)getX{
    return x;
}
-(int)getY{
    return y;
}
-(int)getRadius{
    return radius;
}
-(void)setR:(int)_r andX:(int)_x andY:(int) _y{
    radius=_r;
    x=_x;
    y=_y;
}
-(void)setR:(int)_r{
    radius=_r;
}
-(void)setY:(int)_y{
    y=_y;
}
-(void)print{
     NSLog(@"radius = %d ,x = %d , y = %d",self.radius,self.x,[self gy]);
}
@end

main.m文件  主函数调用

#import <Foundation/Foundation.h>
#import "Circle.h"
#import "Person.h"
 
int main (int argc, const char * argv[])
{
    @autoreleasepool {
        Circle* c=[[Circle alloc]init];
        [c setR:2 andX:3 andY:5];
        [c print];
        c.radius=10;
        c.x=10;
        c.y=10;
        [c print];
        NSLog(@"radius = %d ,x = %d , y = %d ",c.radius,c.x,c.y);
        [c sy:30];
        NSLog(@"radius = %d ,x = %d , y = %d",c.radius,c.x,c.y);
        c.y=40;
        NSLog(@"y= %d",[c gy]);
        NSLog(@"y= %d",c.gy);
        NSLog(@"y= %d",c.getX);  
    }
    return 0;
}

属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter的更多相关文章

  1. Eclipse用法和技巧六:自动生成get和set方法1

    java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改了属性值.声 ...

  2. Eclipse用法和技巧七:自动生成get和set方法2

    上一篇文章中我们介绍了自动批量生成get和set函数的方法.这个方法一般在声明完类的数据域之后使用,比较方便快捷.这里再补充几个自动生成get和set函数的方法. 步骤一:在声明的数据域中按Ctrl+ ...

  3. python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner

    python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner 下面给大家介绍下用HTMLTestRunner模块自动生成测试报告的方法. 一.首先我们导入unit ...

  4. 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解

    第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一.    引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...

  5. wpf 通过为DataGrid所绑定的数据源类型的属性设置Attribute改变DataGrid自动生成列的顺序

    环境Win10 VS2019 .Net Framework4.8 在wpf中,如果为一个DataGrid绑定到一个数据源,默认情况下DataGrid会为数据源类型的每个属性生成一个列(Column)对 ...

  6. Eclipse用法:自动生成get和set方法

      方法一 Java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改 ...

  7. 自动生成get,set方法

    引发的问题: Action中有一个属性名字叫private boolean isHideNumber 用struts2的<s:if test ="isHideNumber"& ...

  8. (jdbc)取得数据库自动生成的主键方法

    一些类,在前面的博客中有,就不重复了 public class Test2 { TestDAO t=new TestDAO(); /*前提是数据表的主键是自动增加的, *取得数据库自动生成的主键 * ...

  9. Eclipse,IDEA自动生成相应对象接收方法返回值的快捷键

    @Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemM ...

  10. JavaBean自动生成get和set方法

    用Myeclipse开发java web程序,写javabean的时候,如果字段很多的话,写get和set方法是一件很无语和浪费时间的事情,所以Myeclipse提供了一个自动生成这些方法的功能.   ...

随机推荐

  1. Innodb刷脏页技术深度挖掘

    DBA某数据库集群每日17:00左右会出现一个性能陡降的现象,在10~20秒内主库出现大量慢查询.这些查询本身没有性能问题,也没有任何关联,可以认为是由于数据库系统负载较重,由于并发导致的慢查询.通过 ...

  2. MySQL 大表优化方案探讨

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...

  3. UVALive 7276 Wooden Signs (DP)

    Wooden Signs 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/E Description http://7xjob4. ...

  4. UVaLive 7362 Farey (数学,欧拉函数)

    题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数. 析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然A ...

  5. SOURCES的文件格式

    SOURCES的文件格式: TARGETNAME=drivername , -本参数用于指定生成的设备驱动程序名称(不需后缀名),所产生的文件 -为drivername.sys. TARGETPATH ...

  6. hdu 4578 Transformation(线段树)

    线段树上的多操作... 题目大意: 树上 的初始值为0,然后有下列三种操作和求和. 1  x y c  在X-Y的之间全部加上C. 2  x y c  在X-Y的之间全部乘上C. 3  x y c   ...

  7. CGContext绘图

    0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGContex ...

  8. heritrix

    Heritrix3.0教程    http://blog.csdn.net/neo_liukun/article/category/1118819

  9. Ubuntu 15.04下MySQL 5.6.25不支持中文解决办法

    Ubuntu 15.04下MySQL 5.6.25不支持中文解决办法,apt-get install 安装的,不是源码包安装的mysql. 1 修改mysql的配置文件 /etc/mysql/conf ...

  10. 一个简单的小例子让你明白c#中的委托-终于懂了!

    模拟主持人发布一个问题,由多个嘉宾来回答这个问题. 分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类. 作为问题的发布者,Host不知道问题如何解答.因此它只能发布这个事件,将事 ...