不同語言的語法區(qū)別

其實都差不多......

語言 方法調(diào)用 變量調(diào)用
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
語言 變量定義 數(shù)組定義
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); 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容