# 進階型態對應探討

``class Complex(val re: Double, val im: Double) {    def + (that: Complex) = new Complex(re + that.re, im + that.im)    def - (that: Complex) = new Complex(re - that.re, im - that.im)    def * (that: Complex) = new Complex(re * that.re - im * that.im,                                        re * that.im + im * that.re)    def / (that: Complex) = {      val d = that.re * that.re + that.im * that.im      new Complex((re * that.re + im * that.im) / d,                  (im * that.re - re * that.im) / d)    }    override def toString =      re + (if (im < 0) " - " + (-im) else " + " + im) + " i"}``

public class Complex extends java.lang.Object implements scala.ScalaObject{
public Complex(double, double);
public java.lang.String toString();
public Complex \$div(Complex);      // 除 /
public Complex \$times(Complex);    // 乘 *
public Complex \$minus(Complex);    // 減 -
public Complex \$plus(Complex);     // 加 +
public double im();
public double re();
public int \$tag()       throws java.rmi.RemoteException;
}

``public class Main {    public static void main(String[] args) {        Complex c1 = new Complex(3, 2);        Complex c2 = new Complex(2, 3);        Complex rc = c1.\$plus(c2);        System.out.println(rc);    }}``

 Scala Java + \$plus - \$minus * \$times / \$div % \$percent & \$amp | \$bar ^ \$up ! \$bang ~ \$tilde ? \$qmark # \$hash : \$colon \ \$bslash @ \$at > \$greater = \$eq < \$less

``trait Fun {    def f1(x: () => String)    def f2(x: (String) => String)    def f3(x: (String, String) => String)    def f3(x: (String, String, String) => String)}``

import scala.Function0;
import scala.Function1;
import scala.Function2;
import scala.Function3;

public interface Fun {
public void f3(Function3<String, String, String, String> paramFunction3);
public void f3(Function2<String, String, String> paramFunction2);
public void f2(Function1<String, String> paramFunction1);
public void f1(Function0<String> paramFunction0);
}

``package scalatrait Function1[-T1, +R] extends AnyRef { self =>   def apply(v1:T1): R  override def toString() = "<function>"  def compose[A](g: A => T1): A => R = { x => apply(g(x)) }  def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }}``

package scala;

public abstract interface Function1<T1, R> extends ScalaObject {
public abstract <A> Function1<T1, A> andThen(Function1<R, A> paramFunction1);
public abstract <A> Function1<A, R> compose(Function1<A, T1> paramFunction1);
public abstract String toString();
public abstract R apply(T1 paramT1);
}

Function1<String, String> f = new Function1<String, String>() {
public <A> Function1<String, A> andThen(Function1<String, A> f) {
return null;
}
public <A> Function1<A, String> compose(Function1<A, String> f) {
return null;
}
public int \$tag() throws java.rmi.RemoteException {
return 0;
}
public String toString() {
return "<function>";
}
public String apply(String paramT1) {
return paramT1 + "...Orz";
}
};

``trait Ordered {    def compare(that: Any): Int    def < (that: Any) = compare(that) < 0    def <=(that: Any) = (this < that) || (this == that)    def > (that: Any) = !(this <= that)    def >=(that: Any) = !(this < that)}``

// Ordered.class
import scala.ScalaObject;

public interface Ordered extends ScalaObject {

public abstract boolean \$greater\$eq(Object obj);
public abstract boolean \$greater(Object obj);
public abstract boolean \$less\$eq(Object obj);
public abstract boolean \$less(Object obj);
public abstract int compare(Object obj);
}

// Ordered\$class.class
import scala.runtime.BoxesRunTime;

public abstract class Ordered\$class {
public static void \$init\$(Ordered ordered) { }

public static boolean \$greater\$eq(Ordered \$this, Object that) {
return !\$this.\$less(that);
}

public static boolean \$greater(Ordered \$this, Object that) {
return !\$this.\$less\$eq(that);
}

public static boolean \$less\$eq(Ordered \$this, Object that) {
return \$this.\$less(that) || BoxesRunTime.equals(\$this, that);
}

public static boolean \$less(Ordered \$this, Object that) {
return \$this.compare(that) < 0;
}
}

``class Ball(val radius: Int) extends Ordered {    def compare(a: Any) = a match {        ...    }    ...}``

public class Ball implements Ordered, ScalaObject {
....
public int compare(Object a) {
...
}
public boolean \$greater\$eq(Object x\$1) {
return Ordered.class.\$greater\$eq(this, x\$1);
}
public boolean \$greater(Object x\$1) {
return Ordered.class.\$greater(this, x\$1);
}
public boolean \$less\$eq(Object x\$1) {
return Ordered.class.\$less\$eq(this, x\$1);
}
public boolean \$less(Object x\$1) {
return Ordered.class.\$less(this, x\$1);
}
}

``class XD {    def doQuack(d: {def quack: String}) {        println(d.quack)    }}``

public class XD extends java.lang.Object implements scala.ScalaObject{
public XD();
public void doQuack(java.lang.Object);
public int \$tag()       throws java.rmi.RemoteException;
public static java.lang.reflect.Method reflMethod\$Method1(java.lang.Class)
}