JRuby中使用接口和抽象类

    要在JRuby中实现java接口,接口 include进来,实现接口方法即可,例如实现java.lang.Runnable接口做线程处理:
require 'java'
include_class 'java.lang.Runnable'
class TestRunnable
    include Runnable
    def initialize(name)
      @name=name     
    end
    def run
      puts "hello,"+@name
    end
end
 
    要在JRuby中继承抽象类,实现其中的抽象方法,方法稍微麻烦点,需要cglib,到 这里下载cglib-nodep-2.1_3.jar,写个通用库abstract_class.rb方便处理:

load 'cglib-nodep-2.1_3.jar'

class Object
  include Java
  include_class "net.sf.cglib.proxy.Enhancer"
  include_class "net.sf.cglib.proxy.NoOp"
  
  class <<self
    def method_missing(mname, *args, &block)
      unless mname == :abstract_impl and respond_to?(:java_class) and JavaLang::reflect::Modifier.isAbstract(JavaLang::Class.for_name(java_class.name).modifiers)
        super
      else
        generate_abstract_impl(args, &block)
      end
    end
    
    private 
    
    def generate_abstract_impl(args, &block)
      factory = Enhancer.new
      factory.setSuperclass(java_class)
      factory.setInterfaces(java_class.interfaces.to_java("java.lang.Class"))
      factory.setCallback(NoOp::INSTANCE)
      
      object_args = args.map { |arg| Java.ruby_to_java(arg) }
      class_arguments = object_args.map {|arg| arg.java_class}.to_java("java.lang.Class")
      generated_class = factory.create(class_arguments, object_args.to_java("java.lang.Object"))
      
      ruby_class = Class.new(generated_class.class)
      ruby_class.class_eval(&block)
      
      return ruby_class.new(*args)
    end
  end
  
end
 
    使用的话,require一下abstract_class,例如我们要继承java.util.TimerTask,实现其中的run方法:
$:.unshift File.join(File.dirname(__FILE__),'.')
require 'java'
require 'abstract_class'
import java.util.TimerTask
import java.util.Timer
timer_task = TimerTask.abstract_impl do
  def run
    puts "timer task"
  end
end

Timer.new.schedule(timer_task, 1000, 1000)
 


你可能感兴趣的:(java,.net,Ruby,jruby)