欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS从零基础到精通就业-OC语言入门 属性2

程序员文章站 2022-09-02 21:43:03
// // Car.h // 属性 // // Created by 蓝鸥 on 16/7/29. // Copyright © 2016年 lua...
//
//  Car.h
//  属性
//
//  Created by 蓝鸥 on 16/7/29.
//  Copyright © 2016年 luanbin. All rights reserved.
//

#import 

@interface Car : NSObject
{
//    NSString *_brand;
//    float _price;
}
//属性声明
@property NSString *brand;
@property float price;
//属性到底做了什么???
/*
 1,自动生成以_开头的实例变量
 2,自动生成set get的声明
 3,自动生成set get的实现
 */

//set get
//-(void)setBrand:(NSString *)brand;
//-(NSString *)brand;

//-(void)setPrice:(float)price;
//-(float)price;


//自定义初始化方法
-(id)initWithBrand:(NSString *)brand price:(float)price;

-(void)run;
-(void)stop;

@end



//
//  Car.m
//  属性
//
//  Created by 蓝鸥 on 16/7/29.
//  Copyright © 2016年 luanbin. All rights reserved.
//

#import "Car.h"

@implementation Car

//@synthesize brand = _brand;
//@synthesize price = _price;

//-(void)setBrand:(NSString *)brand
//{
//    _brand = brand;
//}
//-(NSString *)brand
//{
//    return _brand;
//}
//
//-(void)setPrice:(float)price
//{
//    _price = price;
//}
//-(float)price
//{
//    return _price;
//}



-(id)initWithBrand:(NSString *)brand price:(float)price
{
    self = [super init];
    if (self) {
        _brand = brand;
        _price = price;
    }
    return self;
}

-(void)run
{
    NSLog(@"车正在疾驰");
}

-(void)stop
{
    NSLog(@"停车");
}



@end