[英]java - Variable for variable names ? or is there anything similar to MACRO?
是否可以為變量名稱創建變量? 像這樣
class example {
String VAR1 = "id";
String VAR2 = "mark";
public foo()
{
int VAR1;
int VAR2;
}
}
或者說我嵌套了 class 父子。
class Parent {
char a;
//some variables
class child {
//char b = Parent.a;
}
}
我有很多這樣的 class。所以我需要用 MACRO 之類的東西替換“Parent.a”中的“父”,這樣我就不想單獨更改其子類中的每個“父”。
有什么可能的解決方案嗎?
您的第一個示例在 Java 中是不可能的。如果您更改代碼,第二個示例是有效的:
class Parent {
char a;
class child {
char b = a;
}
}
class Parent {
// Main parent
char a;
//some other fields
// The child class
class Child {
// The main child
char a = Parent.a;
//some other fields
}
// this is what you need
public char getChild() {
Child child = new Child();
return child.a;
}
}
如果您想要一種方便的方法來重命名變量,那么使用 IDE 就 可以做到這一點。
這種東西是(或者,至少,應該是)在編程語言設計的 realm 之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.