博客
关于我
Java与scala之间关系及转换
阅读量:489 次
发布时间:2019-03-07

本文共 949 字,大约阅读时间需要 3 分钟。

Java与Scala最终形成的都是.class类,故本质上代码是一样的,甚至可以相互调用

Scala代码调用Java类

//Java类public class test_1 {    int a;    int b;    public test_1(int a,int b){        this.a=a;        this.b=b;    }    public int getA() {        return a;    }    public void setA(int a) {        this.a = a;    }    public int getB() {        return b;    }    public void setB(int b) {        this.b = b;    }}//Scala调用class geomesa {  def runSparkDemo: String = {    val test=new test_1(1,2);  }}

Scala调用Java的类库

package com.lujinhong.demo.scalaimport scala.io.Sourceimport java.io.PrintWriterimport com.lujinhong.demo.scala.MyJavaClassobject IODemo {  def main(args: Array[String]) = {    val outFile = "/Users/liaoliuqing/Downloads/1.txt"    //将第15行数据输出到一个文件中    writeToFile(outFile, “hello scala")  }  //将内容写入某个文件中,由于scala没有提供写文件的支持,可以使用java.io中的类代替  def writeToFile(outFile: String, content: String) {    val out = new PrintWriter(outFile)    out.write(content)    out.close()  }}

 

转载地址:http://rwxcz.baihongyu.com/

你可能感兴趣的文章
mysql出现错误的解决办法
查看>>
MySQL函数
查看>>
mysql函数汇总之字符串函数
查看>>
mysql函数汇总之数学函数
查看>>
mysql函数汇总之日期和时间函数
查看>>
mysql函数汇总之条件判断函数
查看>>
mysql函数汇总之系统信息函数
查看>>
MySQL函数简介
查看>>
mysql函数遍历json数组
查看>>
MySQL函数(转发)
查看>>
mysql分区表
查看>>
MySQL分层架构与运行机制详解
查看>>
mysql分库分表中间件简书_MySQL分库分表
查看>>
MySQL分库分表会带来哪些问题?分库分表问题
查看>>
MySQL分组函数
查看>>
MySQL分组查询
查看>>
Mysql分表后同结构不同名称表之间复制数据以及Update语句只更新日期加减不更改时间
查看>>
mySql分页Iimit优化
查看>>
MySQL分页查询
查看>>
WebDriverException:未知错误:对于旧版本的 Google Chrome,在 Python 中找不到带有 Selenium 的 Chrome 二进制错误
查看>>