Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449207
  • 博文数量: 80
  • 博客积分: 2301
  • 博客等级: 大尉
  • 技术积分: 884
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-16 20:07
个人简介

I\'m interested in mathematics and Daoism. Welcome to talk about these subjects with me.

文章分类

全部博文(80)

文章存档

2017年(2)

2016年(16)

2015年(4)

2014年(6)

2013年(22)

2012年(2)

2011年(1)

2010年(4)

2009年(20)

2008年(2)

2007年(1)

我的朋友

分类: 大数据

2016-05-27 14:35:18

一、scala代码里面代用java的类
    由于scala语言本身是在jvm上,这一点比较容易。
    以下是将要被调用的java代码:

点击(此处)折叠或打开

  1. package pkg1;

  2. public class Example1 {
  3.     public String name;
  4.     public int age;
  5.     public float salary;
  6.     
  7.     public float getYearIncome(){
  8.         return salary * 12;
  9.     }
  10.     
  11.     public String getDescription(){
  12.         String desc = "name : " + name + "\n"
  13.                 + "age : " + String.valueOf(age) + "\n"
  14.                 + "month salary : " + String.valueOf(salary);
  15.         return desc;
  16.     }
  17.     
  18.     public static void main(String[] args){
  19.         
  20.     }
  21. }

编译以后导出成jar包。
以下是调用以上java类的scala代码:


点击(此处)折叠或打开

  1. package JavaInteraction
  2. import pkg1.Example1
  3. object CallingExample {
  4. def main(args : Array[String]){
  5. val example = new Example1()
  6. example.age = 36
  7. example.salary = 7
  8. example.name = "Nonname"
  9. println("year income: " + example.getYearIncome() + "\n\n")
  10. println("description is: \n" + example.getDescription())
  11. }
  12. }
在scala的工程里面导入刚才导出的jar包。

二、java代码里面调用scala的类
以下是示例的scala代码

点击(此处)折叠或打开

  1. package pkg1

  2. class Car {
  3.    private[this] var year : Int = 0
  4.    private[this] var miles : Int = 0

  5.    def setYear(y : Int){
  6.      year = y
  7.    }
  8.    
  9.    def drive(distance: Int) {
  10.       miles += distance
  11.    }
  12.    
  13.    def getDistance() : Int = {
  14.      miles
  15.    }

  16.    def getDescription() : String = {
  17.      "year:" + year + " miles:" + miles
  18.    }
  19.    
  20.    override def toString() : String = {
  21.      println("-- entering function toString\n")
  22.      "year:" + year + " miles:" + miles
  23.    }
  24. }
导出成jar.
java代码:

点击(此处)折叠或打开

  1. package CallingScala;

  2. import pkg1.Car;

  3. /*
  4.  * XXX:注意一定要把scala的库,加载过来,不然scala类在java里面没法正常使用.
  5.  * */
  6. public class Test {

  7.     public static void main(String[] args){
  8.         Car c = new Car();
  9.         c.setYear(2);
  10.         
  11.         c.drive(100);
  12.         c.drive(200);
  13.         
  14.         System.out.println("car description is: \n" + c.toString());
  15.         //System.out.println("car description is: \n" + c.getDescription());
  16.         
  17.     }
  18. }

注意
    在java工程里面除了要导入刚才从scala工程导出的jar包以外,还要添加scala的库。
    比如我在编译scala工程的时候,使用scala语言是2.10.4,那么,现在在java的工程里面也添加2.10.4版本的以下库:
    scala-compiler.jar
    scala-library.jar
    scala-reflect.jar
    (scala-swing.jar可以不添加)
阅读(1851) | 评论(0) | 转发(0) |
0

上一篇:mysql sql语句示例

下一篇:Annotation示例

给主人留下些什么吧!~~