先来几段代码,Dog类的.h .m文件 和main.m dog.h #import Foundation/Foundation.h@interface Dog : NSObject{ int _ID; NSString *_name; int _age; float _price;}//凡是用initXXX开头的都是构造函数//init方法实际上没有什么特别的,只是遵循命名约定的普
先来几段代码,dog类的.h .m文件 和main.m
dog.h
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
int _ID;
NSString *_name;
int _age;
float _price;
}
//凡是用initXXX开头的都是构造函数
//init方法实际上没有什么特别的,只是遵循命名约定的普通方法
-(id)init;
-(id)initWithID:(int)newID;
-(id)initWithID:(int)newID andAge:(int)newAge;
-(id)initWithID:(int)newID andAge:(int)newAge andPrice:(int)newPrice;
@property int ID;
@property NSString *name;
@property int age;
@property float price;
@end#import "Dog.h"
@implementation Dog
-(id)init
{
return [self initWithID:1001];
}
-(id)initWithID:(int)newID
{
return [self initWithID:newID andAge:20];
}
-(id)initWithID:(int)newID andAge:(int)newAge
{
return [self initWithID:newID andAge:newAge andPrice:80.0];
}
-(id)initWithID:(int)newID andAge:(int)newAge andPrice:(int)newPrice
{
self=[super init];
if(self){
_ID=newID;
_age=newAge;
_price=newPrice;
}
return self;
}
@synthesize ID=_ID;
@synthesize name=_name;
@synthesize age=_age;
@synthesize price=_price;
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "NSString+ReverseString.h"
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Dog *dog1=[[Dog alloc ] init];
dog1.name=@"syj";
NSLog(@"%@",[dog1 name]);
[dog1 setName:@"ldusyj"];
NSLog(@"%@",dog1.name);
}
return 0;
}
先看一下:
@property
@property int age;
等同于 -setAge:(int)newAge;
-(void)age;
@synthesize
@synthesize age=_age;
等同于: -(void)setAge:(int)newAge{
age=newAge;
}
-(int)age{
本文档是python学习笔记与简明教程;为什么用Python作为编程入门语言?每种语言都会有它的支持者和反对者。去Google一下“why python”,你会得到很多结果,诸如应用范围广泛、开源、社区活跃、丰富的库、跨平台等等等等,也可能找到不少对它的批评,格式死板、效率低、国内用的人很少之类。不过这些优缺点的权衡都是程序员们的烦恼。作为一个想要学点编程入门的初学者来说,简单才是最重要的。当学C++的同学还在写链表,学Java的同学还在折腾运行环境的时候,学Pyt
0
return age;
}
在看一下点:1.
dog1.name=@"syj";
等同
[dog1 setName:@"syj"];
NSLog(@"%@",dog1.name);
NSLog(@"%@",[dog1 name]);
注意:
-(void)setAge:(int)newAge
{
NSLog(@"调用了setAge方法:%i",newAge);
self.age = newAge;
}不能在setAge:方法中使用self.age = newAge,相当于在setAge:方法中调用[self setAge:newAge ], 出现死循环
-(int)age
{
NSLog(@"调用了age方法:%i",_age);
return self.age;
}
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号