Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
Class is a set of data and procedure.
Kinx is a prototype based language but class keyword is prepared as a syntax sugar of creating an object instance.
The class definition is basically that there are the case of having arguments and the case of no arguments.
class A { /* ... */ }
class A() { /* ... */ }
class A(a, b) { /* ... */ }
In the above case, class A is the same as class A().
The : is used for inheritance of class.
About arguments for inheritance, it is also same as a simple class definition, and the arguments with ( and ) are able to be omitted.
The examples of inheritance are below.
class A : B { /* ... */ }
class A() : B { /* ... */ }
class A : B(true) { /* ... */ }
class A(a, b) : B(b) { /* ... */ }
The class object is instantiated by new operator.
When instantiating the object, you have to use a function call style like new ClassName().
It makes no sense with new ClassName only.
class A { /* ... */ }
var a = new A();
this and @this means the instance itself.
class A {
public flagOnAlt() {
this.flagOn();
}
public flagOn() {
this.flag = true;
}
}
You can use @ instead of this., which is this plus dot.
By this, you can write the following for the above code.
class A {
public flagOnAlt() {
@flagOn();
}
public flagOn() {
@flag = true;
}
}
The variable declared in class scope is a local private variable.
A property of this is a public variable.
class A {
var flag_ = false; // a local private variable.
public flagOn() {
@flagOnActual();
}
public flagOnActual() {
@flag = true; // a public variable.
}
}
var a = new A();
a.flagOn();
System.println(a.flag ? "true" : "false"); // => true
Methods are defined with public and private keyword.
The function defined with private is used only in a local scope, which is inside a class scope.
On the other hand, the function defined with public can be accessed by an instance’s property.
class A {
private method1() {
/* ... */
}
public method2() {
method1(); // Okay, the `method1` is accessible inside the class scope.
/* ... */
}
private method3() {
method2(); // The `method2` is also accessible inside the class scope.
@method2(); // The `method2` is also accessible through `this`.
}
}
var a = new A();
// a.method1(); // Error, the `method1` is accessible through the instance.
a.method2(); // Okay, the `method2` is accessible through the instance.
The class keyword will define some special methods.
initializeIf initialize method is defined, the initialize method is automatically called right after instantiated. The initialize method can be defined either public or private.
class A {
private initialize() {
System.println("called");
}
}
var a = new A(); // => "called"
instanceOfA class instance has an instanceOf method automatically.
This method accepts a class name as a variable, and it will return true if a target object is an instance of a specified class or a base class.
class C {};
class B {};
class A : B {};
var a = new A();
System.println(a.instanceOf(A) ? "true" : "false"); // => "true"
System.println(a.instanceOf(B) ? "true" : "false"); // => "true"
System.println(a.instanceOf(C) ? "true" : "false"); // => "false"
class A(name) {
public method() {
System.println(name, " called.");
}
}
class B : A("class B") {}
class C() : A("class C") {}
class D(name) : A("class "+name) {}
new B().method();
new C().method();
new D("D").method();
class B called.
class C called.
class D called.
class A {
var flag_ = false; // a local private variable.
public flagOn() {
@flagOnActual();
}
public flagOnActual() {
@flag = true; // a public variable.
}
}
var a = new A();
a.flagOn();
System.println(a.flag ? "true" : "false"); // => true
true
initialize methodclass A {
private initialize() {
System.println("called");
}
}
var a = new A(); // => "called"
called
instanceOf methodclass C {};
class B {};
class A : B {};
var a = new A();
System.println(a.instanceOf(A) ? "true" : "false"); // => "true"
System.println(a.instanceOf(B) ? "true" : "false"); // => "true"
System.println(a.instanceOf(C) ? "true" : "false"); // => "false"
true
true
false