jjzjj

java - Java 转 Scala,如何处理调用父类(super class)构造函数?

coder 2024-04-02 原文

问题摘要 - 如何将其转换为 Scala 类?

问题 - 多个构造函数调用不同的 super 构造函数

Java 类 -

public class ClassConstExample extends BaseClassExample {
    private String xyzProp;
    private string inType = "def";
    private String outType = "def";
    private String flagSpecial = "none";

    public ClassConstExample(final String file, final String header, final String inType, 
             final String outType, final String flag) {
        super(file);
        init(header, inType, outType, flag);
    }

    public ClassConstExample(final String file, final String header, final String inType, 
             final String outType, final String flag, final String mode) {
        super(file, mode);
        init(header, inType, outType, flag);
    }

    public ClassConstExample(final String file, final String header, final String flag){
        super(file);
        //some logic here that's irrelevant to this
        ...
        this.xyxProp = getXYZ(header);
        this.flagSpecial = getFlagSpecial(flag);
    }
    ...
}

我一直在尝试将此类的这些构造函数转换为 Scala 大约一天,但我无法在如何处理以下问题上取得任何进展 - (多个构造函数在 Scala 中调用不同的基类构造函数)。有人介意帮我想办法转换这门课吗?我读过一些地方说在 Scala 中使用标准 super 调用是不可能做到这一点的,那么我该如何实现呢?

最佳答案

必须调用主构造函数,因此任何其他构造函数都必须调用主构造函数或将调用主构造函数的另一个构造函数。 super 的构造函数在主构造函数中作为继承声明的一部分被调用。这意味着您只能调用一个 super 构造函数。

class BaseClassExample(file: String, mode: String) {
  def this(file: String) = this(file, "mode")
}

class ClassConstExample(file: String, header: String, inType: String, outType: String, flag: String, mode: String) extends BaseClassExample(file, mode) {
  def this(file: String, header: String, inType: String, outType: String, flag: String) = this(file, header, inType, outType, flag, "mode")
  def this(file: String, header: String, flag: String) = this(file, header, "inType", "outType", flag)
}
  • 请注意,BaseClassExample 参数是在主构造函数中定义的。
  • 不要使用父类(super class)的默认值,只需将它们明确地放在子类中(如示例中的“模式”)。
  • 因为必须调用主构造函数,所以不需要从每个构造函数调用 init 方法(只需在主构造函数中调用,甚至直接在主体中调用 init)

关于java - Java 转 Scala,如何处理调用父类(super class)构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50357913/

有关java - Java 转 Scala,如何处理调用父类(super class)构造函数?的更多相关文章

  1. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  2. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  3. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  4. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  5. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  6. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  7. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  10. ruby - 分布式事务和队列,ruby,erlang,scala - 2

    我有一个涉及多台机器、消息队列和事务的问题。因此,例如用户点击网页,点击将消息发送到另一台机器,该机器将付款添加到用户的帐户。每秒可能有数千次点击。事务的所有方面都应该是容错的。我以前从未遇到过这样的事情,但一些阅读表明这是一个众所周知的问题。所以我的问题。我假设安全的方法是使用两阶段提交,但协议(protocol)是阻塞的,所以我不会获得所需的性能,我是否正确?我通常写Ruby,但似乎Redis之类的数据库和Rescue、RabbitMQ等消息队列系统对我的帮助不大——即使我实现某种两阶段提交,如果Redis崩溃,数据也会丢失,因为它本质上只是内存。所有这些让我开始关注erlang和

随机推荐