Java N级嵌套泛型
我想知道是否有允许N级嵌套泛型的东西 如果我有Java N级嵌套泛型,java,generics,Java,Generics,我想知道是否有允许N级嵌套泛型的东西 如果我有 class Example< A > { ... } 类示例{ ... } 有什么窍门吗 Example<Example<Example<Example<Example<Example<...A...>>>>>>> 示例> 程序运行时(即不硬编码) 我之所以想知道这一点,是因为我参数化了一元多项式类的系数-所以我有了多项式,其中C是系数的类型,
class Example< A > {
...
}
类示例{
...
}
有什么窍门吗
Example<Example<Example<Example<Example<Example<...A...>>>>>>>
示例>
程序运行时(即不硬编码)
我之所以想知道这一点,是因为我参数化了一元多项式类的系数-所以我有了多项式Polynomial< Polynomial< Integer > >
多项式<多项式<整数>>
对于两个变量多项式可能没问题,但如果它变成任意数量的变量,我就有麻烦了。我意识到这可能是个糟糕的设计。是否有解决此问题的方法(除了重新启动)
谢谢你的帮助 实际上有一种方法可以使用参数递归来解决这个问题。它并没有给出确切的N嵌套参数化,但它解决了必须键入的问题
Example< Example < Example< ... ... > > > >
如果你有一个多项式
对象,它被参数化为多项式>代码>
当我在脑海中思考这个问题时,我怀疑你是否可能初始化这样一个对象。但一旦我开始编码,它似乎就足够简单了。下面是我“黑”在一起的3个类,它们允许我们操作多元单项式。我会用更好的设计重写(我应该使用公共接口单项式
,其中T
是单项式的系数类型),如果有时间,我会添加注释
如示例所示,您可以得到一个使用字母表中每个字母的多元单项式,而无需键入单项式>代码>26次
单项类:(如5x^2,4a^-5)
变量类:(例如x、y、z)
泛型纯粹是编译时的。这根本没有意义。你应该使用常规多态性。我对实际问题有点困惑,但你说当程序运行时,由于类型擦除,泛型类型信息在运行时丢失。如果您实际上没有尝试在运行时执行此操作,我不知道您所做的为什么不起作用。我从来不知道泛型是“纯编译时”的。我认为这解决了问题。谢谢
public interface RingElem< T extends RingElem< T > > {
public abstract RingElem< T > add( RingElem< T > e );
public abstract RingElem< T > multiply( RingElem< T > e );
}
public class Polynomial implements RingElem< Polynomial > {
///...
}
public class Monomial implements RingElem< Monomial > {
private Monomial m_coefficient;
private Monomial m_base;
private Monomial m_exponent;
public Monomial() {
}
public Monomial( Monomial coefficient , Monomial base , Monomial exponent ) {
if ( base.m_coefficient != null ) {
this.m_coefficient = coefficient.multiply( base.m_coefficient );
if ( base.m_base != null ) {
this.m_coefficient = coefficient.multiply( base.m_base );
}
}
else {
this.m_coefficient = coefficient;
}
this.m_exponent = exponent;
this.m_base = base;
}
@Override
public Monomial add( RingElem< Monomial > e ) {
if ( e instanceof Monomial ) {
Monomial addend = (Monomial) e;
if ( this.m_base.equals( addend.m_base ) && this.m_exponent.equals( addend.m_exponent ) ) {
Monomial rtn = new Monomial( this.m_coefficient.add( addend.m_coefficient ) , this.m_base , this.m_exponent );
return rtn;
}
else {
throw new RuntimeException( "Addition would produce a polynomial, which is not implemented yet!" );
}
}
else {
throw new RuntimeException( "Operation not defined" );
}
}
@Override
public Monomial multiply( RingElem< Monomial > e ) {
if ( e instanceof Constant ) {
Constant c = ( Constant ) e;
return new Monomial( this.m_coefficient.multiply( c ) , this.m_base , this.m_exponent );
}
else if ( e instanceof Monomial ) {
Monomial m = ( Monomial ) e;
return new Monomial( this.m_coefficient.multiply( m.m_coefficient ).multiply( m.m_base ) , this.m_base , this.m_exponent.add( m.m_exponent ));
}
else {
throw new RuntimeException( "Operation not defined" );
}
}
@Override
public String toString() {
if ( this.m_coefficient == null || this.m_base == null || this.m_exponent == null ) {
return "<error>";
}
return this.m_coefficient.toString() + this.m_base.toString() + "^" + this.m_exponent.toString();
}
final public static void main( String[] args ) {
String[] letters = { "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" };
Variable[] v = new Variable[ letters.length ];
for ( int i=0 ; i<letters.length ; i++ ) {
v[ i ] = new Variable( letters[ i ] );
}
Monomial coefficient = new Constant( 1.2 );
Monomial exponent = new Constant( 4.5 );
Monomial test = new Monomial( coefficient , v[ 0 ] , exponent );
for ( int i=1 ; i<letters.length ; i++ ) {
test = test.multiply( new Constant( 1.2 ) );
test = new Monomial( test , v[ i ] , new Constant( (int)(Math.random() * 25 )) );
}
System.out.println( test.toString() );
}
@Override
public boolean equals( Object o ) {
if ( o instanceof Monomial ) {
Monomial m = ( Monomial ) o;
return this.m_coefficient.equals( m.m_coefficient ) && this.m_base.equals( m.m_base ) && this.m_exponent.equals( m.m_exponent );
}
else {
return false;
}
}
}
public class Constant extends Monomial {
private double m_val;
public Constant( double val ) {
super();
this.m_val = val;
}
@Override
public Constant add ( RingElem e ) {
if ( e instanceof Constant ) {
Constant c = ( Constant ) e;
return new Constant( this.m_val + c.m_val );
}
else if ( e instanceof Variable ) {
throw new RuntimeException( "Operation would create a polynomial, which is not defined yet!" );
}
else {
throw new RuntimeException( "Operation not defined" );
}
}
@Override
public Monomial multiply( RingElem e ) {
if ( e instanceof Constant ) {
Constant c = ( Constant ) e;
return new Constant( this.m_val * c.m_val );
}
else if ( e instanceof Variable ) {
return new Monomial( this.clone() , (Variable) e , new Constant( 1 ) );
}
else {
throw new RuntimeException( "Operation not defined" );
}
}
@Override
public String toString() {
return String.valueOf( this.m_val );
}
@Override
public boolean equals( Object o ) {
if ( o instanceof Constant ) {
Constant c = ( Constant ) o;
return this.m_val == c.m_val;
}
else {
return false;
}
}
@Override
public Constant clone() {
return new Constant( this.m_val );
}
}
public class Variable extends Monomial {
final private String m_varName;
public Variable( String varName ) {
this.m_varName = varName;
}
@Override
public Monomial add( RingElem e ) {
if ( e instanceof Variable ) {
if ( e.equals( this ) ) {
return new Monomial( new Constant( 2 ) , this , new Constant( 1 ) );
}
else {
throw new RuntimeException( "Operation would create a polynomial, which is not defined yet!" );
}
}
else if ( e instanceof Monomial ) {
throw new RuntimeException( "Operation would create a polynomial, which is not defined yet!" );
}
else {
throw new RuntimeException( "operation not defined!" );
}
}
@Override
public Monomial multiply( RingElem e ) {
if ( e instanceof Constant ) {
Constant c = ( Constant ) e;
return c.multiply( this );
}
if ( e instanceof Variable ) {
return new Monomial( (Variable) e , this , new Constant( 1 ) );
}
else {
return this.multiply( e );
}
}
@Override
public String toString() {
return this.m_varName;
}
@Override
public boolean equals( Object o ) {
if ( o instanceof Variable ) {
Variable v = ( Variable ) o;
return this.m_varName.equals( v.m_varName );
}
else {
return false;
}
}
}