interface Ciało <K> {
K zero();
K dodaj (K a, K b);
K mnóż (K a, K b);
}
public class CiałoDouble implements Ciało <Double>{
@Override
public Double zero() {
return 0.0;
}
@Override
public Double dodaj(Double a, Double b) {
return a+b;
}
@Override
public Double mnóż(Double a, Double b) {
return a*b;
}
}
public class Main {
public static void main (String[] args) {
CiałoDouble f = new CiałoDouble();
Wektor<Double, CiałoDouble> v = new Wektor<Double, CiałoDouble>(f, new Double[]{1.0, 2.0});
v.przemnóżPrzezSkalar(3.0);
}
}
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
public class Wektor <K, F extends Ciało <K>> {
private K[] x;
private F f;
private void alokuj (int rozmiar, K... zbędne) {
Class<?> c = zbędne.getClass().getComponentType();
x = (K[]) Array.newInstance(c, rozmiar);
}
Wektor(F f) { this.f=f; alokuj(0);}
Wektor(int n, F f) {
this.f=f;
alokuj(n);
for (int i=0; i<x.length; i++)
x[i]=f.zero();
}
Wektor(F f, K[] a) {
this.f=f;
x = Arrays.copyOf(a, a.length);
}
Wektor(Wektor<K, F> w) {
this(w.f, w.x);
}
@Override
public int hashCode() {
//return x.hashCode();
return Arrays.hashCode(x);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Wektor <?, ?>)) return false;
Wektor<K, F> v = (Wektor<K, F>) obj;
if (v==null) return false;
if (v.x.length != x.length) return false;
for (int i=0; i<x.length; i++) {
if (Objects.equals(v.x[i], x[i]))
return false;
}
return true;
}
public void przemnóżPrzezSkalar (K a) {
for (int i=0; i<x.length; i++) {
x[i] = f.mnóż(x[i], a);
}
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}