当前位置:网站首页>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 :
边栏推荐
猜你喜欢
![[GYCTF2020]Node Game](/img/8d/7e6c2fb2a0359298fbcc1cd8544710.png)
[GYCTF2020]Node Game

Data control language (DCL)

Data preprocessing

Analysis of five data structure principles of redis

数据预处理

数据查询语言(DQL)

类(2) 和 协议

@5-1 CCF 2019-12-1 报数

OC--Foundation--数组

How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
随机推荐
What are stand-alone, cluster and distributed?
最短路问题 Bellman-Ford(单源最短路径)(图解)
对象数据如何转化成数组
数据分析之numpy基础包
laravel 调用第三方 发送邮件 (php)
A brief introduction to the interest of convolutional neural networks
数据预处理
CoreData存储待办事项
@5-1 CCF 2019-12-1 报数
&lt; T&gt; Generic method demonstration
@4-1 CCF 2020-06-1 线性分类器
Numpy- array属性、改变形状函数、基本运算
【代码源】每日一题 添加括号
Idea practical tips --- now change pom.xml (red) to pom.xml (blue)
matplotlib数据可视化三分钟入门,半小时入魔?
~2 ccf 2022-03-1 未初始化警告
Laravel calls a third party to send mail (PHP)
【代码源】每日一题 非递减01序列
【代码源】每日一题 分数拆分
Swagger2显示get接口有问题,加注解就能解决