Java 带有类的静态类属性<;MyClass>;我的班级
我在这里做了一点实验 假设我有一门课:Java 带有类的静态类属性<;MyClass>;我的班级,java,Java,我在这里做了一点实验 假设我有一门课: static class MyClass { static String property = "myProperty"; } 以及一种方法: public static void myMethod0(Class<MyClass> clazz) { try { MyClass myClass = clazz.newInstance(); System.out.println (myClass.p
static class MyClass {
static String property = "myProperty";
}
以及一种方法:
public static void myMethod0(Class<MyClass> clazz) {
try {
MyClass myClass = clazz.newInstance();
System.out.println (myClass.property);
} catch (Exception e) {
e.printStackTrace();
}
}
myMethod0将在这里工作,但是,我正在创建一个新实例以访问该属性。
由于属性是静态的,我应该能够访问它们,而无需实际创建任何实例。例如,当您访问静态属性时,即MyClass.property
总结如下:
通过使用clazz=MyClass.Class类,是否可以达到MyClass的静态属性
谢谢
**
编辑:
**
要正确看待上述情况以及我实际想要实现的目标:
public static class PDF_1 { public static PDF_1 it = new PDF_1();
static String contentType = "application/pdf";
static String fileEnding = "pdf";
}
static void myMethod0(PDF_1 pdf) {
System.out.println(pdf.fileEnding);
}
public enum PDF_2 {it;
static String contentType = "application/pdf";
static String fileEnding = "pdf";
}
static void myMethod1(PDF_2 pdf) {
System.out.println(pdf.fileEnding);
}
public static void main(String[] args ) {
myMethod0(PDF_1.it); // Works fine! However very verbose because of public static PDF_1 it = new PDF_1();
myMethod1(PDF_2.it); // Works fine! Somewhat verbose though because of the "it" keyword
}
关于我试图用它来完成的全部想法是,我经常看到人们声明大量字符串,即:
static class Constants {
static String PDF_CONTENT_TYPE = "application/pdf";
static String PDF_FILE_ENDING = "pdf";
static String HTML_CONTENT_TYPE = "text/html";
static String HTML_FILE_ENDING = "html";
}
// There is no way knowing what type the method actually wants. Is it contentType, fileEnding or something entirely different ?
public void myMethod(String str) {
}
我试图实现的是允许您传递一个主类/枚举,即:PDF,并且该方法本身将决定它将使用什么。调用方只知道传递什么,一个PDF或HTML类/枚举。我也在寻找这种重构友好的东西。不使这一创造的宣告复杂化也是令人感兴趣的。我发现完整的枚举和类一样显眼,而且很难阅读。IDEA是指我只是将父对象“PDF”和“HTML”中的两个字符串分组。枚举:
public enum SomeType {
PDF("application/pdf", "pdf"), HTML(...);
String contentType;
String fileEnding;
// Constructor ...
}
不允许您声明方法并指定此方法需要HTML内容。只有枚举类型是SomeType类型。存在“某人”将SomeType.PDF传递给该方法的风险。我使用enum和类所做的工作看起来像是一个noob解决方案,Java语言应该提供这样的功能,还是已经提供了
这有意义吗 您可以使用反射
System.out.println(myClass.getDeclaredField("property").get(null));
get
-方法通常需要一个实例才能从中获取属性,但由于属性
是静态的,您可以向它传递一个null我希望您是这么想的-我用hamcrest编写了测试-所以不要奇怪;)
谢谢,这对我来说是一种新的方法。但是,从重构的角度来看,它并不可靠,因为它使用字符串访问属性。我完全同意你的看法。反思是最后的手段。然而,据我所知,如果没有思考,这是不可能的:/你不应该像那样更改发布的问题。整个编辑是一个新问题,应该作为一个问题发布。请随意链接到这个问题中的新问题。是的,我知道。。但我不能删除这一个,然后发布一个新的。人们经常会抱怨问同一个问题好几次,所以我不想再发了。谢谢你的回复。然而,这将是它的突出部分。还不清楚我们要传递给这个方法的类型是HTML值还是PDF值。一个会创建PDF,另一个不会。从声明静态字段(wish是懒惰的程序员的方式)到完全扩展的enum(~class或多或少)。枚举很难读取,它们就像一个类。我想我会找到我的解决方案,上面的枚举稍微“轻一点”。枚举是为懒惰的程序员准备的,你只需指定一次,然后在整个程序中使用它们,如果有更多的值或没有值,你就不会伤脑筋。您只需处理类以获得它们所需的值(如fileending、charset或任何需要的值)。我不知道您更喜欢哪个枚举,但是如果您想使用2个值,您应该使用与我类似的枚举。您可以将enum放入一个新文件中,并将其导入到需要它的类中。。。
System.out.println(myClass.getDeclaredField("property").get(null));
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
public class TestEnumEnding {
@Test
public void whenMethod1WithFiletypeHtmlWeShouldGetStringText_Html() throws Exception {
// Arrange
String contentType = null;
// Act
contentType = method1(Filetype.HTML);
// Assert
assertThat(contentType, is("text/html"));
}
@Test
public void whenMethod1WithFiletypePdfWeShouldGetapplication_pdf() throws Exception {
// Arrange
String contentType = null;
// Act
contentType = method1(Filetype.PDF);
// Assert
assertThat(contentType, is("application/pdf"));
}
private static String method1 (Filetype anyFiletype) {
return anyFiletype.getContentType();
}
public enum Filetype {
HTML("html","text/html"), PDF("pdf","application/pdf");
private final String fileEnding;
private final String contentType;
private Filetype(String fileEnding, String contentType) {
this.fileEnding = fileEnding;
this.contentType = contentType;
}
public String getFileEnding() {
return this.fileEnding;
}
public String getContentType() {
return this.contentType;
}
}
}
public enum PDF { it;
static String contentType = "application/pdf";
static String fileEnding = "pdf";
}
static void myMethod1(PDF pdf) {
System.out.println(pdf.fileEnding);
}
public static void main(String[] args ) {
myMethod1(PDF.it); // Works fine! Somewhat verbose though because of the "it" keyword. Not 100% ideal!
}