当前位置:网站首页>类(2) 和 协议
类(2) 和 协议
2022-07-25 09:21:00 【山河丘壑】
系列文章目录
第一章 OC之单例模式
第二章 OC之对象初始化
第三章 OC之类的创建和协议
第四章 OC之main函数的操作和基础概念
前言
本篇介绍类的创建和协议
一、类
什么是类?
类是面向对象的重要内容,我们可以把类当作一种自定义数据。
可以使用类来定义变量。这种类型的变量相等于指针类型的变量;
定义类
接口部分:定义该类包含的成员变量和方法
实现部分:为类的方法提供实现
类的接口:
类的实现:
二、协议
协议的定义类似于接口,用于定义多个类应遵守的规范
协议不提供任何实现

三、新建一个XiyouMobilePerson类
User类的接口:
#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类的实现:
#import "User.h"
@implementation User
@synthesize name;
@synthesize age;
@end
类的接口
#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
类的实现:
#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
测试:
#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]);
}
}
结果:
四、新建一个Model类
类的接口:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Model : NSObject
@property(nonatomic, stong) NSMutableArray* XiyouMobileArrey;
@end
NS_ASSUME_NONNULL_END
类的实现:
#import "Model.h"
@implementation Model
@synthesize XiyouMobileArrey;
@end
测试:
#import"User.h"
#import"Model.h"
#import<Foundation/Foundation.h>
int main () {
@autoreleasepool {
Model* m = [[Model alloc]init];
m = @"iOS";
NSLog(@"%@",m);
}
}
结果如下:
五、新建一个Demand协议
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
测试:
#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);
}
}
结果:
边栏推荐
- 【代码源】每日一题 国家铁路
- cf #785(div2) C. Palindrome Basis
- Thick willow dustpan, thin willow bucket, who hates reptile man? Asynchronous synergism, half a second to strip away a novel
- @5-1 CCF 2019-12-1 报数
- 【代码源】每日一题 农田划分
- Read and write mongodb database files
- Neural network method -- Boston house price (regression problem)
- sqli-labs Basic Challenges Less1-10
- Two Sum
- *7-1 CCF 2015-09-1 数列分段
猜你喜欢

Data query language (DQL)

粗柳簸箕细柳斗,谁嫌爬虫男人丑 之 异步协程半秒扒光一本小说

~3 ccf 2022-03-2 出行计划

Idea hot deployment

数据控制语言(DCL)

Week summary

Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground

变量名可以用中文?直接把人干蒙了

作业7.15 shell脚本
![[GYCTF2020]Ez_ Express](/img/ce/02b90708f215715bb53cacfd4c21f0.png)
[GYCTF2020]Ez_ Express
随机推荐
Redis string 结构命令
~2 ccf 2022-03-1 未初始化警告
微信小程序中的列点击隐藏,再点击显示
Go foundation 1
梦想启航(第一篇博客)
Go foundation 2
*6-1 CCF 2015-03-2 数字排序
Detailed explanation of the use of nanny scanner class
Redis set 结构命令
数据查询语言(DQL)
Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground
[GKCTF 2021]easynode
Numpy- array属性、改变形状函数、基本运算
TCP network application development process
Redis list 结构命令
sqli-labs安装 环境:ubuntu18 php7
*7-1 CCF 2015-09-1 数列分段
@3-2 CCF 2020-12-2 期末预测之最佳阈值
Deep understanding of static keyword
Dynamically add multiple tabs and initialize each tab page