//
// main.m
// study2023
//
// Created by zhifei zhu on 2023/7/30.
//
#import <Foundation/Foundation.h>
//声明类 @interface开头 @end结尾
@interface Person : NSObject
{
//声明属性为public,这样对象就能访问
@public
NSString *_name;
int _age;
float _height;
}
//无参方法
- (void)run;
//无返回值,有参
-(void)eat:(NSString *)foodname;
//有返回值,有参
-(int)sum:(int)num1:(int)num2;
//有返回值,有参,提供可读性写法
-(int)sumWith:(int)num1 and:(int)num2;
@end
//实现类 @implementation开头 @end结尾
@implementation Person
//实现方法
- (void) run
{
NSLog(@"i'm running!");
}
-(void)eat:(NSString *)foodname
{
NSLog(@"%@,is delicious",foodname);
}
-(Boolean)jump:(int) account
{
if(account>=1000){
NSLog(@"完成任务!");
return true;
}else
{
NSLog(@"未完成任务!");
return false;
}
}
-(int)sum:(int)num1:(int)num2
{
return num1+num2;
}
-(int)sumWith:(int)num1 and:(int)num2
{
return num1+num2;
}
@end
//声明方法
void test();
//实现方法
// 常用的一些占位符:
// %@:字符串占位符
// %d:整型,BOOL,Boolean也用这个
// %ld:长整型
// %f:浮点型
// %c:char类型
// %%:%的占位符
void test(){
NSLog(@"大家好,好好学习,天天向上!");
BOOL b=YES;
NSLog(@"b=%d",b);
}
//主方法,oc运行开始的地方
int main(int argc, const char * argv[]) {
float f=12.18f;
NSString *str=@"hello,china!";
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSLog(@"float value= %f",f);
NSLog(@"str value= %@",str);
NSLog(str);
//test();
Person *p=[Person new];
// p->_name=@"figo";
// p->_age=18;
// p->_height=1.75f;
(*p)._name=@"rose";
(*p)._age=18;
(*p)._height=1.65f;
NSLog(p->_name);
NSLog(@"%d",p->_age);
NSLog(@"%f",p->_height);
//调用方法
[p run];
[p eat:@"鸡蛋饼"];
Boolean b=[p jump:2000];
NSLog(@"%@",b?@"YES":@"NO");
NSLog(@"%d",b);
int sum=[p sum:1 :2];
NSLog(@"%d",sum);
NSLog(@"%d",[p sumWith:2 and :3]);
}
return 0;
}