oc 点语法 / KVC /readwrite/readonly/

这篇博客介绍了Objective-C中的点语法、Key-Value Coding(KVC)以及属性的使用。通过示例代码展示了如何使用点语法进行对象属性的设置和获取,以及如何利用KVC进行动态赋值和取值。同时,文章讨论了属性的`readwrite`和`readonly`特性,并提供了`StudentOO`类的详细实现,包括属性的原子性和语义特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


main.m



#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"
#import "CollegeStudent.h"
#import "Animal.h"
#import "StudentOO.h"
#import "Book.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /*
        //property
        
        Person *person=[[Person alloc]initWithName:@"hiphop" Age:23];
    
//        [person setName:@"miko"];
        
        person.name=@"zhufu";  //[person setName:@"zhufu"];
        
        //只要符合setter和getter这种格式,就可以用点语法,点出来
        //.点语法与属性没有半毛钱关系。
        //点语法在等号左侧 是setter方法
        //          右侧 是getter方法

      
        
        Student *student=[[Student alloc]init];
        student.name=@"wa";
        student.gender=@"male";
        
        //
        CollegeStudent *cstudent=[CollegeStudent colleageName:@"tian" Age:23 Score:98.8];
        //name是只读的。
    
        cstudent.age=12;
      
        
        cstudent.face=@"笑脸";
       
        
        //
        Animal *monkey=[[Animal alloc]init];
        monkey.name=@"tian";
       
        [monkey sayHi];
        
        */
        
        //点语法
        StudentOO *student=[StudentOO studentOOName:@"bella" Gender:@"female" Age:21 Number:39 Score:90];
        //初始化对象--开辟空间
        Book *bookName=[[Book alloc]init];
        bookName.bookname=@"人性的弱点";
        student.book=bookName;
   
        
        //KVC===key-value-coding
        
        StudentOO *student1=[[StudentOO alloc]init];
        
        student1.name=@"";
        //forKey
        [student1 setValue:@"tijm" forKey:@"name"];
       
        
        [student1 setValue:@"male" forKey:@"gender"];
        //value值 是一个任意对象,记住是对象。
        [student1 setValue:@23 forKey:@"age"];
        [student1 setValue:@45 forKey:@"number"];
        [student1 setValue:@99 forKey:@"score"];
        NSLog(@"%@",[student1 valueForKey:@"name"]);
       
        //未定义的,用于

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值