f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks

#

     摘要: 练习重写构造方法并完善总结。  阅读全文
posted @ 2015-05-02 11:01 swp 阅读(207) | 评论 (0)编辑 收藏

     摘要: 主要介绍了,简单的直接继承自NSObject的类如何重写构造方法  阅读全文
posted @ 2015-05-02 10:59 swp 阅读(154) | 评论 (0)编辑 收藏

     摘要: Ruby入门  阅读全文
posted @ 2015-05-02 01:16 swp 阅读(172) | 评论 (0)编辑 收藏

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 @property int age;
 5 - (void)test1;
 6 @end
 7 
 8 @implementation Person
 9 - (void)test1
10 {
11     NSLog(@"Person-test");
12 }
13 @end
14 
15 
16 
17 int main() {
18 
19     // 万能指针类型(能指向任何对象),id。
20     // id相当于NSObject * ,但是本质是不一样的。
21     id p = [Person new];
22     [p test1];
23     return 0;
24 }
25 
26 /*
27 id的认识
28 1.万能指针,相当于NSOjbect *,能指向任何OC对象。
29 2.id类型的定义
30 typedef struct objc_object {
31     Class isa;
32 } *id;
33 
34 // id是指向objc_object类型的指针。
35 */
posted @ 2015-05-01 23:30 swp 阅读(73) | 评论 (0)编辑 收藏

     摘要: 1.总结了@property与@synthesize和最新的@property新特性。  阅读全文
posted @ 2015-05-01 23:14 swp 阅读(125) | 评论 (0)编辑 收藏

 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 @interface Person : NSObject
 5 {
 6     int _age;
 7     int _height;
 8     int age;
 9 }
10 
11 
12 @property int age;
13 @property int height; // 完整写法,= _height制定setter、getter访问的成员变量是_height;
14 - (void)test;
15 @end
16 
17 @implementation Person
18 
19 @synthesize age = _age;
20 @synthesize height = _height;
21 - (void)test
22 {
23     NSLog(@"_age = %d, age = %d", _age, age);
24 }
25 @end
26 
27 int main() {
28 
29     Person * p = [Person new];
30     p.age = 10;
31     [p test];
32 
33     return 0;
34 }
35 
36 /*
37 @property与@synthesize的作用:
38 1.@property可以自动生成成员变量的setter与getter的声明。
39 2.@synthesize 可以自动生成getter与setter的实现。
40 
41 @synthesize的注意事项:
42 1.其set、get名取决与属性名而,访问的成员变量取决于 = XXXX;这句话所声明的。

43 */
posted @ 2015-05-01 15:53 swp 阅读(118) | 评论 (0)编辑 收藏


@interface中的成员变量作用域
/*
局部变量有作用域、全局变量有作用域。所以成员变量当然也有作用域。

成员变量的作用域分为四种:
@public\@private\@protected\@package
*/
/*
@public : 任何地方都能直接访问对象的成员变量。
@private : 只能在当前类中访问
@protected : 只能在当前类和其子类中访问,这是默认方法
@package :
注意事项:
1.需要注意的是这四个修饰符只能修饰成员变量,不能修饰方法。这和C++不一样。

*/
 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 @implementation Car : NSObject
 5 {
 6     int _age;
 7     @public
 8     int _fuck;
 9 }
10 
11 @end;
12 
13 int main() {
14 
15     Car * c = [Car new];
16     //c->_age = 10; // error: instance variable '_age' is private
17     c->_fuck = 20;
18     //NSLog(@"_age = %d", c->_age); // error: instance variable '_age' is private
19     NSLog(@"_fuck = %d", c->_fuck);
20     return 0;
21 }
22 
23 /*
24 总结:
25 在implementation的注意事项
26 1.默认为private属性,所以不能用->直接访问。
27 2.可以用成员变量修饰符。
28 
29 
30 分析:但是如果@implementation不是直接写在main函数前面,那么
31 其实我们是无法知道其提供给外部的这个对象有这种成员变量。不管其是否是@public
32 都无法访问,所以
33 3.写在@implementation的成员变量,如果这个@implementation不在main函数前面,那么相当于是没有的。
34 只有在@implementation前面,才用必要考虑1、2两条。
35 */


posted @ 2015-05-01 15:11 swp 阅读(90) | 评论 (0)编辑 收藏

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 {
 5     int _age;
 6 }
 7 
 8 - (void)setAge:(int)age;
 9 - (int)age;
10 @end
11 
12 @implementation Person
13 
14 - (void)setAge:(int)age
15 {
16     _age = age;
17     NSLog(@"调用了setAge方法");
18 }
19 - (int)age
20 {
21     NSLog(@"调用了age方法");
22     return _age;
23 }
24 @end
25 
26 int main() {
27 
28 
29     Person *p = [Person new];
30     p.age = 10;        // 当点语法作为左值调用setter方法
31     int a = p.age;    // 当点语法作为右值调用getter方法
32     NSLog(@"age = %i", a);
33     return 0;
34 }
35 
36 /*
37 输出
38 2015-05-01 14:40:44.890 a.out[1849:11976] 调用了setAge方法
39 2015-05-01 14:40:44.891 a.out[1849:11976] 调用了age方法
40 2015-05-01 14:40:44.891 a.out[1849:11976] age = 10
41 
42 
43 总结:
44 1.点语法的本质就是方法的调用
45 2.当作为左值调用setter方法,反之调用getter方法。
46 */2015/5/1下午2:42:39
posted @ 2015-05-01 14:43 swp 阅读(75) | 评论 (0)编辑 收藏

     摘要:   阅读全文
posted @ 2015-04-30 22:30 swp 阅读(98) | 评论 (0)编辑 收藏

     摘要: super关键字的总结三方面:作用、编程思想、使用注意  阅读全文
posted @ 2015-04-30 16:03 swp 阅读(89) | 评论 (0)编辑 收藏

仅列出标题
共10页: 1 2 3 4 5 6 7 8 9 Last