// 1. 方法集合
public static List<MethodInfo> SameUnitParaMethod { get; set; }
// 2. 類型關係字典:方法名 -> 關係類型
public static Dictionary<string, TypeRelation> TypeRelation { get; set; }
// 3. 複雜類別
public class UnitScalarTypeRelation {
public string ReturnType { get; set; }
public SubElementType SubTypeType = SubElementType.AscOrder;
public List<string> UnitParaType { get; set; }
}
public class MethodRegistry {
// 對應 List
public static List<Method> sameUnitParaMethod = new ArrayList<>();
// 對應 Dictionary
public static Map<String, TypeRelation> typeRelation = new HashMap<>();
public static void register(Method m) { sameUnitParaMethod.add(m); }
public static void registerRelation(String name, TypeRelation rel) { typeRelation.put(name, rel); }
}
public class UnitScalarTypeRelation {
private String returnType;
private SubElementType subTypeType = SubElementType.AscOrder;
private List<String> unitParaType = new ArrayList<>();
public String getReturnType() { return returnType; }
public void setReturnType(String returnType) { this.returnType = returnType; }
public SubElementType getSubTypeType() { return subTypeType; }
public void setSubTypeType(SubElementType subTypeType) { this.subTypeType = subTypeType; }
public List<String> getUnitParaType() { return unitParaType; }
public void setUnitParaType(List<String> unitParaType) { this.unitParaType = unitParaType; }
}
public enum SubElementType {
AscOrder, Combination, DisOrder
}
public enum TypeRelation {
DIV_LENGTH_SPEED, // 長度 ÷ 速度
// 其他關係...
}
| C# | Java | 關鍵差異 |
|---|---|---|
List<T> | List<T> (ArrayList/LinkedList) | 介面導向,建議宣告 List<T> 實例化 ArrayList |
Dictionary<K,V> | Map<K,V> (HashMap/TreeMap) | 同理 |
public T Prop { get; set; } | private T prop; public T getProp() {...} public void setProp(T v) {...} | Java 無屬性語法,需手寫 Getter/Setter (Lombok @Data 可自動生成) |
struct | class (final + record Java 14+) | Java 無值型別,用不可變類別模擬 |
static 建構函式 | static { ... } 靜態區塊 | 初始化靜態集合用 |
// 註冊方法
MethodRegistry.register(MyClass.class.getMethod("add", int.class, int.class));
MethodRegistry.registerRelation("DIV_LENGTH_SPEED", TypeRelation.DIV_LENGTH_SPEED);
// 取用
List<Method> methods = MethodRegistry.sameUnitParaMethod;
TypeRelation rel = MethodRegistry.typeRelation.get("DIV_LENGTH_SPEED");
善用 Lombok @Data/@Builder 可大幅減少樣板碼,讓 Java 程式碼更接近 C# 屬性體驗。