1. 数据结构
语言 / 数据结构 | PHP7 | Python3 | ES6 | Java |
---|---|---|---|---|
备注 | 需安装扩展库DS支持 How to install PHP Data Structures (DS) extension on Ubuntu 16.04 | Vladimir Ivanov | |||
数组 /列表 | $a = [1,2,3] | a = [1,2,3] | a = [1,2,3] | int[] a; 或 int[] a ={1,2,3}; |
集合 | $set = new \Ds\Set([1, 2, 3]) | s = {1, 2, 3} s = set((1, 2, 3)) | const s=new Set() const s=new Set([1,2,3]) | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3)); |
字典 | $d = [ | d = {'foo': 1, 'foo': 2} | const map = new Map([['foo', 1 ], [ 'foo', 2 ]]) | // java9 import static java.util.Map.entry;
// java8 Map<String, String> myMap = new HashMap<String, String>() {{ |
队列 | $q = new \Ds\Queue() | import queue q = queue.Queue() | class Queue extends Array { dequeue() { peek() { isEmpty() { | Queue<Integer> q = new LinkedList<>(); |
元组 | t = (1, 2, 3, 4, 5) | |||
2. 语法
语法/语法 | PHP7 | Python3 | ES6 | Java |
---|---|---|---|---|
继承 | class A extends B | class A(B): | class A extends B | class A extends B |
构造函数 | __construct() | __ init__(self) | constructor() | 同类名 |
析构函数 | __destruct() | __del__(self) | no | no |
静态方法 | public static m() | @staticmethod def m(): | static m() | public static void m(String[] args) |
静态属性 | public static p1 = "p1" | >>> class MyClass: ... i = 3 ... >>> m = MyClass() >>> m.i = 4 >>> MyClass.i, m.i >>> (3, 4) | static p1 = "p1" | static String p1 = "p1"; |
模板字符串 | "i am {$xman}" | f"i am {xman}" | `i am ${xman}` | import java.text.MessageFormat; static String p1 = "p1"; static int i1 = 100; String p2 = MessageFormat.format("A string {0} - {1}.", p1, i1); |
父类方法 | parent::m() | super().m() | super.m() | super.m() |
实例判断 | $a instanceof Class | isinstance(a, Class) | a instanceof Class | a instanceof Class |
for in 循环 | foreach ($items as $item){} | for item in items: | for (let item of items) {} 或 items.forEach(x=>{do sth}) | // 集合,数组 for (int var : set) { System.out.println(var); } // 字典 map.forEach((key, value) -> System.out.println(key + " " + value)); |
浅合并 | array_replace_recursive($d1, $d2) | {**d1, **d2} | Object.assign({}, d1, d2); 或 const r = {...d1, ...d2, ...d3}; | Map<String, Employee> map1 = new HashMap<>(); Map<String, Employee> map2 = new HashMap<>(); map1.put("Henry", new Employee(1L, "Henry")); map1.put("Annie", new Employee(22L, "Annie")); map1.put("John", new Employee(8L, "John")); map2.put("George", new Employee(2L, "George")); map2.put("Henry", new Employee(3L, "Henry")); Map<String, Employee> map3 = new HashMap<>(map1); map2.forEach((key, value) -> map3.merge(key, value, (v1, v2) -> new Employee(v2.getId(), v2.getName()))); map3.forEach((key, value) -> System.out.println(key + " " + value.getId())); |
函数动态调用用 | function area( $area_func_name = 'area'; // Method 1 // Method 2 | def area(length: int, width: int): area_func = globals()["area"] area_func(5, 5) | ||
对象方法动态调用 | class Rectangle: def solve_for(self, name: str): rectangle = Rectangle(3, 5) rectangle.solve_for('area') | |||
参考:
- Stacks and Queues in JS with es6 classes and Array - DEV Community
- Dynamically calling functions in Python... Safely - Daniel Morell