有没有办法从 Scala 实例中删除 Trait?
我想知道是否有一个在运行时添加了特征的实例,是否可以从该实例中删除该特征,以便它可以像原来一样运行。
I would like to know if having an instance to which a trait has been added to in runtime, it is possible to remove the trait off the instance, so it can behave as originally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
特征在编译时与类结合。因此不可能在运行时添加/删除特征。
对于您的特定情况,请考虑向您的类添加一个方法:
然后您可以调用
instance.original()
来获取不受任何特征影响的对象的副本。Traits are combined with classes at compile-time. So it is not possible to add/remove traits at run-time.
For your particular case, consider adding a method to your class:
Then you can call
instance.original()
to get back a copy of your object that is not affected by any traits.这是不可能的。该实例属于一个类,该特征的代码是该类的一部分。
如果 JVM 支持类似“class+traits”的东西,那么理论上就有可能恢复它。
It is not possible. That instance belongs to a class of which the trait's code is part of.
If JVM supported something like "class+traits", then it would be theoretically possible to revert that.