Pass unknown class extending parent, then call functions of parent (Java) -
i know classes passed function extending jcomponent, don't know classes themselves.
my code specific, asking more general. in code attempting pass unknown class extends jcomponent purpose of calling jcomponent method of setfont() on class.
i have written as:
public void setcustomfont(string ttffile, class<? extends jcomponent> jc){ try { graphicsenvironment ge = graphicsenvironment.getlocalgraphicsenvironment(); ge.registerfont(font.createfont(font.truetype_font, new file(ttffile))); } catch (ioexception|fontformatexception e) { e.printstacktrace(); } jc.super.setfont(font.truetype_font); }
jc.super.setfont(
gives error: jc cannot resolved type
while ((jcomponent) jc).setfont(
gives error: cannot cast class<capture#1-of ? extends jcomponent jcomponent>
and jc.setfont(
gives error: the method setfont(int) undefined type class<capture#1-of ? extends jcomponent>
so, cannot figure out how call functions of parent through child passed class.
you don't need pass class function.
just pass object itself:
public void setcustomfont(string ttffile, jcomponent jc){ ... jc.setfont(font.truetype_font); }
the setfont
method not class method, it's instance method, need actual object implements jcomponent in order call method.
by defining parameter jcomponent jc
, method accept objects extend jcomponent, without knowing specific class.
Comments
Post a Comment