美文网首页
类与对象二

类与对象二

作者: 我不白先生 | 来源:发表于2020-09-11 22:42 被阅读0次

1.类与对象(续)

1.1description(方法)
1.2属性
1.2.1关键字property自动生成,成员变量声明+setter(赋值)+getter(访问)
1.2.2.1nonatomic:线程不安全
1.2.2.2readonly:只生成成员变量的声明+getter(访问),不生成setter(赋值)
1.3初始化
1.3.1无参初始化:init方法是从NSObject类中继承过来的
1.3.2带参初始化:方法名的格式为:-(instancetype)initWith...
1.3.3instancetype:实例(对象)的数据类型
1.3.4id
1.3.4.1万能指针(数据类型的关键字)
1.3.4.2id数据类型定义指针时,不需要加*
1.3.4.3只能指向OC类的对象,不能指向基本数据类型或结构体类型的变量
1.3.4.4万能指针不能使用“.”操作属性,只能调用sette或getter方法

1.3.5self
1.3.5.1是一个指针(变量名)
1.3.5.2是一个局部变量,只能用在类中方法的函数体中
1.3.5.3在函数体中指向调用该方法的对象
1.3.5.4使用self的目的是在同一个方法的函数体中区分不同的对象属性
1.3.6super:是一个指针变量名

day12_04(无参初始化)

description类的标配.png
#import "TRInteger.h"

@implementation TRInteger
-(instancetype)init
{
    if(self = [super init])
    {
        self.integer = 10;
    }
    return self;
}//格式代码
-(NSString *)description//description一个类的标配
{
    return [NSString stringWithFormat:@"%d",self.integer];
}
@end


#import "ViewController.h"
#import "TRInteger.h"
#import "TRPoint.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int a = 10;
    TRInteger *i1 = [[TRInteger alloc] init];
    [self print:i1];
    TRPoint *p1 = [[TRPoint alloc] init];
    [self print:p1];
    
}
-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
}

@end

带参初始方法需要声明day12_05(带参初始化)

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TRInteger : NSObject
@property int integer;
-(instancetype)initWithInteger:(int)integer;//带参初始化需要声明
@end

NS_ASSUME_NONNULL_END
#import "TRInteger.h"

@implementation TRInteger
-(instancetype)init
{
    if(self = [super init])
    {
        self.integer = 10;
    }
    return self;
}
-(instancetype)initWithInteger:(int)integer
{
    self = [super init];
    if(self)
    {
        self.integer = integer;
    }
    return self;
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"%d",self.integer];
}

@end
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TRPoint : NSObject
@property int x,y;
-(instancetype)initWithX:(int)x andY:(int)y;

@end
NS_ASSUME_NONNULL_END

#import "TRPoint.h"

@implementation TRPoint
-(instancetype)init
{
    self = [super init];
    if(self)
    {
        self.x = 10;
        self.y = 20;
    }
    return self;
    
}
-(instancetype)initWithX:(int)x andY:(int)y
{
    self = [super init];
    if(self)
    {
        self.x = x;
        self.y = y;
    }
    return self;
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"(%d,%d)",self.x,self.y];
}

#import "ViewController.h"
#import "TRInteger.h"
#import "TRPoint.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRInteger *i1 = [[TRInteger alloc]initWithInteger:10];
    [self print:i1];
    TRInteger *i2 = [[TRInteger alloc]initWithInteger:20];
    [self print:i2];
    
    TRPoint *p1 = [[TRPoint alloc]initWithX:10 andY:20];
    [self print:p1];
    TRPoint *p2 = [[TRPoint alloc]initWithX:30 andY:40];
    [self print:p2];
}

-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
}

@end

关于id万能指针

#import "ViewController.h"
#import "TRInteger.h"
#import "TRPoint.h"
typedef struct
{
    int I;
    double d;
}Data;
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    id a;//a是一个万能指针,注意:前面不需要加*
    TRInteger *f = [[TRInteger alloc] init];
     a = f;//只能指向OC的对象
    int b;
    //a = &b;//不能指向基本数据类型的变量
//    [self print:f];
//    TRPoint *f1 = [[TRPoint alloc]initWithX:10 andY:20];
//    [self print:f1];
    Data data = {10 ,3.14};
    //a = &data;//不能指向结构体类型的变量
    f.integer = 20;
    //a.integer = 30;//万能指针不能使用"."操作属性
    [a setInteger:20];//只能调用setter或getter方法
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f];
      
}

练习二


练习二类.png

TRStudent.h

import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TRStudent : NSObject
@property NSString *name;
@property int age;
@property int ID;
@property NSString *tr_address;
-(instancetype)initWithName:(NSString *)name AndAge:(int)age andID:(int)ID andAddress:(NSString *)tr_address;
@end

TRStudent.m

#import "TRStudent.h"

@implementation TRStudent
-(instancetype)init//无参初始化
{
    self = [super init];
    if(self)
    {   self.name =@"张三";//[self setName:@"张三"];
        self.age = 18;
        self.ID = 1000;
        self.tr_address = @"江苏南京";
    }
    return self;
}
-(instancetype)initWithName:(NSString *)name AndAge:(int)age andID:(int)ID andAddress:(NSString *)address//带参初始化
{
    self = [super init];
    if(self)
    {
        self.name = name;//带参的用形参来赋值与无参初始化的区别
        self.age = age;
        self.ID = ID;
        self.tr_address = address;
    }
    return self;
    
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"\n姓名:%@\n年龄:%d\n学号:%d\n家庭住址:%@",self.name,self.age,self.ID,self.tr_address];
}

ViewController.m

@interface ViewController ()//叫扩展
@property (weak, nonatomic) IBOutlet UILabel *outputNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputAgeLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputIDLabel;
@property (weak, nonatomic) IBOutlet UILabel *outputAddressLabel;//weak弱指针,nonatomic表示线程不安全
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRStudent *stu1 = [[TRStudent alloc] init];
    [self print:stu1];
    
    TRStudent *stu2 = [[TRStudent alloc] initWithName:@"李四" AndAge:20 andID:1001 andAddress:@"广东深圳"];
    [self print:stu2];
}
-(void)print:(id)obj
{
    self.outputNameLabel.text = [obj name];
    self.outputAgeLabel.text = [NSString stringWithFormat:@"%d",[obj age]];
    self.outputIDLabel.text = [NSString stringWithFormat:@"%d",[obj ID]];
    self.outputAddressLabel.text = [obj tr_address];
}

重点重构!!!

重点重构!!!

重点重构!!!

相关文章

  • python-高级、面向对象

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

  • 类与对象二

    1.类与对象(续) 1.1description(方法)1.2属性1.2.1关键字property自动生成,成员变...

  • OC重学第一天

    一 . Objective-C基础语法 二. OC 与 C 的对比 三 . 类与对象 1 . 类与对象的区分 2 ...

  • 二、类与对象 @GeekBand

    类型系统 引用类型 reference type 类 class 指针 pointer 块 block 值类型 v...

  • js的类和对象的创建与技术

    js的类和对象的创建的技术 一:类和对象的调用 Js代码 二:函数创建对象 this指当前类的属性与java相似(...

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

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

  • java基础

    一、.java源程序与编译型运行区别 二、对象和类 对象:对象是类的一个实例,有状态和行为。 类:类是一个模版,它...

  • 2018-12-05Java第二季笔记

    Java第二季 类与对象 类 所有程序都以类class为组织单元;类是模子,确定对象的特征(属性)和行为(方法);...

  • 类,封装

    一,面向对象 二,类 1,类的定义 2,类的实例化过程 3,属性查找与绑定方法 示例 4,类和对象 -- 游戏中生...

  • 函数类和对象区别

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

网友评论

      本文标题:类与对象二

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