OC简单语法复习

这篇博客回顾了Objective-C(OC)中的基本语法,重点在于如何创建和使用类。通过实例讲解了Student类的定义,包括Student.h头文件、Student.m实现文件以及main.m主程序的编写,展示了OC中类的声明、实现和调用过程。
摘要由CSDN通过智能技术生成

第一个oc类


OC中。新建一个类会生成2个文件,OC中一个类需要2个文件来描述。
一个.h(声明),用于声明变量.方法
声明的关键字:@interface @end  
一个.m(实现)用于实现.h中的方法 
实现的关键字 @implementation @ end
 

如:Student 类

Student.h——文件

#import <Foundation/Foundation.h>
// @interface 声明一个类的关键字,在.h文件当中。 
@interface Student : NSObject{
// {中声明变量 }
  int _age;
int _height;
}
// {}外声明方法。
// 方法格式 -/+ (返回类型)方法名:(参数类型1)参数名1:方法名:(参数类型2)参数名2。。。
// -代表动态方法, +代表静态方法
// 动态方法(对象方法),能使用对象中的参数,只能对象调用的方法。
//静态方法(对象方法),不能使用对象中的参数,只能类调用的方法。
// 一般不使用对象参数就能用的方法,定义为静态方法,
// 动态方法和静态方法可以很重名
// 在这里声明的方法都是公共的。
/*
	@private		只能在类内部访问
	@protected	只能在类内部和子类中访问(默认)
	@public 		全局都可以访问
*/


-(int)age;
-(int)height;
/*
方法格式 -/+ (返回类型)方法名:(参数类型1)参数名1:方法名:(参数类型2)参数名2。。。
在OC方法中,一个冒号:对应一个参数。由于age的set方法接收一个int类型的参数,参数名为newAge,所以(int)newAge前面有一个冒号:
一定要记住:一个冒号:对应一个参数,而且冒号:也是方法名的一部分
*/
-(void)setAge:(int)newAge;
-(void)setHeight:(int)newHeight;
- (void)setAge:(int)newAge andHeight:(float)newHeight;
/*
如:
方法格式 -/+ (返回类型)方法名:(参数类型1)参数名1:方法名:(参数类型2)参数名2。。。
- (void)setAge:(int)newAge andHeight:(float)newHeight;
* 这个方法是动态方法、没有返回值,接收2个参数,所以有2个冒号:

* 这个方法的方法名是setAge:andHeight:

* 其实andHeight是可以省略的,它只是为了让方法名念起来通顺一点,也让(float)newHeight前面的冒号:不那么孤单
*/ 

// 定义自己的构造函数,一般返回值是id 意思为能返回任何类型。id中已经包含了指针表示*,所以不用再加*来定义 
// 其实不以init开头也可以,其他符合方法名规范的名字都行,不过。根据OC中的构造函数定义规范,一般都以init开头。
-(id)initWithAge:(int)age andHeight:(int)height;
@end


Student.m——文件

#import "Student.h" // 需要导入.h的头文件。相当于把Student.h拷贝到#import所在的位置

@implementation Student
-(int)age{
/*
	@implementation中实现方法。
	这里可以使用.h中定义的_age变量是因为导入了Student.h头文件
*/
    return _age;
}
-(int)height{
	return _height;
}
-(void)setAge:(int)newAge{
// 注:千万不要使用self.age=newAge;
/*
	因为OC中的点语法。self.age 会编程 [self age];
	[self age:newAge];
	会死循环。
*/
    _age=newAge;
}
-(void)setHeight:(int)newHeight{
	_height=newHeight;
}
- (void)setAge:(int)newAge andHeight:(float)newHeight{
	_age=newAge;
	_height=newHeight;
}

/*
  实现构造方法
 */
-(id)initWithAge:(int)age andHeight:(int)hegith{
    /*
        1。先调用super的构造方法
     */
    // 防止init方法错误,如果self不为空,
    if(self=[super init]){
        _age=age;
        _height=hegith;
        /*
          可以使用点语法。 不会造成死循环。
         self.age=age;
         self.height=height;
         */
    }
    return self;
}

// 重写父类的description方法,实现自定义格式化对象的输出
// 当使用%@打印一个对象的时候,会调用这个方法
-(NSString *)description{
    NSString *str=[NSString stringWithFormat:@"age is %i and no is %i",_age,_height];
  // 点语法
   NSString *str=[NSString stringWithFormat:@"age is %i and no is %i",self.age,self.height];
    return str;
}

// test只在.m文件中实现,没有在.h文件中声明,那test方法,便是私有方法。
-(void)test{
	NSLog(@"这个方法是私有方法!");
}

// 谁调用方法,self就代表谁
-(void)test2{
	// 对象方法self代表调用的对象
	int age=self.age;
}
-(void)test2{
	//类效用方法,self代表类名
	[Student alloc];
	[self alloc];
}

@end


main.m——文件

#import <stdio.h>
#import "Student.h"
int main(int argc, const char * argv[])
{
    // 声明一个指针变量,指向,Student 调用类方法的alloc开辟的一个空间。
    Student *stu=[Student alloc];

// 调用init 初始化对象。
    [stu init];
// 开辟空间,并初始化。
  Student *stu2=[[Student alloc]init];

// 调用age方法取值
int age=[stu age];
// 调用setAge:方法赋值 
[stu setAge:20];

// 调用setAge: andHeight: 方法赋值 
[stu setAge:20 andHeight: 150];

// 使用自己的构造方法来定义对象
 Student *stu3=[[Student alloc] initWithAge:50 andHeight:70];
/*
	点语法,
	对象名.方法名
	stu.age;
  编译器会自动根据使用情况,将stu.age ,翻译成[stu age]或[stu setAge:10];
	注:编译器翻译成[stu setAge:10];,[stu age];语句,而不是stu.变量名。
*/
stu.age=10;
age=stu.age;

NSLog(@" age=%d,height=%d",[stu age],[stu height]);

// 点语法,输出对象变量
NSLog(@" age=%d,height=%d",stu.age,stu.height);

//当使用%@可以打印对象
NSLog(@"%@",stu3);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dwt1220

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值