不同语言的语法区别

其实都差不多......

语言 方法调用 变量调用
objective-c [self funA:5] self.var
swift self.funA(para: 5) self.var
c++ this->funA(5) this->var
java this.funA(5) this.var
js this.funA(5) this.var
python funA(var) var = 1
php $this->funA(5) $this->var
语言 变量定义 数组定义
objective-c int a=1 int ary[] = {1, 2, 3, 4, 5}
swift var a = 10 var ary = [1, 2, 3, 4, 5]
c++ int a = 10 int ary[] = {1, 2, 3, 4, 5}
java int a = 10 int[] ary = {1, 2, 3, 4, 5}
python a = 10 ary = [1, 2, 3, 4, 5]
php $a = 10 $ary = array(1, 2, 3, 4, 5);

类定义使用
oc:

@interface MyClass : NSObject
@property (nonatomic, assign) int intProperty;
- (void)printSomething:(NSString *)something;
@end

// MyClass.m 文件
#import "MyClass.h"

@implementation MyClass
- (void)printSomething:(NSString *)something {
    NSLog(@"%@", something);
}
@end

// 使用MyClass
MyClass *myObject = [[MyClass alloc] init];
myObject.intProperty = 2;
[myObject printSomething:@"Hello, World!"]; 
NSLog(@"%d", myObject.intProperty); 

swift:

class MyClass {
    let intProperty: Int = 2
    func printSomething(_ something: String) {
        print(something)
    }
}

let myObject = MyClass()
myObject.printSomething("Hello, World!") 
print(myObject.intProperty) 

c++:

#include <iostream>
#include <string>

class MyClass {
public:
    int intProperty = 2;

    void printSomething(std::string something) {
        std::cout << something << std::endl;
    }
};

int main() {
    MyClass myObject;
    myObject.printSomething("Hello, World!"); 
    std::cout << myObject.intProperty << std::endl; 
    return 0;
}

java:

public class MyClass {
    public int intProperty = 2;
  
    public void printSomething(String something) {
        System.out.println(something);
    }

    public static void main(String[] args) {
        MyClass myObject = new MyClass();
        myObject.printSomething("Hello, World!"); // 输出 "Hello, World!"
        System.out.println(myObject.intProperty); // 输出 "2"
    }
}

js:

class MyClass {
  constructor() {
    this.intProperty = 2;
  }

  printSomething(something) {
    console.log(something);
  }
}

let myObject = new MyClass();
myObject.printSomething("Hello, World!"); 
console.log(myObject.intProperty); 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。