iOS入门(十五)类和对象

类和对象
类和对象是面向对象的核心 
定义类,创建对象,使用对象。
类 : 具有相同特征和行为的事物的抽象。
  定义类包括两个部分,接口部分和实现部分
@ oc对c语言的扩展。
@interface person:NSObject

创建对象   :分配内存空间    Person *P = 【Person alloc 】;
初始化       : p = 【p init 】;
Person *p = 【【Person alloc 】 init 】

-(id)init;表示这个方法属于对象,只能对象执行;
+(id)init; 表示这个方法属于类,只能类执行;
id  表示任意类型的对象。

类名首字母大写,方法名首字母小写,每个单词首字母大写。
实例变量操作
实例变量可见度有三种  


定义6个类: 1、声明:实例变量、方法。实现:初始化方法、自定义方法、自我

    介绍方法。(方法实现时,以NSLog输出即可)
    2、在main.m中分别创建6个类的对象。 3、给对象的实例变量进行赋值、取值操作


完成蓝鸥班级开班仪式流程:   
  1、早9:30开班仪式开始。(NSLog(@“开班仪式正式开始”))2、刘辉老师做自我介绍。  3、三名学生做自我介绍。

    需求: (1)定义一个班级类。

    实例变量:所属培训机构名称,班级名,班级人数、授课老师名字、开班日期。 方法:初始化(设置学校名字:蓝鸥)、集体活动、开班仪式。

    (2)定义一个老师类。 实例变量:名字、性别、年龄、正在教的课程、正在授课班级。 方法:初始化、讲课、布置作业、验收作业、解决问题、自我介绍。

    (3)定义一个学生类。 实例变量:名字、性别、年龄、学号、班级名、任课老师、正在学习的课程。 方法:初始化、学习、做作业、提出问题、回答问题、自我介绍。 注:方法以NSLog输出为主


编程实现:在一个视图坐标系中(原点在左上角,原点向右为正 x,原点向下为正y,坐标系宽320,高480),创建3个矩形对象,参 数如图所示(下一页)。输出这三个矩形的坐标x,坐标y,宽,高, 周长,面积。矩形不需要显示,只需要设置参数即可。需求:

    定义矩形类:

    实例变量:坐标x、坐标y、宽、高、矩形中心点的坐标centerX、 矩形中心点的坐标centerY、面积、周长。

    方法:初始化、绘制(输出4个主要数据,面积、周长),计算周长、 计算面积。

 

分数(分子/分母)计算:在main.m中,创建两个分数对象,对这 两个分数进行加、减、乘、除运算,并通过执行方法输出结果。

    提示:定义分数类:

    1、实例变量:分子、分母

    2、方法:输出结果(分子/分母 字符串表示分数)、约分。



#import

#import "Person.h"

#import "AudCar.h"

#import "MobilePhone.h"


#import "Fruit.h"

#import "Close.h"

#import "Vegetable.h"

#import "Meal.h"

#import "Flower.h"

#import "Tree.h"


#import "Classes.h"

#import "Teacher.h"

#import "Student.h"


#import "Number.h"


#import "Rectangle.h"

int main(int argc, const char * argv[])

{


//    Person * per1 = [Person alloc] ;

//    per1 = [per1 init ] ;

//    per1 -> _name = @"郑博";

//    per1 -> _age = 27;

//    per1 -> _sex = @"男";

//    per1 -> _hobby = @"张静静";

//    

//    NSLog(@"%@" ,per1 -> _name);

//    [per1 sayHi];

//    

//    Person *per2 = [[Person alloc ] init ];

//    [per2 sayHi];

//    

//    AudCar  *aud1 = [[AudCar alloc] init ];

//    [aud1 info];

//    

//    MobilePhone *mo1 = [[MobilePhone alloc] init ] ;

//    [mo1 info];

    

    // 题目一  :

//    Fruit *fr = [[Fruit alloc] init];

//    [fr info];

//    Close *cl = [[Close alloc] init] ;

//    [cl info];

//    Vegetable *ve = [[Vegetable alloc] init ];

//    [ve info];

//    Meal *me = [[Meal alloc] init ];

//    [me info];

//    Flower *flo = [[Flower alloc] init ] ;

//    [flo info];

//    Tree *tr = [[Tree alloc] init ];

//    [tr info];

    

    //题目二  :

//    NSLog(@" 开班仪式正式开始 ~ ");

//    Classes *cla =  [[Classes alloc] init] ;

//    [cla info];

//    NSLog(@" 任课老师介绍");

//    Teacher *tea = [[Teacher alloc]init];

//    [tea info];

//    NSLog(@" 刘辉老师做自我介绍 :");

//    Teacher *tea2 = [Teacher alloc ] ;

//    tea2 -> _name = @"刘辉";

//    tea2 -> _sex = @"男";

//    tea2 -> _age = 32;

//    tea2 -> _classes = @"蓝欧学校" ;

//    tea2 -> _class = @"c 语言";

//    [tea2 info];

//    NSLog(@" 学生自我介绍");

//    NSLog(@" 第一名学生");

//    Student *stu = [[Student alloc] init ];

//    [stu info];

//    NSLog( @" 第二名学生");

//    Student *stu2 = [[Student alloc]init];

//    stu2 -> _name = @"付金诗";

//    stu2 -> _age = 24;

//    stu2 -> _num = 454354;

//    [stu2 info];

//    NSLog( @" 第三名学生 ");

//    Student *stu3 = [[Student alloc] init] ;

//    stu3 -> _name = @"裴紫夷";

//    stu3 -> _age = 22;

//    stu3 -> _num = 456451;

//    stu3 -> _sex = @" 女";

//    [stu3 info];

    

    //题目三  :

//    Rectangle *r1 = [Rectangle alloc ] ;

//    Rectangle *r2 = [Rectangle alloc ];

//    Rectangle *r3 = [Rectangle alloc ];

//    Rectangle *r4 = [Rectangle alloc ];

//    r1 -> _x = 0 ;

//    r1 -> _y = 0 ;

//    r1 -> _width = 320 ;

//    r1 -> _height = 480;

//    r1 -> _centerX = (r1 -> _x + r1 ->_width )/2 ;

//    r1 -> _centerY = (r1 ->_y + r1 ->_height) / 2;

//    [r1 info];

//    r2 -> _x = 60 ;

//    r2 -> _y = 100 ;

//    r2 -> _width = 70 ;

//    r2 -> _height = 120;

//    r2 -> _centerX = (r2 -> _x + r2 ->_width )/2 ;

//    r2 -> _centerY = (r2 ->_y + r2 ->_height) / 2;

//    [r2 info];

//    r3 -> _x = 170 ;

//    r3 -> _y = 150 ;

//    r3 -> _width = 120 ;

//    r3 -> _height = 50;

//    r3 -> _centerX = (r3 -> _x + r3 ->_width )/2 ;

//    r3 -> _centerY = (r3 ->_y + r3 ->_height) / 2;

//    [r3 info];

//    r4 -> _x = 180 ;

//    r4 -> _y = 280 ;

//    r4 -> _width = 90 ;

//    r4 -> _height = 160;

//    r4 -> _centerX = (r4 -> _x + r4 ->_width )/2 ;

//    r4 -> _centerY = (r4 -> _y + r4 ->_height) / 2;

//    [r4 info];

    

    // 题目四  :

    Number *num1 = [[Number alloc] init] ;

    Number *num2 = [[Number alloc] init ];

    Number *sum = [[Number alloc] init ];

    Number *poor = [[Number alloc ] init ];

    Number *indigestion = [[Number alloc] init ];

    Number *quotient = [[Number alloc] init ] ;

    num1 -> _fenmu = arc4random() ;

    num2 -> _fenzi = arc4random() ;

    num1->_fenzi = arc4random();

    num2->_fenmu = arc4random();

    [num1 sample];

    [num2 sample];

    NSLog(@" 这两个数字分别为  :  ");

    [num1 info];

    [num2 info];

    NSLog( @" 它们的和为  :  ");

    sum -> _fenmu = num1 -> _fenmu * num2 ->_fenmu ;

    sum -> _fenzi = num1 -> _fenzi * num2 -> _fenmu  +  num2 -> _fenzi * num1 ->_fenmu;

    [sum sample];

    [sum info];

    NSLog( @" 它们的差为  :  ");

    poor -> _fenmu = num1 -> _fenmu * num2 ->_fenmu ;

    poor -> _fenzi = num1 -> _fenzi * num2 -> _fenmu  -  num2 -> _fenzi * num1 ->_fenmu;

    [poor sample];

    [poor info];

    NSLog( @" 它们的积为  :  ");

    indigestion -> _fenmu = num1 -> _fenmu * num2 ->_fenmu ;

    indigestion-> _fenzi = num1 -> _fenzi * num2 -> _fenzi ;

    [indigestion sample];

    [indigestion info];

    NSLog( @" 它们的商为  :  ");

    quotient -> _fenmu = num1 -> _fenmu * num2 ->_fenzi ;

    quotient-> _fenzi = num1 -> _fenzi * num2 -> _fenmu ;

    [quotient sample];

    [quotient info];


#import "Number.h"


@implementation Number

-(id)init

{

    _fenmu = 1 ;

    _fenzi = 1;

    return self;

}


-(void)info

{

    NSLog(@" 这个结果为 : %d/%d . " , _fenzi , _fenmu);

}


-(void)sample

{

    int a = _fenmu > _fenzi ? _fenmu : _fenzi ;

    int b = _fenmu < _fenzi ? _fenmu : _fenzi ;

    while ( b != 0   &&  a % b != 0 ) {

        int r = a ;

        a = b ;

        b = r  % b ;

    }

    _fenmu /= b ;

    _fenzi /= b ;

    

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值