📄 class2.jc
字号:
/*
* In this file we're going to test using nested objects
*/
import stdlib;
/*
* class Point
*
* - A class must have at least ONE constructor
* - A constructor must DIRECTLY (not by calling another method) initialize ALL member variables
*/
class Point
{
// construct a zero Point
method Point()
{
x = 0;
y = 0;
}
// constructor with coords
method Point( long X, long Y )
{
x = X;
y = Y;
}
// output members
method out()
{
stdlib::Printf("Point members are %d, %d\n", {x, y});
}
// coords
long x;
long y;
}
/*
* class Rect
*
* a Rect consists of two Points
*/
class Rect
{
// construct a zero Rect
method Rect()
{
tl = new Point(); // assign new (zero-) Points
br = new Point();
}
// construct a Rect with two points
method Rect( const Point& topLeft, const Point& bottomRight )
{
tl = topLeft; // copy the given Points
br = bottomRight;
}
// construct a Rect with four coords
method Rect( long left, long top, long right, long bottom )
{
tl = new Point(left, top);
br = new Point(right, bottom);
}
// check if a Point is inside this Rect
method long isInside( const Point& p )
{
return (p.x >= tl.x && p.x < br.x && p.y >= tl.y && p.y < br.y);
}
// output members
method out()
{
stdlib::Printf("Rect members are %d, %d, %d, %d\n", {tl.x, tl.y, br.x, br.y});
}
// data
Point tl; // top left
Point br; // bottom right
}
/*
* function main
*/
function string main()
{
stdlib::Print("* Creating a Rect with coords 50,50,200,200\n");
Rect aRect = new Rect(50, 50, 200, 200);
aRect.out();
stdlib::Print("* Setting Rect members to coords 60,40,150,250\n");
aRect.tl.x = 60;
aRect.tl.y = 40;
aRect.br.x = 150;
aRect.br.y = 250;
aRect.out();
stdlib::Print("* Creating Point A with coords 32,48\n");
Point A = new Point(32, 48);
A.out();
stdlib::Print("* Creating Point B with coords 64,96\n");
Point B = new Point(64, 96);
B.out();
if( aRect.isInside(A) )
stdlib::Print("Point A is inside the Rect.\n");
else
stdlib::Print("Point A is NOT inside the Rect.\n");
if( aRect.isInside(B) )
stdlib::Print("Point B is inside the Rect.\n");
else
stdlib::Print("Point B is NOT inside the Rect.\n");
stdlib::Print("* Assigning Point A to B.\n");
B = A;
B.out();
if( aRect.isInside(B) )
stdlib::Print("Point B is inside the Rect.\n");
else
stdlib::Print("Point B is NOT inside the Rect.\n");
return "Done.";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -