📄 arrayobj.jc
字号:
/*
* arrayobj.jc
*
* A little demonstration how to use the built-in array object.
*/
import stdlib;
/*
* function main
*
* The type of data in an array is typeless 'var', so you normally cannot access
* any object members from an object in an array. This is because the compiler
* can't know there are Person instances in the array. Hence, writing:
*
* myArray[item].Print();
*
* will give you a compiler error message. However, there is a way around this
* restriction, by using the scope operator:
*
* myArray[item].Person::Print();
*
* This will tell the compiler to interprete the item in the array as an instance
* of class Person and call it's member function Print().
* Of course this would fail BADLY at runtime, if we would use this expression
* while there are no Person objects, but other data in the array!
* Unless you use an array that only contains a single type, you should use the
* typeof() operator to verify your array element has the correct type, before
* calling the function:
*
* if( typeof(myArray[item]) == typeof(Person) )
* myArray[item].Person::Print();
*/
function string main()
{
// create an empty address book
array& AddressBook = new array;
// add some people to our address book
AddressBook += new Person( "John", "Small Street 476", new Date(22, 7, 1968) );
AddressBook += new Person( "Annie", "Broadway 75", new Date(16, 2, 1975) );
AddressBook += new Person( "Harold", "Village Road 12", new Date(30, 10, 1961) );
// print our address book
PrintAddresses( AddressBook );
// Make a second addressbook, using an alternative way:
array& addresses = {
new Person( "Joshua", "Market Street 45", new Date(7, 4, 1983) ),
new Person( "Maria", "5th Avenue", new Date(4, 2, 1975) ),
new Person( "Homer", "Springfield Road 6", new Date(10, 12, 1963) )
};
// merge the new addresses with those from our address book and print result (this operation won't modify our address book!)
PrintAddresses( AddressBook.Insert(addresses, 1) );
// insert yet another address at the top of the list (again this does not modify our address book!)
Person rox = new Person( "Roxanne", "Red Lantern Boulevard 125", new Date(15, 8, 1981) );
PrintAddresses( AddressBook.InsertItem(rox, 0) );
// or remove entry 1 from our address book
PrintAddresses( AddressBook.Remove(1, 1) );
// or extract a sub array:
PrintAddresses( AddressBook.SubArray(1, 4) );
// if we wanted to make all these changes permanently:
AddressBook = AddressBook.Insert(addresses, 1).InsertItem(rox, 0).Remove(1, 1).SubArray(1, 4);
PrintAddresses( AddressBook );
return "";
}
/*
* function PrintAddresses
*
* Print our addresses to console
*/
function PrintAddresses( const array& addresses )
{
stdlib::Print("Addresses:\n");
for( long item = 0; item < addresses.length; item++ )
{
addresses[item].Person::Print();
}
stdlib::Print("\n");
}
/*
* class Date
*
* Simple class for storing a date
*/
class Date
{
method Date(long d, long m, long y)
{
m_day = d;
m_month = m;
m_year = y;
}
method const string& convertor()
{
// we construct an array and use the ToString() method
array& date = { m_month, m_day, m_year };
return date.ToString("%d/%d/%d");
}
// data
long m_day;
long m_month;
long m_year;
}
/*
* class person
*
* Simple class describing a person
*/
class Person
{
method Person(const string& name, const string& address, const Date& birthday)
{
m_name = name;
m_address = address;
m_birthday = birthday;
}
method Print()
{
stdlib::Print( "Name: " + m_name + "\n" );
stdlib::Print( "Address: " + m_address + "\n" );
stdlib::Print( "Birthday: " + m_birthday + "\n\n" );
}
// data
string m_name;
string m_address;
Date m_birthday;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -