Previous Topic Next topic Print topic


Generics, Defining - in COBOL and Java

Defining Generics in Managed COBOL

*> Generic class
class-id Point3D using T.

*> With constraints
class-id Point3D using T.
constraints.
   constrain T implements type IComputable.
   
*> Generic interface
interface-id IComputable using T.
*> Generic method
method-id MakePoint3D static public using T.
procedure division using by value
       x as T y as T z as T
       returning ret as type Point3D[T].
       
*> With constraints
method-id MakePoint3D static public using T.
constraints.
   constrain T implements type IComputable.
*> Generic value type
valuetype-id container using T Y.
   01 a T public.
   01 b Y public.
end valuetype.

*> With constraints
valuetype-id container using T Y.
constraints.
   constrain T implements type IComputable.

*> Generic iterator, use a parametrised class
class-id a using T.

iterator-id itg static.
01 val binary-long value 1.
procedure division using by value x as T yielding y as T.
    perform until val = 50
        add 1 to val
        set y to x *> Just for example!
        goback
    end-perform
end iterator.
       
end class.

*> To use, parametrise the class
perform varying thg through type a[string]::itg("Dog")
    display thg
end-perform

Defining Generics in Java

// Generic class
class Point3D <T>

// Generic class with constraints
class Point3D_c <T extends IComputable>

//Generic interface
interface IComputable<T>
                 
// Generic method
public static <T> Point3D MakePoint3D(T x, T y, T z) 
{
    return new Point3D(x, y, z);
}
	
// Generic method with constraints
public static <T extends IComputable> Point3D MakePoint3D(T x, T y, T z) 
{
	return new Point3D<T>(x, y, z);
}

// No value type in Java, so generic simple class instead
class container<T, Y>
{
    public T a;
    public Y b;
}

// No value type in Java, so simple generic class with constraints
class container<T extends IComputable, Y> 


// Generic iterator, use a parameterised class
class a<T>
{
    public static <T> T itg(T x)
    {
        int val = 1;
        while (val < 50)
        {
            val++;
            return x;
        }
    }
}


for (String dog : a<string>) 
    System.out.println(dog);

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.

Previous Topic Next topic Print topic