1.变量声明。
Android
(访问修饰符) (数据类型) (标识符) = (默认值);
private String str = "";
public int age = 0;
protected float amount = 0.0f;
IOS
(访问修饰符) (数据类型)(指针+标识符)= (OC类型默认值);
@public NSString *str = @"";
@private int age = 0;
@protected float amount = 0.0f;
2.常量申明。
Android
(访问修饰符) (数据类型)(标识符)= (值);
private final String str = "";
public final int age = 0;
protected final float amount = 0.0f;
IOS
(常量修饰符) (标识符)(值);
#define STR @"";
#define AGE 0;
#define AMOUNT 0.0f;
3.类的声明和实现,方法的声明和实现。
Android
(访问修饰符) (类声明) (标识符)
public class Dog {
//属性
public int id;
public String color;
public String name;
//方法(无返回值)(无参数)
public void getInfo(){
//需要处理的数据
}
//方法(带返回值)(带参数)
public String getInfo(int id){
return "this is return content";
}
//方法(带返回值)(带多个参数)
public String getInfo(int id,String name){
return "this is return content";
}
//静态方法
public static void getDetail(){
//处理逻辑。。。
}
IOS
(@interface类声明) (标识符) : (继承类)
@interface Dog : NSObject {
@public int _id;
@public NSString *_color;
@public NSString *_name;
}
//方法(无返回值)(无参数)
- (void)getInfo;
- (NSString *)getDogInfo:(int id);
- (NSString *)getDogInfoMulti:(int id) :(NSString *name);
//类方法,也就是java中的静态方法static
+ (void)getDetail;
@end
(@implementation类实现) (标识符)
@implementation Dog {
}
- (void)getInfo {
//需要处理的数据
}
- (NSString *)getDogInfo:(int id) {
return @"this is return content";
}
- (NSString *)getDogInfoMulti:(int id) :(NSString *name) {
return @"this is return content";
}
+ (void)getDetail {
//处理逻辑。。。
}
4.类的调用
Android
(类名) (标识符) = new (构造参数);
Dog dog = new Dog();
IOS
(类名) (标识符) = [类名 new ];
//2.0之前
Dog *dog = [[Dog alloc] init];
//2.0之后
Dog *dog = [Dog new];
5.属性赋值
Android
(对象标识符) . (属性名) = (值);
dog.name = "coco";
IOS
(对象标识符)->(属性名) = @(值);
//第一种(推荐)
dog->_name = @"coco";
//第二种
(*dog)._name = @"coco";
6.方法调用
Android
返回值类型 返回值标识符 =(对象标识符) . (方法名) (参数,参数N);
//无参数,无返回值
dog.getInfo();
//有参数,有返回值
String desc = dog.getInfo(id,name);
//静态方法(类名.方法名)
Dog.getDetail();
IOS
返回值类型 *返回值标识符 =[(对象标识符) (方法名) : 参数 :参数N];
//无参数,无返回值
[dog getDogInfo];
//有参数,有返回值
NSString *desc = [dog getDogInfoMulti:id :name];
//类方法([类名 类方法名])
[Dog getDetail];
7.日志打印
Android
Log.i("TAG","dog name is: "+dog.name+" dog color is: "+dog.color);
IOS
NSLog(@"dog name is: %@ dog color is: %@",dog->_name,dog->_color);
// %@ 这个取决于数据类型占位符,如果你是NSString对应的占位符就是%@,如果是int对应的占位符就是%i,float对应的就是%f,不过多赘述。
8.Android this和 IOS self
Android
//this是指向的是当前对象。
public class Dog {
String s = "Dog";
public Dog(String s) {
System.out.println("s = " + s);
System.out.println("1 -> this.s = " + this.s);
this.s = s;
System.out.println("2 -> this.s = " + this.s);
}
public static void main(String[] args) {
new Dog("ThisDog!");
}
}
//运行结果:
s = ThisDog!
1 -> this.s = Dog
2 -> this.s = ThisDog!
IOS
/*1.self是个指针,在对象方法中self指向当前对象,在类方法中self指向当前类。
2.在类方法中的self,指向的是代码段中类的isa指针。
3.对象方法中不能使用self调用本类的类方法,只能调用当前对象属性和对象方法。
4.类方法中,不能使用self访问对象的成员,不能直接调用对象方法和属性,因为类方法中的self,代表的是当前类。
*/
@implementation Dog {
NSString *_name;
}
- (NSString *)testPrint{
//处理逻辑
return @"test content";
}
+ (NSString *)classMethods{
//处理逻辑
return @"test content";
}
- (void)eat {
//在对象方法中,self指向的时候new出来当前对象。
//相当[Dog new];
//所以在类方法中是可以直接用self访问类的成员。
NSLog(@"print content===%@",[self testPrint]);
NSLog(@"print name===%@",self->_name);
}
+ (void)eat {
//在类方法中,self指向的是代码段中的isa指针,所以不能直接访问对象的成员
//在类方法中,可以通过self访问其它类方法
//具体要问为什么,可以了解一下栈的存储方式和机制,就能明白为什么
NSLog(@"print content===%@",[self classMethods]);
}
9.方法返回当前类对象
Android
public class Dog {
//返回值类型直接用类名,返回值使用关键字this
public Dog getDog(){
return this;
}
)
IOS
@interface Dog : NSObject {
}
- (instancetype)getDog;
@end
//返回值类型使用关键字instancetype,返回值使用关键字self
@implementation Soldier
- (instancetype)getDog {
return self;
}
@end