Mẹo C ++ truyền lớp cho hàm tạo

Mẹo Hướng dẫn C ++ truyền lớp cho hàm tạo Chi Tiết

Hoàng Trung Dũng đang tìm kiếm từ khóa C ++ truyền lớp cho hàm tạo được Update vào lúc : 2022-12-22 07:26:04 . Với phương châm chia sẻ Mẹo Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi Read tài liệu vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Admin lý giải và hướng dẫn lại nha.

Lớp được tạo chứa một biến thể hiện và một thuộc tính cho từng thành viên tài liệu Slice. Xem xét định nghĩa lớp sau

Lát cắt

class TimeOfDay     short hour;         // 0 - 23     short minute;       // 0 - 59     short second;       // 0 - 59     string format();    // Return time as hh:mm:ss

Trình biên dịch Slice tạo mã sau cho định nghĩa này

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end

Có một số trong những điều cần lưu ý về mã được tạo

Lớp được tạo @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 7 bắt nguồn từ @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8, là lớp cha của tất cả những lớp. Lưu ý rằng @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8 không in như @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end0. Nói cách khác, bạn không thể vượt qua một lớp mà proxy được yêu cầu và ngược lạiLớp được tạo chứa một thuộc tính cho từng thành viên tài liệu SliceLớp được tạo đáp ứng một phương thức @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end1 đồng ý một đối số cho từng thành viên tài liệu và nó đáp ứng hai hàm tạo tiện lợi in như cấu trúc và ngoại lệ

Quay lại đầu trang ^

Bắt nguồn từ @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8 trong Mục tiêu-C

Tất cả những lớp ở đầu cuối đều xuất phát từ một lớp cơ sở chung, 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8. Lưu ý rằng điều này rất khác với việc triển khai giao thức @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end0 (do proxy triển khai). Do đó, bạn không thể vượt qua một lớp mà proxy được mong đợi (và ngược lại) vì nhiều chủng loại cơ sở cho những lớp và proxy không tương thích

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8 định nghĩa một số trong những phương pháp

Mục tiêu-C

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end

Các phương thức được phân chia giữa giao thức và lớp

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8 vì những lớp hoàn toàn có thể là người phục vụ

Các phương thức của 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8 hoạt động và sinh hoạt giải trí như sau
    @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end8
    Phương thức này trả về @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end9 nếu đối tượng tương hỗ ID loại đã cho và @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 60 nếu không.
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 đáp ứng bài kiểm tra kĩ năng tiếp cận cơ bản cho lớp học. Nếu nó hoàn thành xong mà không đưa ra một ngoại lệ, thì lớp tồn tại và hoàn toàn có thể truy cập được. Lưu ý rằng @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 thường chỉ được gọi trên proxy cho một lớp hoàn toàn có thể ở xa vì luôn hoàn toàn có thể truy cập được một phiên bản lớp cục bộ (trong không khí địa chỉ của trình gọi).
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64
    Phương thức này trả về một chuỗi chuỗi đại diện cho tất cả những ID loại được đối tượng này tương hỗ, gồm có cả @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 65.
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 66
    Phương thức này trả về ID loại thời gian chạy thực tế cho một lớp. Nếu bạn gọi @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 66 thông qua một con trỏ tới một phiên bản cơ sở, ID loại được trả về là ID loại thực tế (hoàn toàn có thể có nguồn gốc hơn) của phiên bản.
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 68
    Phương thức này trả về ID loại tĩnh của một lớp.

    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 69
    Phương thức này gửi yêu cầu đến cho một người hầu. Nó được sử dụng trong quá trình triển khai thiết bị chặn công văn.
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 60
    Thời gian chạy băng gọi phương thức này trước khi sắp xếp lại trạng thái của đối tượng, tạo thời cơ cho lớp con xác thực những thành viên tài liệu đã khai báo của nó.
    @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61
    Thời gian chạy băng gọi phương thức này sau khi sắp xếp lại trạng thái của đối tượng. Một lớp con thường ghi đè hiệu suất cao này khi nó cần thực hiện khởi tạo tương hỗ update bằng phương pháp sử dụng những giá trị của những thành viên tài liệu đã khai báo của nó. @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 62
    Hàm này trả về đối tượng @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 63 nếu giá trị đã được cắt lát trong quá trình sắp xếp lại hoặc @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64 nếu không.

Quay lại đầu trang ^

Thành viên tài liệu lớp trong Objective-C

Theo mặc định, những thành viên tài liệu của những lớp được ánh xạ đúng chuẩn như đối với cấu trúc và ngoại lệ. đối với mỗi thành viên tài liệu trong định nghĩa Slice, lớp được tạo chứa một thuộc tính tương ứng

Quay lại đầu trang ^

Trình tạo lớp trong Mục tiêu-C

Các lớp đáp ứng phương thức 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end1 thông thường và một hàm tạo tiện lợi không còn tham số để thực hiện khởi tạo mặc định những biến thể hiện của đối tượng. Các phương thức này khởi tạo những biến thể hiện với bộ nhớ trống, với những ngoại lệ sau
    Một thành viên tài liệu chuỗi được khởi tạo thành một chuỗi rỗngMột thành viên tài liệu điều tra viên được khởi tạo cho điều tra viên đầu tiênThành viên tài liệu cấu trúc được khởi tạo thành giá trị được tạo mặc định

Nếu bạn khai báo những giá trị mặc định trong định nghĩa Slice của tớ, phương thức 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end1 và hàm tạo tiện lợi sẽ khởi tạo từng thành viên tài liệu bằng giá trị đã khai báo của nó thay thế

Ngoài ra, nếu một lớp có những thành viên tài liệu, nó sẽ đáp ứng một phương thức 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end1 và một hàm tạo tiện lợi đồng ý một đối số cho từng thành viên tài liệu. Điều này được cho phép bạn phân bổ và khởi tạo một thể hiện lớp trong một câu lệnh (thay vì trước tiên phải phân bổ và khởi tạo mặc định thể hiện rồi gán cho những thuộc tính của nó)

Đối với những lớp dẫn xuất, phương thức 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end1 và hàm tạo tiện lợi có một tham số cho từng thành viên tài liệu của lớp cơ sở, cộng với một tham số cho từng thành viên tài liệu của lớp dẫn xuất, theo thứ tự từ cơ sở đến lớp dẫn xuất. Ví dụ

Lát cắt

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 6

Điều này tạo ra

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 6

Quay lại đầu trang ^

Các lớp dẫn xuất trong Objective-C

Lưu ý rằng, trong ví dụ trước, dẫn xuất của những định nghĩa Slice được không thay đổi cho những lớp được tạo.

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 69 xuất phát từ @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 8 và @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 91 xuất phát từ @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 69. Điều này được cho phép bạn xử lý và vượt qua những lớp một cách đa hình. bạn luôn hoàn toàn có thể vượt qua một phiên bản @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 91 trong khi một phiên bản @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 69 được mong đợi

Quay lại đầu trang ^

Truyền những lớp dưới dạng tham số trong Objective-C

Các lớp được truyền bởi con trỏ, in như bất kỳ đối tượng Objective-C nào khác. Ví dụ: đây là một thao tác đồng ý

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 95 làm tham số trong và trả về @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 96

Lát cắt

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 9

Phương thức proxy tương ứng trông như sau

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 6

Để vượt qua một phiên bản null, bạn chỉ việc vượt qua 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 97

Quay lại đầu trang ^

  Hoạt động của những lớp trong Objective-C

Nếu bạn nhìn lại mã được tạo cho lớp

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 7, bạn sẽ nhận thấy rằng không còn tín hiệu nào đã cho tất cả chúng ta biết lớp đó có thao tác @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 99. Trái ngược với proxy, những lớp không thực hiện bất kỳ giao thức nào sẽ xác định hoạt động và sinh hoạt giải trí nào khả dụng. Điều này nghĩa là bạn hoàn toàn có thể thực hiện một phần những hoạt động và sinh hoạt giải trí sinh hoạt của một lớp. Ví dụ: bạn hoàn toàn có thể có một lớp Slice với năm thao tác được trả về từ sever cho máy khách. Nếu máy khách chỉ sử dụng một trong năm thao tác, mã phía máy khách chỉ việc triển khai một thao tác đó và hoàn toàn có thể để bốn thao tác còn sót lại không cần triển khai. (Nếu lớp thực hiện một giao thức bắt buộc, mã phía máy khách sẽ phải thực hiện tất cả những hoạt động và sinh hoạt giải trí sinh hoạt để tránh chú ý của trình biên dịch. )

Tất nhiên, bạn phải thực hiện những thao tác mà bạn thực sự định gọi. Ánh xạ hoạt động và sinh hoạt giải trí cho những lớp tuân theo ánh xạ phía sever cho hoạt động và sinh hoạt giải trí trên giao diện. nhiều chủng loại tham số và nhãn hoàn toàn giống nhau. (Xem Truyền tham số trong Mục tiêu-C để biết rõ ràng. ) Tóm lại, ánh xạ phía sever in như ánh xạ phía máy khách ngoại trừ đối với nhiều chủng loại có biến thể hoàn toàn có thể thay đổi và không thể thay đổi, chúng ánh xạ tới biến thể không thể thay đổi trong đó ánh xạ phía máy khách sử dụng biến thể hoàn toàn có thể thay đổi và ngược lại

Ví dụ: đây là cách chúng tôi hoàn toàn có thể triển khai hoạt động và sinh hoạt giải trí 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 99 của lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 của chúng tôi

Mục tiêu-C

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end2

Theo quy ước, việc triển khai những lớp có thao tác có cùng tên với lớp Slice có hậu tố

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 62. Làm điều này là không bắt buộc - bạn hoàn toàn có thể gọi lớp triển khai của tớ bất kể thứ gì bạn thích. Tuy nhiên, nếu bạn không thích sử dụng cách đặt tên ______462-hậu tố, chúng tôi khuyên bạn nên áp dụng một quy ước đặt tên khác và tuân theo quy ước đó một cách nhất quán

Lưu ý rằng 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64 bắt nguồn từ @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 7. Điều này là vì, như tất cả chúng ta sẽ thấy trong giây lát, thời gian chạy Ice sẽ khởi tạo một thực thể @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64 bất kể lúc nào nó nhận được một thực thể @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 qua dây và mong ước thực thể đó đáp ứng những thuộc tính của @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 7

Quay lại đầu trang ^

Class Factory trong Objective-C

Sau khi tạo một lớp ví dụ như 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64, chúng tôi có một triển khai và chúng tôi hoàn toàn có thể khởi tạo lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64, nhưng chúng tôi không thể nhận nó dưới dạng giá trị trả về hoặc dưới dạng tham số ngoài từ lệnh gọi thao tác. Để biết nguyên do tại sao, hãy xem xét giao diện đơn giản sau

Lát cắt

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end2

Khi một ứng dụng khách gọi thao tác 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end21, thời gian chạy Ice phải khởi tạo và trả về một phiên bản của lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64. Tuy nhiên, trừ khi chúng tôi nói với nó, Ice run time không thể biết một cách kỳ diệu rằng chúng tôi đã tạo một lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64 triển khai phương thức @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 99. Để được cho phép thời gian chạy Ice khởi tạo đúng đối tượng, chúng tôi phải đáp ứng cho một nhà máy sản xuất biết rằng lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 Slice được triển khai bởi lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64 của chúng tôi

Để đáp ứng thời gian chạy Ice với một nhà máy sản xuất giá trị cho lớp _______464 của chúng tôi, chúng tôi phải triển khai giao diện 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end28

Lát cắt

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 1

Trong Objective-C, giao diện này được tạo dưới dạng 

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end29. Thời gian chạy Ice gọi khối value factory khi nó cần khởi tạo một lớp @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61. Đây là một triển khai hoàn toàn có thể có của nhà máy sản xuất giá trị của chúng tôi

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 0

Khối block  được chuyển ID loại của lớp để khởi tạo. Đối với lớp 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 của chúng tôi, ID loại là @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end22. Quá trình triển khai của chúng tôi kiểm tra ID loại. nếu khớp với @protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end22, nó sẽ khởi tạo và trả về một đối tượng @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64. Đối với ID loại khác, nó xác định vì nó không biết phương pháp khởi tạo nhiều chủng loại đối tượng khác.

Khi sử dụng tính năng đếm tham chiếu tự động (ARC), bạn nên chỉ có thể định

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end25 trong định nghĩa khối. Không thiết yếu nếu bạn không sử dụng ARC nhưng nhà máy sản xuất của bạn không được tự động phát hành phiên bản trả về. Thời gian chạy băng thay mặt bạn đảm nhận những hoạt động và sinh hoạt giải trí sinh hoạt quản lý bộ nhớ thiết yếu

Đưa ra một triển khai của nhà máy sản xuất, ví dụ như triển khai mà chúng tôi đã chỉ định cho

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end26, chúng tôi phải thông báo cho Ice run time về sự tồn tại của nhà máy sản xuất

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 1

Bây giờ, bất kể lúc nào Ice run time cần khởi tạo một lớp có loại ID

@protocol ICEObject -(BOOL) ice_isA:(NSString*)typeId current:(ICECurrent*)current; -(void) ice_ping:(ICECurrent*)current; -(NSString*) ice_id:(ICECurrent*)current; -(NSArray*) ice_ids:(ICECurrent*)current; -(void) ice_dispatch:(id)request; @end @interface ICEObject : NSObject -(BOOL) ice_isA:(NSString*)typeId; -(void) ice_ping; -(NSString*) ice_id; -(NSArray*) ice_ids; +(NSString*) ice_staticId; -(void) ice_preMarshal; -(void) ice_postUnmarshal; -(id) ice_getSlicedData; @end22, nó sẽ gọi khối của chúng tôi, khối này sẽ trả về một thể hiện @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 64 cho Ice run time

Cuối cùng, hãy nhớ rằng nếu một lớp chỉ có những thành viên tài liệu, nhưng không còn hoạt động và sinh hoạt giải trí, thì bạn tránh việc phải tạo và đăng ký một nhà máy sản xuất giá trị để nhận những phiên bản của lớp đó. Bạn chỉ được yêu cầu đăng ký một nhà máy sản xuất giá trị khi một lớp có hoạt động và sinh hoạt giải trí

Quay lại đầu trang ^

Sử dụng một khuôn khổ để thực hiện những hoạt động và sinh hoạt giải trí sinh hoạt trong Objective-C

Một cách khác để đăng ký một nhà máy sản xuất giá trị là sử dụng khuôn khổ Mục tiêu-C để thực hiện những hoạt động và sinh hoạt giải trí sinh hoạt. Ví dụ: thay vào đó, chúng tôi hoàn toàn có thể đã triển khai phương thức 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 99 của tớ bằng phương pháp sử dụng một khuôn khổ

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 2

Trong trường hợp này, tránh việc phải xuất phát từ lớp 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 7 đã tạo vì chúng tôi đáp ứng triển khai @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 99 dưới dạng khuôn khổ. Cũng tránh việc phải đăng ký một nhà máy sản xuất giá trị. thời gian chạy Ice khởi tạo một đối tượng @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 7 khi một đối tượng @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 61 đến qua dây và phương thức @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 99 được tìm thấy trong thời gian chạy khi nó thực sự được gọi

Đây là một cách tiếp cận thay thế khả thi để thực hiện những hoạt động và sinh hoạt giải trí sinh hoạt của lớp. Tuy nhiên, hãy nhớ rằng, nếu việc triển khai hoạt động và sinh hoạt giải trí yêu cầu sử dụng những biến thể hiện không được định nghĩa như một phần của định nghĩa Slice của một lớp, thì bạn không thể sử dụng phương pháp này vì những khuôn khổ Mục tiêu-C không được cho phép bạn thêm những biến thể hiện vào một

Quay lại đầu trang ^

Sao chép những lớp trong Mục tiêu-C

Các lớp triển khai

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 15. Hành vi in như đối với những cấu trúc, đó là bản sao nông. Để minh họa điều này, hãy xem xét định nghĩa lớp sau

Lát cắt

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 3

Chúng tôi hoàn toàn có thể khởi tạo hai phiên bản của loại

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 16 như sau

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 4

Điều này tạo ra tình huống hiển thị dưới đây

C ++ truyền lớp cho hàm tạo
Hai phiên bản của loại

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 16

Bây giờ, tất cả chúng ta tạo một bản sao của nút đầu tiên bằng phương pháp gọi 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 18

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 5

Điều này tạo ra tình huống hiển thị ở đây

C ++ truyền lớp cho hàm tạo

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 16 trường hợp sau khi gọi @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 18 trên @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 01

Như bạn hoàn toàn có thể thấy, nút đầu tiên được sao chép, nhưng nút ở đầu cuối (được chỉ ra bởi biến thể hiện 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 02 của nút đầu tiên) không được sao chép;

Tham chiếu theo chu kỳ luân hồi trong Mục tiêu-C

Một điều cần lưu ý là những tham chiếu theo chu kỳ luân hồi Một trong những lớp. Ví dụ, tất cả chúng ta hoàn toàn có thể thuận tiện và đơn giản tạo một quy trình bằng phương pháp thực hiện những câu lệnh sau

Mục tiêu-C

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 6

Điều này làm cho biến thể hiện 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 02 của hai lớp trỏ vào nhau, tạo ra quy trình như phía dưới

C ++ truyền lớp cho hàm tạo
Hai nút có tham chiếu tuần hoàn

Không có vấn đề gì khi gửi biểu đồ lớp này làm tham số. Ví dụ: bạn hoàn toàn có thể chuyển 

@interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 01 hoặc @interface EXTimeOfDay : ICEObject     ICEShort hour;     ICEShort minute;     ICEShort second; @property(nonatomic, assign) ICEShort hour; @property(nonatomic, assign) ICEShort minute; @property(nonatomic, assign) ICEShort second; -(id) init:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; +(id) timeOfDay; +(id) timeOfDay:(ICEShort)hour minute:(ICEShort)minute second:(ICEShort)second; @end 08 làm tham số cho một thao tác và trong sever, thời gian chạy Ice sẽ xây dựng lại biểu đồ tương ứng một cách trung thực, duy trì chu kỳ luân hồi. Tuy nhiên, nếu sever trả về một biểu đồ như vậy từ một lệnh gọi thao tác dưới dạng giá trị trả về hoặc dưới dạng tham số ngoài, thì tất cả những phiên bản lớp là một phần của quy trình đều bị rò rỉ. Điều này cũng đúng ở phía người tiêu dùng. nếu bạn nhận được một biểu đồ như vậy từ một lời gọi hoạt động và sinh hoạt giải trí và không phá vỡ quy trình một cách rõ ràng, thì bạn sẽ rò rỉ tất cả những phiên bản tạo thành một phần của quy trình

Bởi vì rất khó để phá vỡ những chu kỳ luân hồi theo cách thủ công (và, về phía sever, đối với những giá trị trả về và tham số ngoài, không thể phá vỡ chúng), chúng tôi khuyên bạn nên tránh những tham chiếu theo chu kỳ luân hồi Một trong những lớp

Tải thêm tài liệu liên quan đến nội dung bài viết C ++ truyền lớp cho hàm tạo programming c++

Video C ++ truyền lớp cho hàm tạo ?

Bạn vừa tham khảo nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Video C ++ truyền lớp cho hàm tạo tiên tiến nhất

Chia Sẻ Link Down C ++ truyền lớp cho hàm tạo miễn phí

Quý khách đang tìm một số trong những Chia SẻLink Tải C ++ truyền lớp cho hàm tạo miễn phí.

Thảo Luận thắc mắc về C ++ truyền lớp cho hàm tạo

Nếu sau khi đọc nội dung bài viết C ++ truyền lớp cho hàm tạo vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha #truyền #lớp #cho #hàm #tạo