类的声明和实现语法

  1. //
  2. // main.m
  3. // ClassDemo 类
  4. //
  5. // Created by ZhaiKun on 2017/10/9.
  6. // Copyright © 2017年 ZhaiKun. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*
  10. 类:具有相同特征和行为的对象的集合
  11. 对象:个人的理解是 万物皆对象
  12. */
  13. //类的声明
  14. //Person 类名 首字母大写
  15. @interface Person : NSObject
  16. @end
  17. //类的实现
  18. @implementation Person
  19. @end
  20. int main(int argc, const char * argv[]) {
  21. @autoreleasepool {
  22. // insert code here...
  23. NSLog(@"Hello, World!");
  24. }
  25. return 0;
  26. }

类的属性/特征

  1. //
  2. // main.m
  3. // ClassDemo 类
  4. //
  5. // Created by ZhaiKun on 2017/10/9.
  6. // Copyright © 2017年 ZhaiKun. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*
  10. 类:具有相同特征和行为的对象的集合
  11. 对象:个人的理解是 万物皆对象
  12. */
  13. //类的声明
  14. //Person 类名 首字母大写
  15. @interface Person : NSObject
  16. {
  17. //类的属性定义在 { } 中
  18. NSString *name;
  19. int age;
  20. }
  21. @end
  22. //类的实现
  23. @implementation Person
  24. @end
  25. int main(int argc, const char * argv[]) {
  26. @autoreleasepool {
  27. // insert code here...
  28. NSLog(@"Hello, World!");
  29. }
  30. return 0;
  31. }

类属性的调用

  1. //
  2. // main.m
  3. // ClassDemo 类
  4. //
  5. // Created by ZhaiKun on 2017/10/9.
  6. // Copyright © 2017年 ZhaiKun. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*
  10. 类:具有相同特征和行为的对象的集合
  11. 对象:个人的理解是 万物皆对象
  12. */
  13. //类的声明
  14. //Person 类名 首字母大写
  15. @interface Person : NSObject
  16. {
  17. //类的属性定义在 { } 中
  18. @public
  19. NSString *name;
  20. int age;
  21. }
  22. @end
  23. //类的实现
  24. @implementation Person
  25. @end
  26. int main(int argc, const char * argv[]) {
  27. @autoreleasepool {
  28. Person *person = [Person new];//类的一个实例对象,类的每一个对象是各不相同的
  29. //person->name = @"类属性的调用";
  30. (*person).name = @"类属性的另一种调用方式";
  31. NSLog(@"%@", person->name);
  32. }
  33. return 0;
  34. }

类行为(方法或函数,就是某一个功能的实现)的声明、实现与调用

  1. //
  2. // main.m
  3. // ClassDemo 类
  4. //
  5. // Created by ZhaiKun on 2017/10/9.
  6. // Copyright © 2017年 ZhaiKun. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*
  10. 类:具有相同特征和行为的对象的集合
  11. 对象:个人的理解是 万物皆对象
  12. */
  13. //类的声明
  14. //Person 类名 首字母大写
  15. @interface Person : NSObject
  16. {
  17. //类的属性定义在 { } 中
  18. @public
  19. NSString *name;
  20. int age;
  21. }
  22. //方法的声明
  23. - (void)noParamMethod;//声明一个 返回值为空void 无参数 名为noParamMethod的方法
  24. //带有一个参数的方法
  25. - (void)oneParamMethod:(NSString *)nameParam;//声明一个 返回值为空void 参数类型为NSString * 参数名为name 名为oneParamMethod的方法
  26. //带多个参数的方法
  27. - (void)paramsMethod:(int)age1 :(int)age2;
  28. //在类方法的实现中,可以直接访问类的属性
  29. - (int)classSelfParamMethod;
  30. @end
  31. //类的实现
  32. @implementation Person
  33. //方法的实现
  34. - (void)noParamMethod{
  35. NSLog(@"noParamMethod方法的实现");
  36. }
  37. - (void)oneParamMethod:(NSString *)nameParam{
  38. nameParam = @"带有一个参数的方法";
  39. NSLog(@"%@", nameParam);
  40. }
  41. - (void)paramsMethod:(int)age1 :(int)age2{
  42. NSLog(@"多个参数的方法:%d", age1+age2);
  43. }
  44. - (int)classSelfParamMethod{
  45. age = 18;
  46. return age;
  47. }
  48. @end
  49. int main(int argc, const char * argv[]) {
  50. @autoreleasepool {
  51. Person *person = [Person new];//类的一个实例对象,类的每一个对象是各不相同的,类中的属性和方法必须通过类的实例对象进行调用
  52. //person->name = @"类属性的调用";
  53. (*person).name = @"类属性的另一种调用方式";
  54. NSLog(@"%@", person->name);
  55. //方法的调用
  56. [person noParamMethod];
  57. [person oneParamMethod:@""];
  58. [person paramsMethod:1 :2];
  59. NSLog(@"在方法中直接调用类的属性:%d", [person classSelfParamMethod]);
  60. }
  61. return 0;
  62. }