freemarker:强制转换"Interface";反对真正的秘密阶层 [英] freemarker : cast "Interface" Object to the real concret class
问题描述
我迭代到一个IColonne对象列表.但是我需要转换为concret类以获取特定的属性.我的列表中有"Colonne"和"ColonneGroup"对象.
I iterate to a list of IColonne object. But I need to cast to the concret class to get specific attribut. My list have "Colonne" and "ColonneGroup" object.
IColonne界面:
The IColonne interface :
public interface IColonne {
String getFtlName();
int getWidthPx(final int tableSize);
}
科隆大学的具体班级:
public class Colonne implements IColonne {
....
}
ColonneGroup的具体课程:
The ColonneGroup concret class :
public class ColonneGroup implements IColonne {
private List<String> texts;
}
我需要访问文本"属性.但是我有唯一的" IColonne.因此,我需要转换为ColonneGroup.我该怎么办?
I need to access to access to the "texts" attribut. But I have "only" IColonne. So I need to cast to ColonneGroup. How can I do this?
推荐答案
我想您需要从FreeMarker模板访问texts
.由于FTL是动态键入的,因此您不需要强制转换.如果对象具有公共getTexts()
方法,则只能访问成员.可以使用colonne.texts??
进行测试(也可以是非null),也可以使用!
运算符为其提供默认值.所以可能是这样的:
I suppose you need to access texts
from a FreeMarker template. As FTL is dynamically typed, you need no casting for that. You can just access the member, if the object has a public getTexts()
method. Wether it has it (and that it's also non-null) you can test as colonne.texts??
, or you can use the !
operator to give it a default. So it could be something like this:
<#list colonnes as colonne>
...
<#if colonne.texts??>
Do something with colonne.texts
</#if>
...
<#!-- Or if you would #list the texts anyway: -->
<#list colonne.texts!>
<p>We have some texts here:
<ul>
<#items as text>
<li>${text}</li>
</#items>
</ul>
</#list>
...
</#list>
这篇关于freemarker:强制转换"Interface";反对真正的秘密阶层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!