NSString:
int main(){
NSString *str=@"itcast";//最簡單的創建字符串的方式。
//char *name="itcast";
NSLog(@"%@",str);
//NSLog(@"%s",name);
int age = 15;
int no = 5;
NSString *name = @"jack";
int size=[name length];//字符串長度=4,算的是字數
//通過一個字符串和一些參數組合成另一個字符串
NSString *newStr = [NSString stringwithFormat:@"My age is %d and no is %d and name is%@",age,no,name);
NSLog(@" %@",newStr);
return 0;
}
/*
設計一個類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開二次根
*/
#import <Foundation/Foundation.h>
#import <math.h>
//聲明:
@interface Point2D : NSObject
{
double _x;
double _y;
}
//聲明_x setget
- (void)setX:(double)x;
- (double)x;
//聲明_y setget
- (void)setY:(double)y;
- (double)y;
//聲明:設計一個對象方法,同時設置x和y。
- (void)set:(double)x andY:(double)y;
//聲明:設計一個對象方法,計算根其他點的距離。
- (double)distanceWithAnotherPoint:(Point2D *)another;
//聲明:設計一個類方法,計算兩個點之間的距離。
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end
//實現:
@implementation Point2D
//實現_x setget
- (void)setX:(double)x
{
_x=x;
}
- (double)x
{
return _x;
}
//實現_y setget
- (void)setY:(double)y
{
_y=y;
}
- (double)y
{
return _y;
}
//實現同時設置x和y
- (void)set:(double)x andY:(double)y
{
[self setX:x];//只需要在setget方法中一次性修改就可以
[self setY:y];
//_x=x;_y=y;當更改setget方法時,不方便修改。
}
//聲明:設計一個對象方法,計算根其他點的距離。
- (double)distanceWithAnotherPoint:(Point2D *)another
{
//1.用於類方法調用對象方法
double firstpingfang=pow([self x]-[another x],2);
double secondpingfang=pow([self y]-[another y], 2);
return sqrt(firstpingfang+secondpingfang);
//2.利用對象方法調用類方法
//return [Point2D distanceBetweenPoint1:self andPoint2:another];
}
//聲明:設計一個類方法,計算兩個點之間的距離。
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
//1利用類方法調用對象方法
return [p1 distanceWithAnotherPoint:p2];
/*2.用於對象方法嗲用類方法
//(x1-x2)平方firstpingfang
double firstpingfang=pow([p1 x]-[p2 x],2);
//(y1-y2)平方secondpingfang
double secondpingfang=pow([p1 y]-[p2 y], 2);
//firstpingfang+secondpingfang開根號resultkaigenhao
return sqrt(firstpingfang+secondpingfang);
*/
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
XCode特色註釋:
#pragma mark 今天學到這
#pragma mark - 姓名的setget
#pragma mark 姓名的set
#pragma mark 姓名的get
點語法:
Person *p =[Person new];
p.age=10;//點語法的本質還是方法調用,不是訪問成員變量,自動轉換成[p setAge:10];
int a=p.age;//此時自動轉化成[p age];
//若直接訪問成員變量,OC只有通過->來訪問
但是注意:
使用點方法前必須得有setget方法
- (void) setAge:(int)age
{
_age = age;
NSLog(@"setAge");
//self.age=age;//會引發死循環
}
- (int)age
{
NSLog(@"age");
return _age;//或者return self->age;
//return self.age;//死循環
}
@public //在任何地方都能直接訪問對象的成員變量
int _age;
@private //只能在當前類的對象方法中直接訪問
int _height;
@protected //能在當前類和子類的對象方法中直接訪問(默認)
int _weight;
聲明的頭h文件中:
@property int age;//可以自動生成setter getter 方法
相當於:
- (void)setAge:(int) age;
- (void)age;
實現的m文件中:
@synthesize age = _age;//可以自動實現setter getter 方法,並且會訪問_age這個成員變量,
@synthesize age;//沒有聲明_age則自動生成聲明age(不是_age)
@synthesize weight = _weight , height = _height;
極簡:xcode4.4之後只需要一行代碼就完成3件事:
1.聲明setter getter
2.實現setter getter
3.聲明_開頭的成員變量
@property int age;//缺點在於自動生成的成員變量是私有的@private
若希望子類能訪問成員變量,需要加一個聲明:
@protected
int _age;
注意:
>若手動實現了setter,編譯器就只會自動生成getter方法
>若手動實現了getter,編譯器就只會自動生成setter方法
>若同時手動實現了setter和getter,編譯器就不會自動生成不存在的成員變量