OC语法基础(5)—OC特有

这篇博客深入探讨了Objective-C的语法特性,包括点语法、访问控制符、类的本质、属性(property)的使用、自动生成私有实现、重写getter/setter、私有方法、id类型以及构造方法的应用,从基础到进阶,详细解析了OC中的关键概念。
摘要由CSDN通过智能技术生成

点语法

//  Girl.h

#import

 

@interface Girl : NSObject

{

    @public

    int _age;// 年龄

    NSString *_name; // 姓名

    NSString *_tel;// 电话

}

 

- (void)setAge:(int)age;

- (int)age;

 

- (void)setName:(NSString *)name;

- (NSString *)name;

 

- (void)setTel:(NSString *)tel;

- (NSString *)tel;

 

- (void)test;

@end

//  Girl.m

#import "Girl.h"

 

@implementation Girl

 

- (void)setAge:(int)age

{

    _age = age;

}

- (int)age

{

    return _age;

}

 

- (void)setName:(NSString *)name

{

    _name = name;

}

- (NSString *)name

{

    return _name;

}

 

- (void)setTel:(NSString *)tel

{

    _tel = tel;

}

- (NSString *)tel

{

    return _tel;

}

 

 

- (void)test

{

    NSLog(@"test");

}

- (NSString *)description

{

    NSString *str = [NSString stringWithFormat:@"age = %d, name = %@, tel = %@", _age, _name, _tel];

    return str;

}

@end

//  main.m

#import

#import "Girl.h"

 

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

{

 

    Girl *g = [Girl new];

//    直接给成员变量赋值

//    g->_age = 12;

//    g->_name = @"猪立叶";

//    g->_tel = @"13888888888";

//   

//    NSLog(@"age = %d", g->_age);

   

//    间接赋值

//    [g setAge:22];

//    [g setName:@"王老吉"];

//    [g setTel:@"13812346577"];

//    NSLog(@"age = %d", [g age]);

   

//    利用点语法

//    g.age = 23;

//    g.age == [g setAge:23];

//    其实点语法就是调用set发放,编译器在编译的时候会自动转换为set方法

//    g.name = @"加多宝";

//    g.tel = @"123456789";

//    NSLog(@"age = %d", g.age);// [g age];

   

//

//    NSLog(@"%@", g);

   

//    只用点语法来设置属性,和获得属性值,不能用来调用除setter、getter外的其它方法

//    g.test;

   

    return 0;

}

成员变量作用域访问控制符

//  Girl.h

#import "Person.h"

 

@interface Girl : Person

 

- (void)test;

@end

//  Girl.m

#import "Girl.h"

 

@implementation Girl

 

- (void)test

{

//    在子类中该可以访问父类中protected修饰的成员变量

    NSLog(@"_no = %d", _no);

   

//    在子类中该可以访问父类中public修饰的成员变量

    NSLog(@"_age = %d", _age);

   

//    在子类中该"不可以"访问父类中private修饰的成员变量

//    NSLog(@"_weight = %d", _weight);

}

@end

//  Person.h

#import

 

 

@interface Person : NSObject

{

//    默认情况下所有属性都是protected

    int _no;

   

   

    @public

    int _age;

   

    @private// 私有

    int _weight;

   

    @protected// 受保护的

    float _height;

}

 

- (void)test;

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

 

- (void)test

{

//    当属性是public的时候可以在本类的对象方法中直接访问

    NSLog(@"_age = %d",_age);

//    当属性是private的时候可以在本类的对象方法中直接访问

    NSLog(@"_weight = %d", _weight);

//    当属性是protected的时候可以在本类对象方法中直接访问

    NSLog(@"_height = %.1f", _height);

}

@end

//  main.m

#import

#import "Person.h"

#import "Girl.h"

 

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

{

 

//    Person   *p = [Person new];

//    当是public的是可以在其它文件中直接访问

//    p->_age = 20;

//    NSLog(@"age = %d", p->_age);

   

//    当是private的时候,在其它文件中不能直接访问

//    p->_weight = 50;

   

//    当是protected的时候,在其它文件中不能直接访问

//    p->_height = 1.75;

   

//    p->_no = 44;

//    [p test];

   

   

    Girl *g = [Girl new];

    [g test];

    return 0;

}

类的本质

OC语法基础(5)—OC特有

OC语法基础(5)—OC特有

//  Person.h

#import

 

@interface Person : NSObject

{

    @public

    int _age;

}

- (void)test;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (void)test

{

//    NSLog(@"Person test xxoo");

//    self其实就指向了isa

    NSLog(@"self = %p", self);

}

@end

//  Student.h

#import "Person.h"

 

@interface Student : Person

 

@end

//  Student.m

#import "Student.h"

 

@implementation Student

 

 

@end

//  Dog.h

#import

 

@interface Dog : NSObject

 

@end

//  Dog.m

#import "Dog.h"

 

@implementation Dog

 

@end

//  main.m

#import

#import "Person.h"

#import "Dog.h"

 

typedef struct

{

    int year;

    int month;

    int day;

}Date;

 

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

{

   

  

//    类的本质其实也是一个类对象

    Person *p = [Person new];

    [p test];

   

//     获取Person类的类对象

//     Class本质:typedef struct objc_class *Class;

    Class cs = [Person class];

    Person *p1 = [cs new];

    [p1 test];

//    无论创建多少个对象都是指向同一个类对象

    Person *p2 = [cs new];

 

    Dog *d = [Dog new];

    NSLog(@"xxxx");

   

   

 

   

    return 0;

}

 

property

//  Person.h

#import

 

@interface Person : NSObject

{

    int _age;// 年龄

    int _no;// 身份证号

    NSString *_name;// 姓名

}

 

@property NSString * name;

@property int no;

 

 

//property是一个编译器特性

@property int age;//注意:这个地方成员变量的名称写去掉下划线后的

 

 

 

//- (void)setAge:(int)age;

 

//- (int)age;

 

//- (void)setNo:(int)no;

//- (int)no;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (void)setAge:(int)age

{

    _age = age;

}

 

- (int)age

{

    return _age;

}

 

- (void)setNo:(int)no

{

    _no = no;

}

 

- (int)no

{

    return _no;

}

 

- (void)setName:(NSString *)name

{

    _name = name;

}

 

- (NSString *)name

{

    return _name;

}

//- (void)set_age:(int)_age

//{

//   

//}

//

//- (int)_age

//{

//   

//}

@end

//  main.m

#import

#import "Person.h"

 

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

{

 

    Person *p = [Person new];

    [p setAge:20];

    int a = [p age];

    NSLog(@"a = %d", a);

   

    [p setNo:10];

    [p setName:@"goeji"];

    NSLog(@"no = %d, name = %@", [p no], [p name]);

   

    return 0;

}

synthesize

//  Person.h

#import

 

@interface Person : NSObject

{

    @public

    int _age;// 年龄

    int age;

   

    int _no;// 身份证号

    NSString *_name;// 姓名

}

 

//@property NSString * name;

//@property int no;

@property int age;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

//@synthesize 用来自动生成get/set方法的实现

// @synthesize age 意思是给.h文件中名称叫做ageproperty生成实现

//@synthesize age = _age;

 

 

 

 

 

//@synthesize age = _no;

 

 

// 如果没有明确告诉synthesize后面的age要赋值给谁它就会赋值给和它同名的成员变量

// 但是不明确使用synthsize时(即只有propertyage赋值给_age

@synthesize age;

 

 

 

//- (void)setAge:(int)age

//{

//    _age = age;

//}

 

//- (int)age

//{

//    return _age;

//}

@end

//  main.m

#import

#import "Person.h"

 

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

{

 

    Person *p = [Person new];

    [p setAge:10];

//    NSLog(@"age = %d", [p age]);

//   

//    NSLog(@"age = %d, no = %d", p->_age, p->_no);

//

    [p setAge:22];

    NSLog(@"_age = %d, age = %d", p->_age, p->age);

    return 0;

}

 

 

property增强(自动生成私有实现)

//  Person.h

#import

 

@interface Person : NSObject

{

//    如果没有写成员变量,property会自动帮我们生成一个下划线开头的成员变量

//    @public

//    int _age;// 年龄

//    int age;

   

    @public

    int _no;

   

    @protected

    int _value;

   

    @private

    int _number;

}

 

 

@property int age;

 

 

- (void)personInfomation;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

//{

    int _age;

//    int _xxx;

//}

 

- (void)personInfomation

{

    NSLog(@"infomation age = %d", _age);

//    NSLog(@"xxx = %d", _xxx);

}

 

@end

//  main.m

#import

#import "Person.h"

 

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

{

 

    Person *p = [Person new];

    [p setAge:33];

//    NSLog(@"_age = %d", p->_age);

//    NSLog(@"age = %d", p->age);

   

    NSLog(@"age = %d", [p age]);

    [p personInfomation];

// 上面两句验证:如果没有手动声明成员变量,perperty会在.m文件中自动帮我们生成一个_开头的成员变量

    return 0;

}

重写set/get

//  Person.h

#import

 

@interface Person : NSObject

 

@property int age;

@property NSString * name;

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

// 重写set方法

// 1.当想控制外界传入的一些数据的合理性安全性的时候我们就需要手动实现set方法

- (void)setAge:(int)age

{

    _age = age;

    if (_age < 0) {

        _age = 0;

    }

}

 

// 2.当我们手动同时实现了set/get方法的时候,property就不会自动给我们生成成员变量

//- (int)age

//{

//    return _age;

//}

@end

//  main.m

#import

#import "Person.h"

 

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

{

 

    Person *p = [Person new];

//    [p setAge:12];

   

    [p setAge:-12];

    [p setName:@"goeji"];

    NSLog(@"age = %d, name = %@", [p age], [p name]);

    return 0;

}

OC私有方法

//  Person.h

#import

 

@interface Person : NSObject

 

//- (void)test;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

// 只有实现,没有声明我们称之为私有方法

- (void)test

{

    NSLog(@"- test");

}

@end

//  main.m

#import

#import "Person.h"

 

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

{

 

    Person *p = [Person new];

    [p test];

    return 0;

}

id类型

       万能指针类型

       相当于NSObject *

数据类型的共同特性

Char float doubl int BOOL NSObject *

1.都能够声明称变量

2.都能够做位方法的参数传递,也作为方法得 返回值

3.都能够声明称类的成员变量

Id var = ?

-    (void)method idvar;

-    (id)method idvar;

-    @property id var;

 

 

 

 

 

//  Person.h

#import

 

@interface Person : NSObject

 

@property int age;

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

@end

//  main.m

#import

#import "Person.h"

 

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

{

//    Person *p = [Person new];

//    [p setAge:20];

//    NSLog(@"age = %d", [p age]);

   

   

//      id == NSObject *

//    id的本质: typedef struct objc_object *id;

//    如果用id类型接收一个对象,调用对象特有方法不用进行强制类型转换

//    id 是一个万能指针,可以用来指向任何对象, 注意id后面不要加*

    id p3 = [Person new];

    [p3 setAge:44];

    NSLog(@"age = %d", [p3 age]);

   

//    double double

//    id _id ID

   

    return 0;

}

 

构造方法基本概念

//  Person.h

#import

 

@interface Person : NSObject

 

@property int age;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

 

// 优化init方法

- (id)init

{

    if (self = [super init]) {

        _age = 30;

    }

    return  self;

}

 

@end

//  main.m

#import

#import "Person.h"

 

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

{

   

//    [Person new];

   

    // 开辟存储空间,会返回一个没有初始化的对象

    Person *p1 = [Person alloc];

    // 对对象中的成员变量进行初始化,返回一个初始化后的对象

    Person *p2 = [p1 init];

   

//    [p2 setAge:23];

//    NSLog(@"age = %d", [p2 age]);

//    开发中常用写法

//    如果想让对象一出生它的某些属性就是一些固定的值,我们可以重写init方法

    Person *p3 = [[Person alloc] init];

//    [p3 setAge:18];

    NSLog(@"age = %d", [p3 age]);

   

    Person *p4 = [[Person alloc]init];

//    [p4 setAge:18];

    NSLog(@"age = %d", [p4 age]);

   

    return 0;

}

构造方法应用场景

//  Gun.h

#import

 

@interface Gun : NSObject

//{

//    int _bulletCount;// 子弹数

//}

 

@property int bulletCount;

// 射击方法

- (void)shoot;

 

@end

//  Gun.m

#import "Gun.h"

 

@implementation Gun

 

 

- (id)init

{

    if (self = [super init]) {

        _bulletCount = 10;

    }

    return self;

}

- (void)shoot

{

    if (_bulletCount > 0) {

        _bulletCount--;

         NSLog(@"剩余子弹 %d", _bulletCount);

    }else

    {

        NSLog(@"请上子弹");

    }

}

@end

//  Police.h

#import

#import "Gun.h"

 

@interface Police : NSObject

//{

//    Gun *_gun;//

//}

 

@property Gun * gun;

 

// 开枪

- (void)fire;

@end

//  Police.m

#import "Police.h"

#import "Gun.h"

 

@implementation Police

 

 

- (id)init

{

//    注意点:初学者记住不要写成(self == [super init](错误写法)

    if (self = [super init]) {

//        开发当中自己的事情自己干,不要多管闲事

        _gun = [[Gun alloc]init];

//        [_gun setBulletCount:10];

    }

    return  self;

}

 

- (void)fire

{

//     oc 当中使用nil调用方法(发送消息),不会报错

    [_gun shoot];

}

 

 

@end

//  main.m

#import

#import "Police.h"

 

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

{

 

 

    Police *p = [Police new];

   

//    Gun *gun = [Gun new];

//    [gun setBulletCount:10];

//    [p setGun:gun];

   

    [p fire];

   

    Police *p2 = [[Police alloc] init];

   

//    Gun *gun2 = [Gun new];

//    [gun2 setBulletCount:10];

//    [p2 setGun:gun2];

   

    [p2 fire];

 

   

    return 0;

}

 

 

自定义构造方法

//  Person.h

#import

 

typedef enum

{

    kGenderMale,

    kGenderFemale

}Gender;

 

@interface Person : NSObject

 

@property int age;

 

@property Gender gender;

 

- (id)initWithAge:(int)age;

 

- (id)initWithAge:(int)age andGender:(Gender)gender;

 

- (void)infomation;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

 

- (id)init

{

    if (self = [super init]) {

        _age = 18;

    }

    return self;

}

 

 

 

- (id)initWithAge:(int)age

{

    self = [super init];

    if (self) {

//        把传入的年龄赋值给成员变量

        _age = age;

    }

    return self;

}

 

- (id)initWithAge:(int)age andGender:(Gender)gender

{

    self = [super init];

    if (self) {

        _age = age;

        _gender = gender;

    }

    return  self;

}

 

- (void)infomation

{

    NSLog(@"age = %d gender = %d", _age, _gender);

}

@end

//  main.m

#import

#import "Person.h"

 

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

{

 

//    Person *p = [[Person alloc] init];

   

    Person *p = [[Person alloc]initWithAge:20];

    [p infomation];

   

//    当想让某个对象一出生就拥有指定的属性的时候就可以使用自定义构造方法

    Person *p1 = [[Person alloc]initWithAge:38 andGender:kGenderFemale];

    [p1 infomation];

   

    Person *p2 = [[Person alloc]init];

    [p2 setAge:48];

    [p2 setGender:kGenderFemale];

    [p2 infomation];

    return 0;

}

继承中自定义构造方法

OC语法基础(5)—OC特有

//  Person.h

#import

 

@interface Person : NSObject

//{

//    int _age;

//}

@property int age;

 

- (id)initWithAge:(int)age;

 

- (void)infomation;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (id)init

{

    if (self = [super init]) {

        _age = 1;

    }

   

    return self;

}

 

- (id)initWithAge:(int)age

{

    if (self = [super init]) {

        _age = age;

    }

    return self;

}

 

 

 

 

- (void)infomation

{

    NSLog(@"age = %d", _age);

}

@end

//  Student.h

#import "Person.h"

 

@interface Student : Person

 

@property NSString * name;

 

- (id)initWithAge:(int)age andName:(NSString *)name;

@end

//  Student.m

#import "Student.h"

 

@implementation Student

 

 

 

 

 

- (id)initWithAge:(int)age andName:(NSString *)name

{

    self = [super initWithAge:age];

    if (self) {

        _name = name;

    }

    return self;

}

 

 

- (void)infomation

{

    [super infomation];

    NSLog(@"name = %@", _name);

}

@end

//  main.m

#import

#import "Person.h"

#import "Student.h"

 

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

{

 

//    Person *p = [[Person alloc]init];

//    [p infomation];

   

//    Person *p = [[Person alloc]initWithAge:23];

//    [p infomation];

//   

//    Student *stu = [[Student alloc]initWithAge:44];

//    [stu infomation];

   

    Student *stu2 = [[Student alloc]initWithAge:68 andName:@"oooxxx"];

    [stu2 infomation];

   

    return 0;

}

 

类方法创建对象(难点)

//主要演示 如何 定义类方法创建Person对象(要让它的子类也能用) 及注意点

//  Person.h

#import

 

@interface Person : NSObject

 

@property int age;

 

 

// 定义类方法创建Person对象

// 一般开发中会提供一个对象方法和一个类方法用于创建初始化对象

// 约定:只要是用于创建对象的类方法,方法名称和类名一致(首字母小写)

 

+ (id)person;

+ (id)personWithAge:(int)age;

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

+ (id)person

{

//    Person *p1 = [Person alloc];

//    Person *p2 = [p1 init];

//    return p2;

   

//    return [[Person alloc]init];

//    self 谁调用就代表谁

    return [[self alloc]init];

}

 

 

 

+ (id)personWithAge:(int)age

{

    Person *p = [self person];

    [p setAge:age];

    return p;

}

 

 

@end

//  Student.h

 

#import "Person.h"

 

@interface Student : Person

 

@property NSString * name;

@end

//  Student.m

#import "Student.h"

 

@implementation Student

 

@end

//  main.m

#import

#import "Person.h"

#import "Student.h"

 

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

{

   

//    Person *p = [Person new];

//    Person *p2 = [[Person alloc]init];

   

   

   

   

   

    Student *stu2 = [Student personWithAge:33];

    [stu2 setName:@"tuhao"];

    NSLog(@"age = %d", [stu2 age]);

     NSLog(@"name = %@", [stu2 name]);

    return 0;

}

类的互引用(需要再理解)

//  Car.h

#import

//#import "Person.h" 如果在A类中导入了B,B类中又导入了A,就会造成循环引用

@class Person; //仅仅用于告诉编译Person是一个类并没有导入person的头文件,也就是说在本文件中用到person类中的

               // 元素时会报错的,在.m中使用到改类的时候必须导入改类的头问题(声明)

 

@interface Car : NSObject

 

- (void)run:(Person *)person  andAddress:(NSString *)address;

@end

//  Car.m

#import "Car.h"

//.m中使用到改类的时候必须导入改类的头问题(声明)

#import "Person.h"

 

 

@implementation Car

 

- (void)run:(Person *)person  andAddress:(NSString *)address;

{

    [person run];

    NSLog(@"%@ 开车到 %@ ",person , address);

}

@end

//  Person.h

#import

#import "Car.h"

 

@interface Person : NSObject

{

    Car *car;// 让人拥有一辆车

}

 

@property NSString * name;

- (void)run;

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (void)run

{

    NSLog(@"风一样的女子");

}

 

- (NSString *)description

{

    return _name;

}

@end

//  main.m

#import

#import "Person.h"

#import "Car.h"

 

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

{

 

    Car *c = [[Car alloc]init];

    Person *p = [[Person alloc]init];

    [p setName:@"lee"];

    [c run:p andAddress:@"马来西亚"];

   

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值