Java 里 switch 语句是在 JDK 7 开始支持的,现在可以支持 char, byte, short, int, Character, Byte, Short, Integer, String
类型。
今天读 flutter 代码的时候无意间发现 switch 语句对 String 的支持是通过 hashCode
配合 equals
实现的 😂。
鉴于 Blog 好久没更新了,特地来水一篇。不多说了,看下代码。
这是源码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public Object (Object proxy, Method method, Object[] args) throws Throwable { switch (method.getName()) { case "addView": addView(args); return null; case "removeView": removeView(args); return null; case "updateViewLayout": updateViewLayout(args); return null; } try { return method.invoke(mDelegate, args); } catch (InvocationTargetException e) { throw e.getCause(); } }
|
这是反编译过的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public Object (Object proxy, Method method, Object[] args) throws Throwable { String var4 = method.getName(); byte var5 = -1; switch(var4.hashCode()) { case -1148522778: if (var4.equals("addView")) { var5 = 0; } break; case 931413976: if (var4.equals("updateViewLayout")) { var5 = 2; } break; case 1098630473: if (var4.equals("removeView")) { var5 = 1; } }
switch(var5) { case 0: this.addView(args); return null; case 1: this.removeView(args); return null; case 2: this.updateViewLayout(args); return null; default: try { return method.invoke(this.mDelegate, args); } catch (InvocationTargetException var6) { throw var6.getCause(); } } }
|
近期评论