美文网首页
类与对象一

类与对象一

作者: 我不白先生 | 来源:发表于2020-09-10 17:22 被阅读0次
image.png

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实例:对象的别称。用类这种类型定义变量,被称为类的实例化
练习如何在项目内新建类,以及赋值加访问,和怎么添加音乐


建类狗.png
TRCat.png
#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];
}

练习

类练习.png

TREmployee.png
#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]];
}
TREmployee屏幕显示.png

TRGoods.png
#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
TRGoods屏幕显示.png

相关文章

  • python-高级、面向对象

    一、类与对象 二、类和对象

  • 类与对象(类与对象定义)

    类与对象的定义和使用 如果在程序之中要定义一个类可以使用class关键字完成,而定义的语法如下: 在这个类中只是定...

  • 类与对象一

    1.类与对象 1.1类 1.1.1是结构体的升级,用于定义变量的数据类型1.1.2与结构体的区别1.1.2.1关键...

  • 一、类与对象

    1. 认识类与对象 (1)什么是类(class)? 类(class)是类型(type),是用户自定义的类型。为什么...

  • 函数类和对象区别

    类与类:行为不同 对象与对象:数据不同

  • 三、元类对象的本质

    1. 元类对象结构。 与类对象的结构一样。 与类对象比较 2. 元类对象的获取。 通过object_getClas...

  • python入门开发学习笔记之类与对象

    本节重点 掌握什么是类、什么是对象 掌握如何定义及使用类与对象 了解对类与对象之间的关系 类与对象的概念 类即类别...

  • 类对象与类的对象

    类对象(obj.getClass)描述的是类的代码信息,比如哪些属性、属性是什么类型、变量名是什么、哪些方法、方法...

  • 面向对象(一)-类与对象

    1.面向过程与面向对象 在计算机语言发展史中 1-机器语言 2-汇编语言 3-高级语言面向过程语言C语言就是面向过...

  • UML 类图与对象图

    一、类图 1.类与对象 1.1类与对象的概念 类是具有相同结构、行为和关系的一组对象的描述符。对象是符合类定义的一...

网友评论

      本文标题:类与对象一

      本文链接:https://www.haomeiwen.com/subject/blqpektx.html