@interface和category和extension小结

本文介绍了Objective-C中Category的概念及其用途,通过实例展示了如何为现有类添加新方法,对比了Category与Extension的区别。

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

注意:category是一种机制,没有关键字@category

Category的作用,它不需要使用子类(Subclassing)机制,就允许向一个类文件中添加新的方法声明,并且在类实现的文件中的同一个名字下定义这些方法。

 

定义category的过程大致可分为以下几个步骤: 

    第一步、创建一个带有接口的新文件,即创建已有类,命名惯例:ClassName+CategoryName,注意扩展的类必须是已经存在的,例如下面扩展的NSString 

    第二步、在新文件中添加需要扩展的方法及方法的实现,即需要添加的分类。注意只能添加方法,不能添加变量。  

 

写了一个简单的例子,为NSString类添加一个方法,一个NSString对象就可以调用这个方法了。

.h文件

 

#import <Foundation/Foundation.h>
@interface NSString(Hello)
- (void) helloMethod;
@end

 

 .m文件

 

#import "NSStringHello.h"
@implementation NSString(Hello)
- (void) helloMethod{
    NSLog(@"hello ,this is a ”@cantegory“ test");
}
@end

 

 为什么对类进行扩展的feature被称作category?该名称应该反映了某种初衷,那就是当某个类比较复杂庞大时,我们可以对它的功能(也就是成员函数)进行分类,并且把它们放入不同的文件加以实现。

 

 

 

CategoryExtension都是用来给已定义的类增加新的内容的。

Category和原有类的耦合更低一些,声明和实现都可以写在单独的文件里。但是只能为已定义类增加Method,而不能加入instance variable。

Extension耦合比较高,声明可以单独写,但是实现必须写在原有类的@implementation中。可以增加Method和instance variable。

Extension给人感觉更像是在编写类时为了封装之类的特性而设计,和类是同时编写的。而category则是在用到某一个framework中的类时临时增加的特性。

Extension的一个特性就是可以redeclare一个instance variable,将之从readonly改为对内readwrite.

 

quding:使用Extension可以更好的封装类,在h文件中能看到的都是对外的接口,其余的instance variable和对内的@property等都可以写在Extension,这样类的结构更加清晰。

 

 

(base) hxwang@raspberrypi:~/realsense_build/librealsense-master/build/wrappers/python $ sudo pip install . --break-system-packages Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Processing /home/hxwang/realsense_build/librealsense-master/build/wrappers/python Preparing metadata (setup.py) ... done Building wheels for collected packages: pyrealsense2 Building wheel for pyrealsense2 (setup.py) ... done Created wheel for pyrealsense2: filename=pyrealsense2-2.55.1-py3-none-any.whl size=520734 sha256=539e7b9fff0c8de7d6eb732ae8a7755de26c5f44367fabf225fdaeee429075ef Stored in directory: /tmp/pip-ephem-wheel-cache-90lin6tz/wheels/c2/26/b6/af813fa561492a8526f6a535300afb985818e02a66048d71f2 Successfully built pyrealsense2 Installing collected packages: pyrealsense2 Attempting uninstall: pyrealsense2 Found existing installation: pyrealsense2 2.55.1 Uninstalling pyrealsense2-2.55.1: Successfully uninstalled pyrealsense2-2.55.1 Successfully installed pyrealsense2-2.55.1 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv (base) hxwang@raspberrypi:~/realsense_build/librealsense-master/build/wrappers/python $ python -c "import pyrealsense2 as rs; print(dir(rs))" ['BufData', 'D400', 'D500', 'DEPTH', 'L500', 'SR300', 'STAEControl', 'STAFactor', 'STCensusRadius', 'STColorControl', 'STColorCorrection', 'STDepthControlGroup', 'STDepthTableControl', 'STHdad', 'STRauColorThresholdsControl', 'STRauSupportVectorControl', 'STRsm', 'STSloColorThresholdsControl', 'STSloPenaltyControl', 'T200', 'TRACKING', '__doc__', '__file__', '__full_version__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'align', 'any', 'any_intel', 'auto_calibrated_device', 'calib_target_type', 'calibration_change_device', 'calibration_status', 'calibration_type', 'camera_info', 'color_sensor', 'colorizer', 'composite_frame', 'config', 'context', 'debug_protocol', 'debug_stream_sensor', 'decimation_filter', 'depth_frame', 'depth_sensor', 'depth_stereo_sensor', 'device', 'device_calibration', 'device_list', 'disparity_frame', 'disparity_transform', 'distortion', 'enable_rolling_log_file', 'event_information', 'extension', 'extrinsics', 'filter', 'filter_interface', 'firmware_log_message', 'firmware_log_parsed_message', 'firmware_logger', 'fisheye_sensor', 'format', 'frame', 'frame_metadata_value', 'frame_queue', 'frame_source', 'hdr_merge', 'hole_filling_filter', 'intrinsics', 'l500_visual_preset', 'log', 'log_message', 'log_severity', 'log_to_callback', 'log_to_console', 'log_to_file', 'matchers', 'max_usable_range_sensor', 'motion_device_intrinsic', 'motion_frame', 'motion_sensor', 'motion_stream', 'motion_stream_profile', 'non_intel', 'notification', 'notification_category', 'option', 'option_from_string', 'option_range', 'option_type', 'option_value', 'options', 'options_list', 'pipeline', 'pipeline_profile', 'pipeline_wrapper', 'playback', 'playback_status', 'pointcloud', 'points', 'pose', 'pose_frame', 'pose_sensor', 'pose_stream', 'pose_stream_profile', 'processing_block', 'product_line', 'quaternion', 'recorder', 'region_of_interest', 'reset_logger', 'roi_sensor', 'rs2_deproject_pixel_to_point', 'rs2_fov', 'rs2_project_color_pixel_to_depth_pixel', 'rs2_project_point_to_pixel', 'rs2_transform_point_to_point', 'rs400_advanced_mode', 'rs400_visual_preset', 'save_single_frameset', 'save_to_ply', 'sensor', 'sequence_id_filter', 'serializable_device', 'software_device', 'software_motion_frame', 'software_notification', 'software_pose_frame', 'software_sensor', 'software_video_frame', 'spatial_filter', 'stream', 'stream_profile', 'sw_only', 'syncer', 'temporal_filter', 'terminal_parser', 'texture_coordinate', 'threshold_filter', 'timestamp_domain', 'units_transform', 'updatable', 'update_device', 'vector', 'vertex', 'video_frame', 'video_stream', 'video_stream_profile', 'wheel_odometer', 'yuy_decoder'] (base) hxwang@raspberrypi:~/realsense_build/librealsense-master/build/wrappers/python $ cd (base) hxwang@raspberrypi:~ $ cd yolov5 (base) hxwang@raspberrypi:~/yolov5 $ python 3D_rappy.py Traceback (most recent call last): File "/home/hxwang/yolov5/3D_rappy.py", line 16, in <module> pipeline = rs.pipeline() ^^^^^^^^^^^ AttributeError: module 'pyrealsense2' has no attribute 'pipeline' (base) hxwang@raspberrypi:~/yolov5 $
最新发布
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值