-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticMethods.lox
51 lines (32 loc) · 926 Bytes
/
staticMethods.lox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Shows use of Static methods in Jlox*
// Cannot use `super` and `this` inside static methods
// Run from base directory using "java -jar Lox.jar sampleFiles/staticMethods.lox"
class A {
init(){
this.name = "Instance of A";
}
// Start a method name with "static" to declare a static method.
static sayHi(){
print "HI";
}
static sayHiHi(){
print "HI,HI";
}
}
// Use static method with the class like this:
print "Calling static method with the class itself : ";
A.sayHi();
// Use static method with instances of the class:
print "";
print "Calling static method with an instance of the class itself : ";
A().sayHiHi();
// static methods are also inherited by the subclass and its objects
// B is a subclass of A
class B < A {
}
print "";
print "Calling static method with the subclass: ";
B.sayHiHi();
print "";
print "Calling static method with an instance of the subclass: ";
B().sayHiHi();