@property

在XCode4.5以上,写了@property之后,系统会自动生成私有_字段和实现@synthesize方法

但如果自己写了set或者get方法之后,就不会帮你产生默认的下划线字段

Student.h:

#import <Foundation/Foundation.h>

@interface Student : NSObject{
    @public   //以下就是共有类型  还有@private和@protected
    int _age;
    int _no;

}
@property int age;//自动申明getter和setter方法
@property int no;
//-(int)age;
//-(int)no;
////OC里面没有构造方法
//-(void)setAge:(int)newAge;  //setAge:是它的方法名(注意要带上冒号)
//-(void)setNo:(int)newNo;
//
//-(void)setAge:(int)newAge AndNo:(int)newNo;  //setAge: AdnNo:是它方法名
-(id)initWithAge:(int)newAge andWithNo:(int)newNo;
//-(NSString *)description;
@end

Student.m:

#import "Student.h"

@implementation Student
@synthesize age=_age,no=_no;//自动实现setter和getter方法

//#pragma mark - setter
//#pragma mark 设置年龄
//-(void)setAge:(int)newAge{
//    age=newAge;
//}
//#pragma mark 设置学号
//-(void)setNo:(int)newNo{
//    no=newNo;
//}
//#pragma mark 设置年龄跟学号
//-(void)setAge:(int)newAge AndNo:(int)newNo{
//    age=newAge;
//    no=newNo;
//}
//#pragma mark - getter
//#pragma mark 获得学号
//-(int)no{
//    return no;
//}
//#pragma mark 获得年龄
//-(int)age{
//    return age;
//}
#pragma mark - 构造方法
#pragma mark 通过传入年龄和学号初始化一个构造方法
-(id)initWithAge:(int)newAge andWithNo:(int)newNo{
    if(self=[super init]) //首先要初始化父类方法
    {
        _age=newAge;
        _no=newNo;
        return self;
    }
    return nil;
}
//重写父类的description方法
//-(NSString *)description{
//    NSString * str =[NSString stringWithFormat:@"age is %i and no is ",self.age,self.no];
//    return str;
//}
@end

main:

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Student * student=[[[Student alloc] initWithAge:20 andWithNo:1] autorelease];  //自动释放
        NSLog(@"this student age=%i,and no=%i",[student age],[student no]);
        NSLog(@"age=%i,no=%i",student.age,student.no);
        NSLog(@"%@",student);//打印内存地址
        //[student release];//防止内存泄漏,要释放该对象内存
    }
    return 0;
}

结果:

2013-08-02 15:55:34.918 @property[1396:303] this student age=20,and no=1

2013-08-02 15:55:34.919 @property[1396:303] age=20,no=1

2013-08-02 15:55:34.920 @property[1396:303] <Student: 0x100109970>

时间: 2024-08-02 05:02:07

@property的相关文章

org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.*.Paper.totalTime

at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:109) at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:586) at org.hibernate.tuple.entity.PojoEntityTuplizer.s

python property

在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]]) fget:获取属性 fset:设置属性 fdel:删除属性 doc:属性含义 用法 1.让成员函数通过属性方式调用 class C(object): def __init__(self): self._x = None def getx(self): return self._x def setx(self, value):

JavaScript的attribute和property辨析

1.Attribute Attribute是HTML上设置的属性,在html中显式地设置,或者通过setAttribute()方法设置. <input type='text' id='txt' a=b /> 比如这样一段html代码,实际上它有三个attribute属性,我们可以打印出来看看: var a = document.getElementById('txt'); console.log(a.attributes); 对于Attribute而言,它有三个常用的方法setAttribut

十三 The Display Property,display属性

操作HTML元素的诀窍在于明白它们工作的方法在于没有特定的形式.一些由标签组成的大部分页面可以设定任何样式.浏览器默认的样式里的大部分html元素由字体样式.margin,padding组成,本质上是显示类型. display属性基本上分为inline,block,和none. inline就像它的本意--显示为inline的元素为行.strong,anchor锚和em强调元素默认是行. block元素前后换行.标题和段落元素是块元素. none,意思不显示元素,在可用性上可以实现漂亮的效果,交

class Property Get、Property Let 使用说明

<%'在 Class 块中,成员通过相应的声明语句被声明为 Private(私有成员,只能在类内部调用) 或 Public(公有成员,可以在类内外部调用) .'被声明为 Private 的将只在 Class 块内是可见的.被声明为 Public 不仅在 Class 块的内部是可见的,对 Class 块之外的代码也是可见的.'没有使用 Private 或 Public 明确声明的被默认为 Public.在类的块内部被声明为 Public 的过程(Sub 或 Function)将成为类的方法.'Pu

自定义组件之属性(Property)的性质(Attribute)介绍(一)

自定义组件之 属性(Property)的性质(Attribute)介绍 属性(property)作为c#语言中一个重要的组成部分,尤其是在我们自己编写组件的时候显得更加重要.我相信大家一定对其有一定的了解.但是大家是否注意到了一个非常关键得细节问题呢?那就是在大家使用任何得组件的时候都需要通过属性浏览器给每一属性赋值,而且更加友好的是对于每种不同类型属性都会自己的形式.比如:数字类型.字符串类型是默认简单的输入的形式,而如Font.Color类型的属性则可以对话框或下拉列表框的形式.不知道大家是

C#中的域(field)和属性(property)

访问一个类的成员变量可以有两种方式:域.属性.域作为public类型的成员变量访问,而属性不能直接进行访问,必须通过访问器(accessors)进行. 域(field) 域(field) -域表示与对象或类相关联的变量. -域的声明中如果加上了readonly修饰符,表明该域为只读域.对于只读域我们只能在域的定义中和它所属类的构造函数中进行修改.在其他情况下,域是"只读"的. -static readonly的作用和#define.const的作用类似.区别是:const型表达式的值是

Spring如何利用propertyConfigurer类 读取.property数据库配置文件

1.Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中 一些动态设定的值(value),在XML中替换为占位该键($key$)的值, .properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性. 2.在Spring中,使用PropertyPlaceholderConfigurer可以在XM

项目启动时报错Element type "property" must be followed by either attri

项目启动时报错Element type "property" must be followed by either attribute specifications, ">" or "/>" 项目启动时,控制台报错,信息如下(有删减): Error creating bean with name 'supplierInfoDAO' defined in class path resource [conf/applicationCont

Delphi类的入门例子(4): property

//类单元unit Person; interface type TPerson = class(TObject) private FName: string; FAge: Integer; public procedure SetName(const strName: string); procedure SetAge(const intAge: Integer); property Name: string read FName write SetName; property Age: In