当前位置:网站首页>Class (2) and protocol
Class (2) and protocol
2022-07-25 09:36:00 【Hills and valleys】
List of articles
Chapter one OC The singleton model
Chapter two OC Object initialization of
The third chapter OC Such creation and agreement
Chapter four OC And main Function operation and basic concepts
Preface
This article introduces the creation and protocol of classes
One 、 class
What is a class? ?
Class is an important part of object-oriented , We can treat a class as a kind of custom data .
You can use classes to define variables . This type of variable is equivalent to a pointer type variable ;
Defining classes
Interface part : Define the member variables and methods contained in this class
Implementation part : Provide implementation for methods of classes
Class interface :
The realization of the class :
Two 、 agreement
The definition of protocol is similar to interface , Used to define the specifications that multiple classes should follow
The protocol does not provide any implementation

3、 ... and 、 Create a new one XiyouMobilePerson class
User Class interface :
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface User : NSObject
@property(nonatomic,copy) NSString*name;
@property(nonatomic,assign) int age;
@end
NS_ASSUME_NONNULL_END
User The realization of the class :
#import "User.h"
@implementation User
@synthesize name;
@synthesize age;
@end
Class interface
#import <Foundation/Foundation.h>
#import"User.h"
NS_ASSUME_NONNULL_BEGIN
@interface XiyouMobilePerson : User{
int iOS;
int web;
NSString* andriod;
NSString* server;
}
-(void)setiOS:(int)i;
-(void)setWeb:(int)w;
-(void)setAndriod:(NSString*)a;
-(void)setServer:(NSString*)s;
-(int)getiOS;
-(int)getWeb;
-(NSString*)getAndriod;
-(NSString*)getServer;
@end
NS_ASSUME_NONNULL_END
The realization of the class :
#import "XiyouMobilePerson.h"
@implementation XiyouMobilePerson
-(void)setiOS:(int)i{
if(i%2==1){
i = i + 1;
} else{
i = i - 1;
}
iOS = i;
}
-(void)setWeb:(int)w{
web = w;
}
-(void)setServer:(NSString*)s{
server = s;
}
-(void)setAndriod:(NSString *)a{
andriod = a;
}
-(int)getiOS{
return iOS;
}
-(int)getWeb{
return web;
}
-(NSString*)getServer{
return server;
}
-(NSString*)getAndriod{
return andriod;
}
@end
test :
#import"User.h"
#import"XiyouMobilePerson.h"
#import<Foundation/Foundation.h>
int main() {
@autoreleasepool {
XiyouMobilePerson* p = [[XiyouMobilePerson alloc]init];
[p setiOS:99];
[p setWeb:98];
[p setServer:@"97"];
[p setAndriod:@"96"];
NSLog(@"setiOS = %d,setWeb = %d,setServer = %@,setAndroid = %@",[p getiOS],[p getWeb],[p getServer],[p getAndriod]);
}
}
result :
Four 、 Create a new one Model class
Class interface :
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Model : NSObject
@property(nonatomic, stong) NSMutableArray* XiyouMobileArrey;
@end
NS_ASSUME_NONNULL_END
The realization of the class :
#import "Model.h"
@implementation Model
@synthesize XiyouMobileArrey;
@end
test :
#import"User.h"
#import"Model.h"
#import<Foundation/Foundation.h>
int main () {
@autoreleasepool {
Model* m = [[Model alloc]init];
m = @"iOS";
NSLog(@"%@",m);
}
}
give the result as follows :
5、 ... and 、 Create a new one Demand agreement
Demand.h
// Demand.h
// gy-OC.5.29
//
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol Demand <NSObject>
@required
-(int)calculate;
@optional
-(int)unnessary;
@end
NS_ASSUME_NONNULL_END
XiiyouMobilePerson.m
#import "XiyouMobilePerson.h"
#import"Demand.h"
@implementation XiyouMobilePerson
-(void)setiOS:(int)i{
if(i%2==1){
i = i + 1;
} else{
i = i - 1;
}
iOS = i;
}
-(void)setWeb:(int)w{
web = w;
}
-(void)setServer:(NSString*)s{
server = s;
}
-(void)setAndriod:(NSString *)a{
andriod = a;
}
-(int)getiOS{
return iOS;
}
-(int)getWeb{
return web;
}
-(NSString*)getServer{
return server;
}
-(NSString*)getAndriod{
return andriod;
}
-(int)calculate{
return iOS + web;
}
@end
test :
#import"XiyouMobilePerson.h"
#import"Demand.h"
#import<Foundation/Foundation.h>
int main(){
@autoreleasepool {
XiyouMobilePerson* p = [[XiyouMobilePerson alloc]init];
[p setiOS:99];
[p setWeb:98];
[p setServer:@"97"];
[p setAndriod:@"96"];
int a =[p calculate];
NSLog(@"iOS + web = %d",a);
}
}
result :
边栏推荐
- 机器学习 —— Sklearn包中StandardScaler()、transform()、fit()的详细介绍
- Click to hide the column in wechat applet, and then click to show it
- @2-1 CCF 2020-12-01 期末预测之安全指数
- *6-2 CCF 2015-03-3 节日
- Go基础4
- [code source] daily question - queue
- Laravel calls a third party to send mail (PHP)
- Data control language (DCL)
- Go foundation 2
- [HCTF 2018]admin
猜你喜欢
随机推荐
@3-1 CCF 2020-09-1 称检测点查询
关于C和OC
[GKCTF 2021]easynode
@3-2 CCF 2020-12-2 期末预测之最佳阈值
单例模式(Singleton)
数据分析之numpy基础包
Assignment 7.21 Joseph Ring problem and decimal conversion
[GYCTF2020]Ez_ Express
js中map()函数的使用
【代码源】每日一题 农田划分
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
~3 ccf 2022-03-2 出行计划
卷积神经网络的兴趣简单介绍
Read and write mongodb database files
cell的定义
UI——无限轮播图和分栏控制器
本地存储待办事项(在待办事项上进行改进)
Go基础3
基本的网络知识
【代码源】每日一题 三段式









