zoukankan      html  css  js  c++  java
  • OC对象:封装、继承、多态的使用举例一

    // 该代码在网上找的视频中的例子,感觉很适合类和对象分不清楚的同学参考,仅供学习分享,谢谢

    // 创建一个Pointtest类,用属性x、y表示点的坐标位置,求两点之间的距离,使用两种方法:类方法和对象方法

    #import <Foundation/Foundation.h>

    #import <math.h> // 要使用到开方和求平方根两个函数,pow和sqrt,所以对该头文件进行声明

    // 声明类的属性和方法

    @interface Pointtest : NSObject

    {

        double _x; // 设置x坐标
        double _y; // 设置y坐标

    }

    // 设置x的setter和getter方法,基础较好的同学应该知道这是啥东西吧

    - (void)setX:(double)x;

    - (double)x;

    // 设置y的setter和getter方法,基础较好的同学应该知道这是啥东西吧

    - (void)setY:(double)y;

    - (double)y;

    // 设置同时获取x、y值,为什么还要设置一次呢?因为封装的思想:隐藏对象的属性和实现细节

    - (void)setX:(double)x andY:(double)y;

    // 写一个对象方法,来计算该点对象与其他点对象的距离

    - (double)distanceWithOther:(Pointtest *)other;

    //写一个类方法,来计算2个点对象之间的距离

    + (double)distanceBetweenPoint1:(Pointtest *)p1 andPoint2:(Pointtest *)p2;

    @end

    // 实现

    @implementation Pointtest

    // 设置x的setter和getter方法,基础较好的同学应该知道这是啥东西吧

    - (void)setX:(double)x

    {

    // 可以在这里添加判断语句或是其他算法来达到封装效果

        _x = x;

    }

    - (double)x

    {

        return _x;

    }

    // 设置y的setter和getter方法,基础较好的同学应该知道这是啥东西吧

    - (void)setY:(double)y

    {

    // 可以在这里添加判断语句或是其他算法来达到封装效果

        _y = y;

    }

    - (double)y

    {

        return _y;

    }


    // 设置同时获取x、y值,为什么还要设置一次呢?因为封装的思想:隐藏对象的属性和实现细节

    - (void)setX:(double)x andY:(double)y

    {

    // 直接使用创建对象本身的方法来调用设置,这样就不用管里面的方法是怎样实现的了,封装的目的就达到了

        [self setX:x];
        [self setY:y];

    }

    // 写一个对象方法,来计算该点对象与其他点对象的距离

    - (double)distanceWithOther:(Pointtest *)other

    {

    // 两点间距离的计算公式:((x1-x2)平方 + (y1-y2)平方)平方根

        double x1 = [self x];

        double x2 = [other x];

        double y1 = [self y];

        double y2 = [other y];

        double powX = x1-x2;

        double powY = y1-y2;

        double powSum = pow(powX,2)+pow(powY,2);

        return sqrt(powSum); // 这里写太细了   o.0

    }

    //写一个类方法,来计算2个点对象之间的距离

    + (double)distanceBetweenPoint1:(Pointtest *)p1 andPoint2:(Pointtest *)p2

    {

    // 这里理解很重要,这里是用对象方法实现的

        return [p1 distanceWithOther:p2];

    }

    @end

    // 主函数

    int main()

    {

    // 创建一个点(13,10)

        Pointtest *d1 = [Pointtest new];

        [d1 setX:13 andY:10]; // 不用单个的去设了

    // 创建一个点(10,14)

        Pointtest *d2 = [Pointtest new];

        [d2 setX:10 andY:14];

    // 调用对象方法

        double distance1 = [d1 distanceWithOther:d2];
        
    //调用类方法

        double distance2 = [Pointtest distanceBetweenPoint1:d1 andPoint2:d2];

        NSLog(@"对象方法算出的结果%f,类方法算出的结果%f",distance1,distance2);

        return 0;

    }



    Henry
  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/TomBombadil/p/5127343.html
Copyright © 2011-2022 走看看