属性声明(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. struts2+Hibernate4+spring3+EasyUI环境搭建之五:引入jquery easyui

    1.下载jquery easyui组件     http://www.jeasyui.com/download/index.php 2.解压 放到工程中  如图 3.jsp引入组件:必须按照如下顺序 ...

  2. linux下编译lua

    curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz 编译代码时,遇到如下错误 /usr/lib/libreadline.so: undefined r ...

  3. vector 之 find 重载

    众所周知,map有find,但vector的find只能调用algorithm中的find通用方法. 参考<How to find an item in a std::vector?> 对 ...

  4. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

  5. threading模块

    threading — Higher-level threading interface¶ Source code: Lib/threading.py This module constructs h ...

  6. angular 管理后台

    http://blog.csdn.net/iamnieo/article/details/50474399

  7. Spring MVC Framework 实例

    一 SpringMVC基础入门,创建一个HelloWorld程序 1 首先,导入SpringMVC需要的jar包. commons-logging-<version>.jar spring ...

  8. ecshop以幻灯版调用首页主广告显示

    默认的是index_ad.lbi模板有一个$flash变量了,但在搜索搜索没发现 <!--{foreach from=$flash name=no item=flash}--> <l ...

  9. 解决Linux下zip文件解压乱码问题

    #!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import zipfile #print "Proce ...

  10. readonly 关键字的用法

    readonly 关键字是可以在字段上使用的修饰符. 当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中. 示例 在此示例中,字段y ...