
1.类与对象
1.1类
1.1.1是结构体的升级,用于定义变量的数据类型
1.1.2与结构体的区别
1.1.2.1关键字
1.1.2.2成员变量前(约定俗成)加下划线
1.1.2.3有函数
1.1.2.4属性(成员变量)和方法有访问权限
1.1.3由属性和方法构成
struct MyStruct
{
int I;
char ch;
};
@interface TRMyClass : NSObject
{
int _i;//成员变量,在类中被称为属性
char _ch;
}
-(void)method;//函数,在类中被称为方法
-(void)setI:(int)i;//成员变量_i的赋值方法
-(int)getI;//成员变量_i的访问方法
-(void)setCh:(char)ch;
-(char)getCh;
@end
@implementation TRMyClass
-(void)method
{
NSLog(@"类中的方法");
}
-(void)setI:(int)i//所有变量的复制都以set开头
{
_i = I;
}
-(int)getI
{
return _i;
}
-(void)setCh:(char)ch
{
_ch = ch;
}
-(char)getCh
{
return _ch;
}
@end
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
struct MyStruct a;//结构体变量可以定义在栈上
struct MyStruct *pa = (struct MyStruct*)malloc(sizeof(struct MyStruct));//结构体变量也可以定义在堆上
pa->i = 10;
self.outputLabel.text = [NSString stringWithFormat:@"%d",pa -> I];
free(pa);//结构体变量需要程序员手动释放
TRMyClass *b;//类的对象(变量)不允许定义在栈上,栈上只能定义类这种类型的指针
b = [TRMyClass alloc];//类的对象只能定义在堆上。且不需要程序员手动的释放
[b method];//使用对象b调用类TRMyClass中方法method.被称为向对象b发送mehtod消息
//b->_i = 10;//类的成员变量在类外不能直接赋值
[b setI:10];
//self.outputLabel.text = [NSString stringWithFormat:@"%d",b -> _i];//类的成员变量在类外不能直接访问
self.outputLabel.text = [NSString stringWithFormat:@"%d",[b getI]];
[b setCh:'a'];
self.outputLabel.text = [NSString stringWithFormat:@"%c",[b getCh]];
}
@end
- 问题:一个类的内存空间到底是由什么决定的。一个类的属性,是在什么时候释放的,作用域是什么
解:一个类的内存空间,是由这个类的所有属性决定的,类中的方法,不放在类的内存空间中,全部放在代码区。
一个类的属性,在通过这个类创建对象时,同时创建对象的属性,放在对象的内存控件中。在对象的内存销毁时,该对象的属性就会销毁。
1.2对象
1.2.1是类这种数据类型的变量,是容器加(处理容器内数据的)方法
1.2.2是一种特殊的变量,特殊在可以向对象发消息(对象可以调用方法)
1.2.3对象只能定义在堆上,不能定义在栈上,且不需要程序员释放,由ARC(编译器)自动释放
1.3访问权限
1.3.1属性(4种)
1.3.2方法(2种)
1.4实例:对象的别称。用类这种类型定义变量,被称为类的实例化
练习如何在项目内新建类,以及赋值加访问,和怎么添加音乐


#import <AVFoundation/AVFoundation.h>
@implementation TRCat
-(void)setName:(NSString *)name
{
_name = name;
}
-(NSString *)name
{
return _name;
}
-(void)setAge:(int)age
{
_age = age;
}
-(int)age
{
return _age;
}
-(void)bark
{
NSURL *u = [[NSBundle mainBundle]URLForResource:@"鹏程路172号.m4a" withExtension:nil];//nil表示空
AVAudioPlayer *av = [[AVAudioPlayer alloc]initWithContentsOfURL:u error:nil];
[av prepareToPlay];
[av play];
sleep(2);
[av stop];
}
viewController.m
#import "ViewController.h"
#import "TRCat.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TRCat *cat = [TRCat alloc];
[cat setName:@"小白"];
[cat setAge:1];
self.outputLabel.text = [NSString stringWithFormat:@"小猫%@%d岁了",[cat name],[cat age]];
[cat bark];
}
练习

一

#import "ViewController.h"
#import "TREmployee.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputAgeLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputGenderLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputSalaryLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TREmployee *e = [TREmployee alloc];//此行称为实例化
[e setName:@"张三"];
[e setAge:18];
[e setSalary:10000];
[e setGender:YES];
self.outputNameLabel.text = [e name];
self.outputAgeLabel.text = [NSString stringWithFormat:@"%d",[e age]];
self.outputGenderLabel.text = [NSString stringWithFormat:@"%@",[e gender] ? @"男" : @"女"];
self.outputSalaryLabel.text = [NSString stringWithFormat:@"%g",[e salary]];
}

二

#import "ViewController.h"
#import "TRGoods.h"
@interface ViewController ()
{
TRGoods *goods;
}
@property (weak, nonatomic) IBOutlet UILabel *outputGoodsNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputGoodsCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputGoodsSoldLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputGoodReMainingLabel;
-(void)printInfo;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
goods = [TRGoods alloc];
[goods setName:@"农夫山泉"];
[goods setGoodsCount:10*24];
[goods setGoodsSold:0];
[self printInfo];
// self.outputGoodsNameLabel.text = [goods name];
// self.outputGoodsCountLabel.text = [NSString stringWithFormat:@"%d",[goods goodsCount]];
// self.outputGoodsSoldLabel.text = [NSString stringWithFormat:@"%d",[goods goodsSold]];
// self.outputGoodReMainingLabel.text = [NSString stringWithFormat:@"%d",[goods goodsRemaining]];
}
-(void)printInfo
{
self.outputGoodsNameLabel.text = [goods name];
self.outputGoodsCountLabel.text = [NSString stringWithFormat:@"%d",[goods goodsCount]];
self.outputGoodsSoldLabel.text = [NSString stringWithFormat:@"%d",[goods goodsSold]];
self.outputGoodReMainingLabel.text = [NSString stringWithFormat:@"%d",[goods goodsRemaining]];
}
- (IBAction)sell:(UIButton *)sender {
[goods sell];
[self printInfo];
}
@end

网友评论