二、java 与 scala相互调用
2024-10-21 14:28:06
介绍: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实现
最新文章
- hdu 3746 Cyclic Nacklace
- Modernizr.js:为HTML5和CSS3而生!
- python函数动态参数详解
- leetcode 138. Copy List with Random Pointer ----- java
- C#相关时间DateTime格式化
- Android开源项目发现---ActionBar篇(持续更新)
- VMware宿主机和虚拟机的网络连接问题
- 怎样在C++中获得完整的类型名称
- Java设计模式之认识阶段
- smarty的学习计划(1)
- 如何快速的理解JavaScript闭包?
- es6学习笔记--字符串&;数值&;数组&;函数&;对象的扩展
- php session序列化攻击面浅析
- 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图
- 小白的python之路11/14
- 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
- Odoo 堆积木似的软件构建
- Android WebView 实现网页缩放
- Vue2.5开发去哪儿网App 第五章笔记 上
- javascript 关于局部变量和全局变量