//基础类库头文件
//<>表示搜索顺序从XCode系统路径中优先搜索
//“”表示搜索顺序从新建本地项目中WKRect项目中优先搜索
#import <Foundation/Foundation.h>
//类的声明
@interface WKRect : NSObject //NSObject是所有OC中类的基础类
{
//定义公有成员变量
@public
//受到保护的成员变量
@protected
//成员变量定义
//左上角坐标x
int xPos ;
//左上角坐标y
int yPos ;
//矩形宽度
int width ;
//矩形高度
int height;
//私有的成员变量
@private
}
//只要声明在头文件中的方法
//默认都为受保护的成员变量
//声明一个方法,设置xPos的值
-(void) setXPosValue:(int) value ;
//声明多个参数的方法,
//方法名包括两部分setXYValue: withYValue:
//除第一部分外后面命名的可以省略,但:不可省,如setXYValue::
//该方法包含两个参数,xValue和 yValue,参数类型都为int
-(void) setXValue:(int) xValue withYValue:(int) yValue;
//声明设置YPos值的方法
-(void) setYPosValue:(int) value ;
//声明设置矩形宽度的方法
-(void) setWidthValue:(int) value;
//声明设置矩形高度的方法
-(void) setHeightValue:(int) value;
//获得XPos值
-(int) getXPosValue ;
//获得YPos值
-(int) getYPosValue ;
//获得矩形宽度值
-(int) getWidthValue;
//获得矩形高度值
-(int) getHeightValue;
@end
//类的实现
#import "WKRect.h"
@implementation WKRect
//函数现实X设置函数
-(void) setXPosValue:(int)value
{
xPos = value ;
}
//实现yPos设置函数
-(void) setYPosValue:(int)value
{
yPos = value ;
}
//实现宽度设置函数
-(void) setWidthValue:(int)value
{
width = value ;
}
//实现高度设置函数
-(void) setHeightValue:(int)value
{
height = value ;
}
-(void)setXValue:(int)xValue withYValue:(int)yValue{
xPos = xValue;
yPos = yValue;
}
//实现getX函数
-(int) getXPosValue
{
return xPos ;
}
//实现getY函数
-(int) getYPosValue
{
return yPos ;
}
//实现getWidth函数
-(int) getWidthValue
{
return width ;
}
//实现getHeight函数
-(int) getHeightValue
{
return height ;
}
@end
#import <Foundation/Foundation.h>
#import "WKRect.h"
int main(int argc, const char * argv[])
{
//OC关键字,处理代码模块内存管理关键字,
@autoreleasepool
{
//创建一个矩形对象,通过alloc函数创建对象并且分配内存
WKRect* rect1 = [WKRect alloc] ;
//通过init函数对对象进行初始化
rect1 = [rect1 init] ;
//rect1 = [[WKRect alloc] init]; //一般采用合并写法
[rect1 setXPosValue: 5 ] ;
[rect1 setYPosValue: 10 ];
[rect1 setWidthValue: 10 ];
[rect1 setHeightValue: 20 ];
int xPos = [rect1 getXPosValue];
int yPos = [rect1 getYPosValue];
int width = [rect1 getWidthValue];
int height = [rect1 getHeightValue];
NSLog(@"xPos = %d",xPos);
NSLog(@"yPos = %d",yPos);
NSLog(@"width = %d",width);
NSLog(@"height = %d",height);
}
return 0;
}
//从Objective-C 2.0开始,可自动生成set方法和get方法(统称存储器方法)
//第一步在接口中通过@peroperty关键字定义需要自动生成存储器方法的实例变量
//第二步,在类实现中用@synthesize指定实例变量,编译器便可自动生成存储器方法。
//--------成员变量自动存储器用法--------
#import <Foundation/Foundation.h>
@interface WK_student : NSObject{
int _num;
int _age;
int _sex;
NSString* _name;
NSString* _phone;
NSString* _QQ;
}
@property int _num,_age,_sex;
@property NSString* _name, *_phone,*_QQ;
-(void) showStudentInfo;
-(void) addStudent:(QF_student *)stu;
@end
#import "WK_student.h"
@implementation WK_student
@synthesize _num,_age,_sex,_QQ;
@synthesize _phone,_name;
-(void)showStudentInfo{
NSLog(@"name=%@",_name);
NSLog(@"num=%d",_num);
NSLog(@"age=%d",_age);
NSLog(@"sex=%@",(_sex==0) ? @"男":@"女");
NSLog(@"QQ=%@",_QQ);
NSLog(@"phone=%@",_phone);
}
-(void)addStudent:(QF_student *)stu{
}
@end
#import <Foundation/Foundation.h>
#import "WK_student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
WK_student *stu = [[WK_studentalloc] init];
stu._name = @"张三";
stu._age = 20;
stu._sex = 1;
stu._num = 1002;
stu._QQ = @"1919192388";
stu._phone = @"13880901088";
[stu showStudentInfo];
}
return 0;
}
相关推荐
Objective-C语言将决定尽可能的从编译和链接时推迟到运行时。只要有可能,Objective-C总是使用...本文档将具体介绍NSObject类以及Objective-C程序是如何和运行时系统交互的。特别地,本文档还给出来怎样在运行时动态地加
《Objective-C 程序设计(第4版)》作者假设读者没有面向对象程序语言或者C语言(Objective-C基础)编程经验,因此,初学者和有经验的程序员都可以使用这本《Objective-C 程序设计(第4版)》学习Objective-C。...
通过学习Objective-C的基本语法、类和对象、控制流程和方法等内容,你将能够编写简单的Objective-C程序并逐渐掌握更复杂的概念。继续学习和实践,你将能够开发各种类型的应用程序,并为用户提供出色的体验。祝你在...
Objective-C语言的许多决策可以在编译和运行时执行。只要有可能,它是动态的... Runtime的核心是在运行时动态操作类和消息分发给其他对象,本源代码详细介绍了怎么使用它们。 博客:http://blog.csdn.net/y550918116j
《objective-c程序设计》通过大量的实例系统地介绍了objective-c语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/...
C语言主要知识点巩固(学习Objective-C 的前提),PPT格式。
用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计...
objective语言快速入门的绝佳途径,是视频教程的完整ppt课件,涵盖所有知识点,后出版书目为《iphone与ipad》开发实战的前半部分语言学习!
本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中的框架,以及继承、复合、源文件组织等众多重要的面向对象编程技术。...
Objective-C是创建Mac OS应用和iPhone应用的主要语言,优雅的面向对象编程环境与C语言特性珠联璧合,造就了它的不俗表现。 本书全面而系统地讲述Objective-C语言的基础知识和面向对象编程的重要概念,结合实例...
Objective-C 基础教程,从入门到精通 Objective-C 基础教程,从入门到精通,是你学习 Objective-C 的经典之作。
在windows系统下搭建Objective-C开发环境,由于公司要做iphone开发,个人学习objc(Objective-C)语法,所以在windows7下搭建了一个objc开发环境,现把自己整理的帮助文档拿来与大家分享,希望能对大家有所帮助。...
Objective-C 是一种通用、面向对象的编程语言,广泛应用于 macOS 和 iOS 等苹果平台的开发中。它是 C 语言的超集,结合了 Smalltalk 式的消息传递机制和 C++ 的特性。Objective-C 是苹果公司采用的主要编程语言之一...
elcome to Learn Objective- C on the Mac! This book is designed to teach you the basics of the Objective- C language. Objective- C is a superset of C and is the language used by many (if not most) ...
Objective-C高级编程 iOS与OS X多线程和内存管理.pdf
本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中的框架,以及继承、复合、源文件组织等众多重要的面向对象编程技术。...
objective-c基础学习 资料 适合初学者学习使用
书共分为四大部分:第一部分全面讲解了Objective-C语言的基础知识,包括类、对象、方法、数据 类型、表达式、程序结构、继承、多态、动态类型和动态绑定、函数、数组、结构和指针等;第二 部分详细阐述了Foundation...