想象一下,我有一个定义a的抽象类Generic Type.所有子类都将实现此通用类型.
我可以通过声明一个强制子类返回该类型的抽象方法来实现.但有没有更优雅的方法直接从子类的'Class'对象定义实现这一点?
public class GenericTest {
public static void main(String[]args) throws Exception{
Class strClass = ClazzImplString.class;
Class intClass = ClazzImplInteger.class;
Class implTypeStr = ((AbsClazz)strClass.getConstructor().newInstance()).getGenericType();
Class implTypeInt = ((AbsClazz)intClass.getConstructor().newInstance()).getGenericType();
System.out.println("implTypeStr: " + implTypeStr);
System.out.println("implTypeInt: " + implTypeInt);
}
}
abstract class AbsClazz<GenericType> {
abstract Class getGenericType();
}
class ClazzImplString extends AbsClazz<String> {
public ClazzImplString() {}
@Override Class getGenericType() {return String.class;}
}
class ClazzImplInteger extends AbsClazz<Integer> {
public ClazzImplInteger() {}
@Override Class getGenericType() {return Integer.class;}
}
Run Code Online (Sandbox Code Playgroud)
先感谢您
我正在尝试使用CellStylesfor 来配置用于格式化的泛型.HSSFCellsApache-POI 3.11
这是代码的可运行示例.正确应用粗体和边框格式.问题在于背景和前景颜色.
我有什么不对的任何线索?
public class TestSO {
private final static short
MY_LIGHT_BLUE=100,
MY_DARK_BLUE=101,
MY_BLACK=102,
MY_WHITE=103;
public static void main(String[]args) throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook();
setPallete( workbook.getCustomPalette() );
HSSFFont fontNormal = workbook.createFont();
fontNormal.setFontHeightInPoints((short)11);
fontNormal.setFontName("Calibri");
HSSFFont fontBold = workbook.createFont();
fontBold.setFontHeightInPoints((short)11);
fontBold.setFontName("Calibri");
fontBold.setBold(true);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillBackgroundColor(MY_DARK_BLUE);
titleStyle.setFillForegroundColor(MY_WHITE);
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(fontBold);
setTopBotBorder(titleStyle);
HSSFCellStyle fpStyle = workbook.createCellStyle();
fpStyle.setFillBackgroundColor(MY_LIGHT_BLUE);
fpStyle.setFillForegroundColor(MY_BLACK);
fpStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
fpStyle.setFont(fontNormal);
setTopBotBorder(fpStyle);
HSSFSheet sheet = workbook.createSheet("Leyenda");
HSSFCell cell;
cell = sheet.createRow( 1 ).createCell( 1 );
cell.setCellValue("TitleStyle"); …Run Code Online (Sandbox Code Playgroud)