javaclass

tests/ValueSubclass.java

149:cd2ca8849c5a
2005-01-23 Paul Boddie Added a note about the Java API implementation.
     1 public class ValueSubclass extends Value {     2     public Value tmp;     3      4     /**     5      * Test of subclass initialisation with super usage and foreign object initialisation.     6      */     7     public ValueSubclass(int x) {     8         super(x);     9         tmp = new Value(42);    10     }    11     12     /**    13      * Test of overriding.    14      */    15     public void setValue(int x) {    16         this.value = -x;    17     }    18     19     /**    20      * Test of overriding and super methods.    21      */    22     public int add(int x) {    23         return super.add(-x);    24     }    25     26     /**    27      * Test of objects as arguments.    28      */    29     public void setValueObject(Value v) {    30         this.value = v.getValue();    31     }    32     33     /**    34      * Test program.    35      */    36     public static void main(String[] args) {    37         SubclassValue sv = new SubclassValue(686);    38         if (sv.getValue() == 686) {    39             System.out.println("sv.getValue() correct: " + sv.getValue());    40         } else {    41             System.err.println("sv.getValue() failed!");    42         }    43             44         ValueSubclass vs = new ValueSubclass(109);    45         if (vs.tmp.getValue() == 42) {    46             System.out.println("vs.tmp.getValue() correct: " + vs.tmp.getValue());    47         } else {    48             System.err.println("vs.tmp.getValue() failed!");    49         }    50         if (vs.getValue() == 109) {    51             System.out.println("vs.getValue() correct: " + vs.getValue());    52         } else {    53             System.err.println("vs.getValue() failed!");    54         }    55         vs.setValue(404);    56         if (vs.getValue() == -404) {    57             System.out.println("vs.getValue() correct: " + vs.getValue());    58         } else {    59             System.err.println("vs.getValue() failed!");    60         }    61         if (vs.add(404) == -808) {    62             System.out.println("vs.add(404) correct: " + vs.add(404));    63         } else {    64             System.err.println("vs.add(404) failed!");    65         }    66         vs.setValueObject(sv);    67         if (vs.getValue() == 686) {    68             System.out.println("vs.getValue() correct: " + vs.getValue());    69         } else {    70             System.err.println("vs.getValue() failed!");    71         }    72     }    73 }    74     75 // This would confuse a simple importer since it should be read before Value in    76 // alphabetical order.    77     78 class SubclassValue extends Value {    79     public SubclassValue(int x) {    80         super(x);    81     }    82 }    83     84 // vim: tabstop=4 expandtab shiftwidth=4