クラス (COBOL、Java)

COBOL Java
 
				  
// Closest possible form of a static class
final class Utils
{
private Utils() { }
public static int square(int x)
{
return x * x;
}
}
// Abstract class
public abstract class Shape
{
protected abstract int getArea();
public String toString()
{
return "Area = " + getArea();
}
}
// Sub class
public class Rectangle extends Shape
{
private int _width, _height;
public int getWidth() { return _width; }
public void setWidth(int value) { _width = value; }
public int getHeight() { return _height; }
public void setHeight(int value) { _height = value; }
protected int getArea() { return _width * _height; }
}
// Sub class of a sub class
public class Square extends Rectangle
{
private int _size;
public int getSize() { return _size; }
public void setSize(int value) { _size = value; }
public int getWidth() { return _size; }
public void setWidth(int value) { _size = value; }
public int getHeight() { return _size; }
public void setHeight(int value) { _size = value; }
protected int getArea() { return Utils.square(getSize()); }
public String toString()
{
return "Square, " + super.toString();
}
}

これらの例の一部は、ハーディング大学コンピューター サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。