- /*
- @public 全局都可以访问
- @protected 只能在类内部和子类中访问
- @private 只能在类内部访问
- */
- #import <Foundation/Foundation.h>
- @interface Student : NSObject
- {
- @public
- NSString *_name;
- NSString *_banJi;
-
- @protected
- NSString *_xueHao;
-
- @private
- int _age;
- float _weight;
- }
- -(void)setAge:(int)age andWeight:(float)weight;
- -(void)info;
- @end
- @implementation Student
- -(void)setAge:(int)age andWeight:(float)weight{
- _age = age;
- _weight = weight;
- }
- -(void)info{
- NSLog(@"姓名 = %@",_name);
- NSLog(@"班级 = %@",_banJi);
- NSLog(@"年龄 = %d岁",_age);
- NSLog(@"体重 = %.2fkg",_weight);
- }
- @end
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- Student *stu=[Student new];
-
- //_name和_banJi用@public修饰,全局都可以访问
- stu->_name =@"张三";
- stu->_banJi=@"0826基础班";
-
- //_age和_weight用@private修饰,只能在类内部访问,所以使用函数传递值
- [stu setAge:23 andWeight:65.0f];
-
- //打印学生信息
- [stu info];
- }
- return 0;
- }
复制代码 继承还没有学习,@protected先留着.......
|
|