OC_语法入门_day3_点语法/成员访问修饰符/property/synthesize

H:/Objective-C/OC_day3/00-圆的设计-1-Point2D.h
#import <Foundation/Foundation.h>

// 点
@interface Point2D : NSObject
{
    double _x; // x值
    double _y; // y值
}

// x值的getter和setter
- (void)setX:(double)x;
- (double)x;

// y值的getter和setter
- (void)setY:(double)y;
- (double)y;

// 同时设置x和y
- (void)setX:(double)x andY:(double)y;

// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other;
// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end

H:/Objective-C/OC_day3/00-圆的设计-2--Point2D.m
/*
 5.设计一个类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进行开根)
 */


#import "Point2D.h"
#import <math.h>

@implementation Point2D
// x值的getter和setter
- (void)setX:(double)x
{
    _x = x;
}
- (double)x
{
    return _x;
}

// y值的getter和setter
- (void)setY:(double)y
{
    _y = y;
}
- (double)y
{
    return _y;
}

// 同时设置x和y
- (void)setX:(double)x andY:(double)y
{
    /*
     _x = x;
     _y = y;
     */
    /*
     self->_x = x;
     self->_y = y;*/
    [self setX:x];
    [self setY:y];
}

// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other
{
    // ( (x1-x2)的平方 + (y1-y2)的平方 ) 开根
    // return [Point2D distanceBetweenPoint1:self andPoint2:other];
    
    // x1-x2
    double xDelta = [self x] - [other x];
    // (x1-x2)的平方
    double xDeltaPF = pow(xDelta, 2);
    
    // y1-y2
    double yDelta = [self y] - [other y];
    // (y1-y2)的平方
    double yDeltaPF = pow(yDelta, 2);
    
    // 返回距离
    return sqrt(xDeltaPF + yDeltaPF);
}

// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
    // 调用p2对象的对象方法计算p2和p1的距离
    return [p2 distanceWithOther:p1];
    
    // return [p1 distanceWithOther:p2];
    
    /*
     // x1-x2
     double xDelta = [p1 x] - [p2 x];
     // (x1-x2)的平方
     double xDeltaPF = pow(xDelta, 2);
     
     // y1-y2
     double yDelta = [p1 y] - [p2 y];
     // (y1-y2)的平方
     double yDeltaPF = pow(yDelta, 2);
     
     // 返回距离
     return sqrt(xDeltaPF + yDeltaPF);
     */
}
@end

H:/Objective-C/OC_day3/00-圆的设计-3-Circle.h
#import "Point2D.h"
#import <Foundation/Foundation.h>

// 圆
@interface Circle : NSObject
{
    // 半径
    double _radius;
    // 圆心
    //    double _x;
    //    double _y;
    Point2D *_point;
}

// 半径的getter和setter
- (void)setRadius:(double)radius;
- (double)radius;

// 圆心的getter和setter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;

// 判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
// 返回值是BOOL类型的,方法名一般都以is开头
- (BOOL)isInteractWithOther:(Circle *)other;
// 判断两个圆是否重叠(重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2;
@end

H:/Objective-C/OC_day3/00-圆的设计-4-Circle.m
/*
 6.设计一个类Circle,用来表示二维平面中的圆
 1> 属性
 * double _radius (半径)
 * Point2D *_point (圆心)
 
 2> 方法
 * 属性相应的set和get方法
 * 设计一个对象判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
 * 设计一个类方法判断两个圆是否重叠(重叠返回YES,否则返回NO)
 */
#import "Circle.h"

@implementation Circle
// 半径的getter和setter
- (void)setRadius:(double)radius
{
    _radius = radius;
}
- (double)radius
{
    return _radius;
}

// 圆心的getter和setter
- (void)setPoint:(Point2D *)point
{
    //[_point setX:[point x]];
    //[_point setY:[point y]];
    _point = point;
}
- (Point2D *)point
{
    return _point;
}

// 判断跟其他圆是否重叠(重叠返回YES,否则返回NO)
// 返回值是BOOL类型的,方法名一般都以is开头
- (BOOL)isInteractWithOther:(Circle *)other
{
    // 如果两个圆心的距离 < 两个圆的半径和 , 重叠
    // 如果两个圆心的距离 >= 两个圆的半径和 , 不重叠
    
    Point2D *p1 = [self point];
    Point2D *p2 = [other point];
    // 圆心之间的距离
    double distance = [p1 distanceWithOther:p2];
    
    // 半径和
    double radiusSum = [self radius] + [other radius];
    
    //    if (distance < radiusSum)
    //    {
    //        return YES;
    //    }
    //    else
    //    {
    //        return NO;
    //    }
    
    //    if (distance < radiusSum)
    //    {
    //        return 1;
    //    }
    //    else
    //    {
    //        return 0;
    //    }
    
    return distance < radiusSum;
}

// 判断两个圆是否重叠(重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)c1 andCircle2:(Circle *)c2
{
    // 调用c1对象的对象方法判断c1和c2是否重叠
    return [c1 isInteractWithOther:c2];
}
@end

H:/Objective-C/OC_day3/00-圆的设计-5-main.m
/*
 总结
 1.只有利用类名调用类方法的时候,不需要在类名后面写*。其他情况下,类名后面统一加上一个*
 Circle *c1 = [Circle new];
 - (BOOL)isInteractWithOther:(Circle *)other;
 
 2.返回值是BOOL类型的方法,方法名一般都以is开头
 - (BOOL)isInteractWithOther:(Circle *)other;
 
 3.想要拥有某个对象,就先创建对象,然后调用set方法将对象传递给内部的成员变量
 // 创建圆心对象
 Point2D *p2 = [Point2D new];
 [p2 setX:12 andY:19];
 // 设置圆心
 [c2 setPoint:p2];
 
 - (void)setPoint:(Point2D *)point
 {
    _point = point;
 }
 
 
 4.定义一个类分2个文件:.h声明文件、.m实现文件
 .h : 成员变量、方法的声明
 .m : 方法的实现
 
 5.如果想使用某一个类,只需要#import类的.h文件即可
 */

#import "Point2D.h"
#import "Circle.h"

int main()
{
    // 圆对象
    Circle *c1 = [Circle new];
    // 设置半径
    [c1 setRadius:1];
    // 创建圆心对象
    Point2D *p1 = [Point2D new];
    [p1 setX:10 andY:15];
    // 先设置圆心
    [c1 setPoint:p1];
    [[c1 point] setX:15];
    
    // c1 半径:1  圆心:(15, 15)
    
    // 圆对象
    Circle *c2 = [Circle new];
    // 设置圆的半径
    [c2 setRadius:2];
    
    // 创建圆心对象
    Point2D *p2 = [Point2D new];
    [p2 setX:12 andY:19];
    // 设置圆心
    [c2 setPoint:p2];
    
    // c2 半径:2 圆心:(12, 19)
    
    // 圆心距离:5 > 半径和:3
    //BOOL b1 = [c1 isInteractWithOther:c2];
    
    BOOL b1 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];
    NSLog(@"%d", b1);
    
    /*
    Point2D *p1 = [Point2D new];
    // (10, 15)
    [p1 setX:10 andY:15];
    
    Point2D *p2 = [Point2D new];
    // (13, 19)
    [p2 setX:13 andY:10];
    
    //double d1 = [p1 distanceWithOther:p2];
    double d1 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
    NSLog(@"%f", d1);*/
    return 0;
}

H:/Objective-C/OC_day3/02-第1个OC工程-1-Person.h
//
//  Person.h
//  第一个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _age;
}

- (void)setAge:(int)age;
- (int)age;

+ (void)test;
@end

H:/Objective-C/OC_day3/02-第1个OC工程-2-Person.m
//
//  Person.m
//  第一个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person

+ (void)test
{
    
}

- (void)setAge:(int)age
{
    _age = age;
}

- (int)age
{
    return _age;
}

@end

H:/Objective-C/OC_day3/02-第1个OC工程-3-Student.m
//
//  Student.m
//  第一个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Student.h"

@implementation Student

@end

H:/Objective-C/OC_day3/02-第1个OC工程-4-Student.h
//
//  Student.h
//  第一个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@interface Student : Person

@end

H:/Objective-C/OC_day3/02-第1个OC工程-5-main.m
//
//  main.m
//  第一个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"

int main()
{
    NSLog(@"4234234324");
    
    int a = 10;
    
    int b = a;
    b = 10;
    
    Student *stu = [Student new];
    
    [stu setAge:10];
    
    
    int c = [stu age];
    NSLog(@"c is %d", c);
    return 0;
}


H:/Objective-C/OC_day3/03-第2个OC工程-1-Person.h
//
//  Person.h
//  03-第2个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _age;
    NSString *_name;
}

// 年龄的getter和setter
- (void)setAge:(int)age;
- (int)age;

- (void)setName:(NSString *)name;
- (NSString *)name;

@end

H:/Objective-C/OC_day3/03-第2个OC工程-2-Person.m
//
//  Person.m
//  03-第2个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person

- (void)setAge:(int)age
{
    _age = age;
}

- (int)age
{
    return _age;
}


#pragma mark - 姓名的seter和getter
#pragma mark 姓名的set方法
- (void)setName:(NSString *)name
{
    _name = name;
}
#pragma mark 姓名的get方法
- (NSString *)name
{
    return _name;
}


#pragma mark - 其他方法
- (void)test
{

}
- (void)test1
{

}
@end

H:/Objective-C/OC_day3/03-第2个OC工程-3-main.m
//
//  main.m
//  03-第2个OC项目
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{
    Person *p = [Person new];
    //[p];
    
    [p setAge:10];
    
    int a = [p age];
    
    NSLog(@"年龄是%d", a);
    
    return 0;
}

H:/Objective-C/OC_day3/04-点语法-1-Person.h
//
//  Person.h
//  04-点语法
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _age;
    NSString *_name;
}

- (void)setAge:(int)age;
- (int)age;


- (void)setName:(NSString *)name;
- (NSString *)name;

@end

H:/Objective-C/OC_day3/04-点语法-2-Person.m
//
//  Person.m
//  04-点语法
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person

- (void)setAge:(int)age
{
    //_age = age;
    
    NSLog(@"setAge:");
    
    // 会引发死循环
    //self.age = age; // [self setAge:age];
}

- (int)age
{
    NSLog(@"age");
    return _age;
    // 会引发死循环
    //return self.age;// [self age];
}

- (void)setName:(NSString *)name
{
    _name = name;
}

- (NSString *)name
{
    return _name;
}

@end

H:/Objective-C/OC_day3/04-点语法-3-main.m
//
//  main.m
//  04-点语法
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{
    Person *p = [Person new];
    
    // 点语法的本质还是方法调用
    p.age = 10; // [p setAge:10];
    
    int a = p.age; // [p age];
    
    
    p.name = @"Jack";
    
    NSString *s = p.name;
    
    NSLog(@"%@", s);
    
    return 0;
}


H:/Objective-C/OC_day3/05-成员访问修饰符-1-Person.m
//
//  Person.m
//  05-成员变量的作用域
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person
{
    int _aaa;// 默认就是私有
    
    @public
    int _bbb;
    // @implementation中不能定义和@interface中同名的成员变量
    // int _no;
}

- (void)test
{
    _age = 19;
    
    _height = 20;
    
    _weight = 50;
    
    _aaa = 10;
}

- (void)setHeight:(int)height
{
    _height = height;
}

- (int)height
{
    return _height;
}

@end

H:/Objective-C/OC_day3/05-成员访问修饰符-2-Person.h
//
//  Person.h
//  05-成员变量的作用域
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

/*
 @public : 在任何地方都能直接访问对象的成员变量
 @private : 只能在当前类的对象方法中直接访问(@implementation中默认是@private)
 @protected : 可以在当前类及其子类的对象方法中直接访问  (@interface中默认就是@protected)
 @package : 只要处在同一个框架中,就能直接访问对象的成员变量
 
 @interface和@implementation中不能声明同名的成员变量
 */

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _no;
    
    @public // 在任何地方都能直接访问对象的成员变量
    int _age;
    
    
    @private  // 只能在当前类的对象方法中直接访问
    int _height;
    
    @protected // 能在当前类和子类的对象方法中直接访问
    int _weight;
    int _money;
}

- (void)setHeight:(int)height;
- (int)height;

- (void)test;
@end

H:/Objective-C/OC_day3/05-成员访问修饰符-3-main.m
//
//  main.m
//  05-成员变量的作用域
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"

@implementation Car : NSObject
{
@public
    int _speed;
    
@protected
    int _wheels;
}

- (void)setSpeed:(int)speed
{
    _speed = speed;
}
- (int)speed
{
    return _speed;
}

@end

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

    @autoreleasepool {
        Student *stu = [Student new];
        
        
        [stu setHeight:100];
        
        
        NSLog(@"%d", [stu height]);
        
        /*
        Car *c = [Car new];
        c->_speed = 250;
        */
        //c.speed = 10;
        
        // NSLog(@"%d", c.speed);
        
        //[c setSpeed:<#(int)#>];
        
        /*
        Person *p = [Person new];
        p->_bbb = 10;
        p->_age = 100;
        */
        //p->_height = 20;
        
        
        //p->_weight = 10;
    }
    return 0;
}




H:/Objective-C/OC_day3/05-成员访问修饰符-4-Student.h
//
//  Student.h
//  05-成员变量的作用域
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@interface Student : Person
- (void)study;
@end

H:/Objective-C/OC_day3/05-成员访问修饰符-5-Student.m
//
//  Student.m
//  05-成员变量的作用域
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Student.h"

@implementation Student
- (void)study
{
    
    // _height = 10;
    [self setHeight:10];
    
    
    int h = [self height];
    
    _weight = 100;
}
@end

H:/Objective-C/OC_day3/06-@property和@synthesize-1-Person.h
//
//  Person.h
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _age;
    // int age;
    
    int _height;
    
    double _weight;
    
    NSString *_name;
}

// @property:可以自动生成某个成员变量的setter和getter声明
@property int age;
//- (void)setAge:(int)age;
//- (int)age;


@property int height;
//- (void)setHeight:(int)height;
//- (int)height;

- (void)test;


@property double weight;

@property NSString *name;

@end

H:/Objective-C/OC_day3/06-@property和@synthesize-2-Person.m
//
//  Person.m
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person

// @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
@synthesize age = _age;

@synthesize height = _height;

@synthesize weight = _weight, name = _name;

@end

H:/Objective-C/OC_day3/06-@property和@synthesize-3-Car.h
//
//  Car.h
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Car : NSObject
{
    //int _speed;
    //int _wheels;
}
@property int speed;
@property int wheels;

//@property int speed, wheels;
- (void)test;
@end

H:/Objective-C/OC_day3/06-@property和@synthesize-4-Car.m
//
//  Car.m
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Car.h"

@implementation Car
//@synthesize speed = _speed, wheels = _wheels;
// 会访问_speed这个成员变量,如果不存在,就会自动生成@private类型的_speed变量
@synthesize speed = _speed;
@synthesize wheels = _wheels;


- (void)test
{
    NSLog(@"速度=%d", _speed);
}

@end

H:/Objective-C/OC_day3/06-@property和@synthesize-5-Dog.h
//
//  Dog.h
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject
@property int age;
@end

H:/Objective-C/OC_day3/06-@property和@synthesize-6-Cat.h
//
//  Cat.h
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Cat : NSObject
{
    //int _age;
    //int age;
}
@property int age;
@end

H:/Objective-C/OC_day3/06-@property和@synthesize-7-Cat.m
//
//  Cat.m
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Cat.h"

@implementation Cat

// 默认会访问age这个成员变量,如果没有age,就会自动生成@private类型的age变量
@synthesize age;


@end

H:/Objective-C/OC_day3/06-@property和@synthesize-8-main.m
//
//  main.m
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Cat.h"
#import "Dog.h"

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

    @autoreleasepool {
        Dog *d = [Dog new];
        
        d.age = 5;
        
        NSLog(@"%d", d.age);
    }
    return 0;
}


H:/Objective-C/OC_day3/06-@property和@synthesize-9-Dog.m
//
//  Dog.m
//  06-@property和@synthesize
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Dog.h"

@implementation Dog


- (void)setAge:(int)age
{

}

//- (int)age
//{
//    
//}

//- (int)age
//{
//    return 10;
//}

@end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值