2020-10-29 scala反射,repeated para

2020-10-29  本文已影响0人  linuxScripter

59 /* found three

60      val cons: Array[ _ <: Any] = Class.forName("Proof").getConstructors();               

61      cons foreach println

62      public Proof()

63      public Proof(java.lang.String)

64      public Proof(java.lang.String,java.lang.String,scala.collection.Seq)

65      */

66      val ts = Seq( naiveTheorems(2).asInstanceOf[Theorem], naiveTheorems(3).asInstanceOf[Theorem] )

67      val obj= new Theorem( "zhcGreat",  Class.forName("Proof").getConstructors()(2).newInstance("hi", "2", ts).asInstanceOf[Proof])

67行中,直接把2定死了,不然总报错。 要改进成下面的样子:

70 /*  

71      val cons: Array[ _ <: Any] = Class.forName("Proof").getConstructors();               

72      cons.map( a=> {

73        if( a.toString.contains("Seq") )

74        {

75          val obj= a.newInstance("hi", "2", naiveTheorems(2).asInstanceOf[Theorem]).asInstanceOf[Proof]

76          println(obj.toString)

77        } 

78      }

79      )

80      */

新方案目前编译不过。

上一篇 下一篇

猜你喜欢

热点阅读