⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interface.jc

📁 The JILRunOnly project is a simple command-line application written in ANSI-C that is intended to de
💻 JC
字号:

/*
 *	interface.jc
 *
 *	The language allows (currently very simple) inheritance from a single interface.
 *	An interface can only have methods, no static functions and no data. All methods
 *	in an interface are implicitly PURE, meaning they cannot have an implementation.
 *	There is a dedicated keyword for declaring interfaces, 'interface'.
 *	When inheriting, you can choose between these variants:
 *
 *	class CHello : Base					// selects class or interface inheritance from Base type
 *	class CHello : interface Base		// ensures 'Base' is an interface
 *	class CHello : class Base			// ensures 'Base' is a class
 *
 *	Deriving from classes is reserved for future implementation.
 */

import stdlib;

/*
 *	IHello
 *
 *	declare a simple interface
 */

interface IHello
{
	method			IHello();
	method			Set(long);
	method long		Get();
}

/*
 *	CHello
 *
 *	The CHello class implements our IHello interface.
 *	It has a long value as data member.
 */

class CHello : IHello
{
	method			CHello()			{ m_Value = 0; }
	method			Set(long v)			{ m_Value = v; }
	method long		Get()				{ return m_Value; }
	method			NotInInterface()	{ m_Value *= 5; }

	long m_Value;
}

/*
 *	CFoo
 *
 *	The CFoo class implements our IHello interface, too.
 *	It has a string as data member.
 */

class CFoo : IHello
{
	method			CFoo()				{ m_String = ""; }
	method			Set(long v)			{ m_String = stdlib::Ltoa(v); }
	method long		Get()				{ return stdlib::Atol(m_String); }

	string m_String;
}

/*
 *	main
 */

function string main()
{
	// create a CHello instance
	CHello& hello = new CHello();
	// call it's methods
	hello.Set( 101 );
	long x = hello.Get();
	hello.NotInInterface();

	// print IHello information
	IHelloPrint( hello );

	// create a CFoo instance
	CFoo& foo = new CFoo();
	// call it's methods
	foo.Set( 202 );
	long y = foo.Get();

	// print IHello information
	IHelloPrint( foo );

	// get an IHello reference from a CHello
	IHello& ihello = hello;

	// call it's methods
	ihello.Set( 303 );
	long z = ihello.Get();

	// print IHello information
	IHelloPrint( ihello );

	return "";
}

/*
 *	IHelloPrint
 */

function IHelloPrint(const IHello& hello)
{
	stdlib::Print("* The given object is a ");
	if( typeof(hello) == typeof(CHello) )
	{
		stdlib::Print("CHello\n");
	}
	else if( typeof(hello) == typeof(CFoo) )
	{
		stdlib::Print("CFoo\n");
	}
	stdlib::Printf( "* IHello::Get() returns %d\n", hello.Get() );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -