介绍:scala 是简化的java,运行于jvm的脚步语言。Java和scala通过各自编译器编译过都是jvm能解析class文件。本文介绍java和scala如何互调

 scala的源代码文件是以.scala为后缀的,编译后的文件class文件。
 
1.scala使用JDK的java类
 
用法:import 要使用java类,在scala代码内Java类
 
import java.lang.reflect._
import  java.util.Date//导入java类
 
println("Today is " + new Date())
 
val methods = getClass.getMethods()//使用java类
methods.foreach {
  methods: Method => println(methods.getName)
}
 
2.scala使用java类
 
用法:跟scala使用JDK的java类方法一样,但是java类编译后的类文件,存放在scala文件所在的包目录下,才能通过编译和使用
 
object UseInvestment {
 
  def main(args: Array[String]) {
    val investment = new Investment("xyz Corporation", InvestmentType.STOCK) //java类
    println(investment.getClass())
 
    val theYield = investment.`yield` // yield是scala关键字,所以要`括起来`
    println("theYield is " + theYield);
  }
 
}
 
Investment和InvestmentType分别是Java类
 
public class Investment {
    private String investmentName;
    private InvestmentType investmentType;
 
    public Investment(String name, InvestmentType type) {
        investmentName = name;
        investmentType = type;
    }
 
    public int yield() {
        return 0;
    }
}
 
public enum InvestmentType {
    SHORT_TERM,
    BOND,
    STOCK
}
 
3. java使用scala
 
用法:Car的class类要在classPath内,import 要使用scala类。
在Java代码内按java语法来是使用scala类
 
//--------------scala类--------------------------
class Car(val year: Int) {
  private[this] var miles: Int = 0
 
  def drive(distance: Int) {
    miles += distance
  }
 
  override def toString(): String = "year:" + year + " miles:" + miles
}
 
//-----------java类-------------------------
public class UseCar {
 
    public static void main(String[] args) {
        Car car = new Car(2009);
 
        System.out.println(car);
        car.drive(10);
        System.out.println(car);
    }
 
}
 
note:
Car的class类要在classPath内,UseCar 才能通过编译
 
4.java使用伴生对象
 
------------伴生对象----------------
class Buddy {
  def greet() {
    println("Hello from Buddy class")
  }
}
 
object Buddy {
  def greet() {
    println("Hello from Buddy object")
  }
}
 
---------------java类------------------------
public class BuddyUser {
    public static void main(String[] args) {
        new Buddy().greet();//使用伴生类
        Buddy$.MODULE$.greet();//使用伴生对象
    }
}
 
note:
java使用伴生对象: 伴生对象名$.MODULE$.方法名();//
 
5.java使用trait
 
//--------------------trait----------------
trait Writable {
  def write(message: String): Unit
}
 
//--------java---------------------------------
public class AWritable implements Writable {
    public void write(String message) {
        System.out.println(message);
    }
 
    public static void main(String[] args) {
        Writable writable = new AWritable();
        writable.write("依晨");
    }
}
 
note:
java使用trait,通过implements 实现trait,当接口使用
必须按接口标准。trait方法有自己实现,Java类使用trait不会使用trait实现

 

 

最新文章

  1. hdu 3746 Cyclic Nacklace
  2. Modernizr.js:为HTML5和CSS3而生!
  3. python函数动态参数详解
  4. leetcode 138. Copy List with Random Pointer ----- java
  5. C#相关时间DateTime格式化
  6. Android开源项目发现---ActionBar篇(持续更新)
  7. VMware宿主机和虚拟机的网络连接问题
  8. 怎样在C++中获得完整的类型名称
  9. Java设计模式之认识阶段
  10. smarty的学习计划(1)
  11. 如何快速的理解JavaScript闭包?
  12. es6学习笔记--字符串&数值&数组&函数&对象的扩展
  13. php session序列化攻击面浅析
  14. 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图
  15. 小白的python之路11/14
  16. Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class
  17. Odoo 堆积木似的软件构建
  18. Android WebView 实现网页缩放
  19. Vue2.5开发去哪儿网App 第五章笔记 上
  20. javascript 关于局部变量和全局变量

热门文章

  1. ElasticSearch(十八)初识分词器
  2. leetcode第一刷_Permutations
  3. valuestack,stackContext,ActionContext.之间的关系以及action的数据在页面中取得的方法
  4. 微信小程序高度设置为100%
  5. maven导入项目时,缺少部分source folder
  6. vim有用的快捷键
  7. 0521 HTML基础
  8. 暑假集训第一周比赛C题
  9. NGINX中遇到SELinux 13:permission denied
  10. DL一(ML基础知识)