简体   繁体   English

通过反射将类名称传递给Scala中的超类构造函数

[英]Passing class name to super class constructor in Scala via reflection

How can I pass the name of a class to its super class constructor and is it even possible? 如何将类的名称传递给其超类构造函数,甚至有可能? I'm thinking about something like this: 我正在考虑这样的事情:

class TestClass extends SuperTestRequiringName(TestClass.getClass.getName) {}

This would be better than passing the name as a hard-coded string because consistency is preserved during rename refactorings. 这比将名称作为硬编码字符串传递更好,因为在重命名重构过程中会保持一致性。

Even better would be the actual runtime class so I would get the name of a subclass if possible. 更好的是实际的运行时类,因此,如果可能的话,我将获得子类的名称。 But this doesn't work as well: 但这并不能正常工作:

class TestClass extends SuperTestRequiringName(this.getClass.getName) {}

I'm not asking how to access the subclass name inside the super class at runtime in general. 我一般不问如何在运行时访问超类内部的子类名称。 It is important that the class name is available as a super constructor argument so the super class can pass it along to its own super class (from a library I can't change). 重要的是,可以将类名用作超级构造函数参数,以便超级类可以将其传递给自己的超级类(从我无法更改的库中)。

class TestClass extends SuperTestRequiringName(classOf[TestClass].getName)

In Scala classOf[A] is the same as A.class in Java. 在Scala中, classOf[A]与Java中的A.class相同。

getClass is a method, which you only can call on instances of classes. getClass是一个方法,您只能调用类的实例。

I'm not sure if I correctly understand your question or not but you can use ClassTag for this purpose. 我不确定我是否正确理解您的问题,但是您可以为此使用ClassTag For example; 例如;

import scala.reflect._
class SuperTestRequiringName(name: String) {}
class TestClass extends SuperTestRequiringName(classTag[TestClass].runtimeClass.getName)

I hope it helps! 希望对您有所帮助!

I don't think your problem has a good solution as is. 我认为您的问题没有一个好的解决方案。 The way JVM memory model works, you can't use this before the call to the base constructor, even to get your own class name. JVM内存模型的工作方式是,在调用基本构造函数之前,甚至无法获得自己的类名,都不能使用this If the reason you inherit from the library's class is to re-use its functionality, rather than the need to actually use your classes in the same contexts the base class is used, I'd suggest replacing inheritance with composition which is the right solution for such a problem anyway. 如果您从库的类继承的原因是重用其​​功能,而不是需要在使用基类的相同上下文中实际使用您的类,那么建议您用组成替换继承,这是解决问题的正确方法无论如何,这样的问题。 The code would go like this: 代码如下:

case class Wrapper(s: String)

class LibraryClass(val w: Wrapper) {
  def someUsefulStuff(n: Int): String = ???

  def someNotUsefulStuff(n: Int): String = ???
}

class Base() {
  private val delegate:LibraryClass = new LibraryClass(Wrapper(this.getClass.getName))

  // expose useful methods from the delegate
  // depending on logic maybe decorate them as protected
  def someUsefulStuff(n: Int): String = delegate.someUsefulStuff(n)
}

class LeafA extends Base

class LeafB extends Base

See this example online . 在线查看此示例。

If you really need to inherit from the LibraryClass to be used in those contexts, you probably will have to make your subclasses pass the string explicitly as in @ygor solution. 如果确实需要继承自LibraryClass以便在这些上下文中使用,则可能必须像@ygor解决方案一样,使子类显式传递字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scala 的 case 类及其构造函数参数传递给超类 - Scala's case class and its constructor parameter passing to super classes 通过反射实例化 Scala 类/案例类 - Instantiating Scala class/case class via reflection 在 Scala 反射中使用参数构造函数创建类 - Creating class with parameter constructor in Scala reflection 通过反射修改不可变的scala类字段 - Modify immutable scala class field via reflection 如何使用零参数构造函数创建具有通过Java反射绑定的上下文的Scala类的新实例? - How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor? Scala 2.10反思:类内case类的构造函数 - Scala 2.10 reflection: constructor of case class inside a class Java类的Scala继承:选择要调用的超级构造函数 - Scala inheritance from Java class: select which super constructor to call Scala:如何设置抽象超类构造函数中定义的实例字段? - Scala: How to set an instance field defined in an abstract super class constructor? 将Java转换为Scala,如何处理调用超类构造函数? - Converting Java to Scala, how to deal with calling super class constructor? Scala反射:如何获取给定类的类的字符串名称 - Scala Reflection: how to get class given String name of the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM