OC之面向对象作业小练

一、设计一个类Point2D,用来表示二维平面中某个点

1> 属性
* double x
* double y
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置x和y
* 设计一个对象方法计算跟其他点的距离
* 设计一个类方法计算两个点之间的距离
3> 提示
* C语言的math.h中有个函数:double pow(double n, double m); 计算n的m次方
* C语言的math.h中有个函数:double sqrt(double n); 计算根号n的值(对n进行开根)

Point2D的声明

#import <Foundation/Foundation.h>
@interface Point2D:NSObject
{
    double _x;
    double _y;
}
//x的set和get方法
-(void)setX:(double)x;
-(double)getX;
//x的set和get方法
-(void)setY:(double)y;
-(double)getY;
//同时设置x、y
-(void)setX:(double)x andY:(double)y;
//计算跟其他点之间的距离s
-(double)distanceWithOther:(Point2D *)other;
//计算两个点之间的距离
+(double)distanceBetweenPoint:(Point2D *)point1 andpoint2:(Point2D *)point2;
@end

Point2D实现

//
//  Point2D.m
//  类练习
//
//  Created by Mac on 15/3/12.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "Point2D.h"
#import <math.h>
@implementation Point2D
-(void)setX:(double)x
{
    _x=x;
}
-(double)getX
{
    return _x;
}


-(void)setY:(double)y
{
    _y=y;
}
-(double)getY
{
    return _y;
}

-(void)setX:(double)x andY:(double)y
{
    [self setX:x];
    [self setY:y];
}

-(double)distanceWithOther:(Point2D *)other
{
    //x1-x2
    double xDelta=_x-[other getX];
    //x1-x2的平方
    double xDeltaPF=pow(xDelta,2);
    //y1-y2
    double yDelta=_y-[other getY];
    //y1-y2
    double yDeltaPF=pow(yDelta,2);
    //返回距离开根号
    return sqrt(xDeltaPF+yDeltaPF);
}

+(double)distanceBetweenPoint:(Point2D *)point1 andpoint2:(Point2D *)point2
{
    return [point1 distanceWithOther:point2];
}
@end

主程序

//
//  main.m
//  作业求两点
//
//  Created by Mac on 15/3/12.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Point2D.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Point2D *p1=[[Point2D alloc]init ];
        [p1 setX:3 andY:4];
        Point2D *p2=[[Point2D alloc]init ];
        [p2 setX:6 andY:8];
        double d1=[p1 distanceWithOther:p2];
        
        //计算两点之间的距离
        double d2=[Point2D distanceBetweenPoint:p1 andpoint2:p2];
        
        NSLog(@"两点之间的距离是%f",d1);
        NSLog(@"两点之间的距离是%f",d2);
    }
    return 0;
}

结果:

2015-03-13 00:41:24.931作业求两点[748:53287]两点之间的距离是5.000000

2015-03-13 00:41:24.932作业求两点[748:53287]两点之间的距离是5.000000


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要改变 UIAlertAction 的字体大小,您可以使用富文本属性来设置 UIAlertAction 的标题的字体大小。下面是一个示例代码: ```objective-c UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 点击确定按钮后的处理逻辑 }]; // 创建富文本属性字典 NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:20]}; // 设置字体大小为 20 // 使用富文本属性设置 UIAlertAction 的标题字体大小 NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:@"确定" attributes:attributes]; [action setValue:attributedTitle forKey:@"attributedTitle"]; [alertController addAction:action]; [self presentViewController:alertController animated:YES completion:nil]; ``` 在上面的示例中,我们首先创建了一个 UIAlertController,并设置其标题和消息。然后,我们创建了一个 UIAlertAction,并设置其标题为 "确定",并指定了点击按钮后的处理逻辑。 接下来,我们创建了一个富文本属性字典,其中包含了一个字体大小为 20 的字体属性。然后,使用这个富文本属性设置 UIAlertAction 的标题字体大小。我们将富文本标题设置为 "确定"。 最后,我们将 UIAlertAction 添加到 UIAlertController 中,并通过 presentViewController 方法来显示 UIAlertController。 这样,您就可以改变 UIAlertAction 的字体大小了。请注意,使用私有键 "attributedTitle" 来设置富文本标题是一种非官方的方法,可能会在未来的 iOS 版本中发生变化。如果有其他问题,请随时提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值