Add a slightly simpler example of implemeting a Java interface in Lisp.

TODO Needs further documentation.
parent 388e4c91
public interface BankAccount {
public int getBalance();
public void deposit(int amount);
public void withdraw(int amount);
}
\ No newline at end of file
import org.armedbear.lisp.Interpreter;
import org.armedbear.lisp.Symbol;
import org.armedbear.lisp.Packages;
import org.armedbear.lisp.JavaObject;
import org.armedbear.lisp.LispObject;
public class BankExampleMain
{
static public void main(String argv[]) {
Interpreter interpreter = Interpreter.createInstance();
interpreter.eval("(load \"bank-account.lisp\")");
org.armedbear.lisp.Package defaultPackage
= Packages.findPackage("CL-USER");
Symbol bankAccountImplSymbol
= defaultPackage.findAccessibleSymbol("*BANK-ACCOUNT-IMPL*");
LispObject value = bankAccountImplSymbol.symbolValue();
Object object = ((JavaObject) value).getObject();
BankAccount account = (BankAccount) object;
System.out.println("Initial balance: " + account.getBalance());
account.withdraw(500);
System.out.println("After withdrawing 500: " + account.getBalance());
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ ABCL Examples Building and Running Instructions
To compile
cmd$ javac -cp ../../dist/abcl.jar Main.java
cmd$ javac -cp ../../dist/abcl.jar Main.java BankAccountMain.java
where the "../../../dist/abcl.jar" represents the path to your
abcl.jar file, which is built via the Ant based build. This path
......@@ -27,3 +27,4 @@ or in Windows use:
where "Main" is the initial class to run in your Java program.
(defparameter *bank-account-impl*
(let ((balance 1000))
(jinterface-implementation
"BankAccount"
"getBalance"
(lambda ()
balance)
"deposit"
(lambda (amount)
(let ((amount (jobject-lisp-value amount)))
(setf balance (+ balance amount))))
"withdraw"
(lambda (amount)
(let ((amount (jobject-lisp-value amount)))
(setf balance (- balance amount)))))))
(defun get-bank-account-impl ()
*bank-account-impl*)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="antlib:org.apache.tools.ant"
default="compile">
<!--
XXX not working
<import file="../../build.xml"/>
<target name="compile" depends="abcl-master.abcl.jar"
-->
<target name="compile">
<javac
classpath="../../dist/abcl.jar"
srcdir=".">
</javac>
</target>
</project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment