Java Reference
In-Depth Information
// Get john's full name from the Person class
var johnSuperFullName = johnSuper.getFullName();
// Print Names
print("Extended full name:", johnFullName);
print("Super full name:", johnSuperFullName);
Extended full name: JOHN JACOBS
Super full name: John Jacobs
Notice that the
getFullName()
method of the extended
Person
class refers to
the variable named
john
that has been declared outside the function. The following
statement assigns the reference of an object that can be used to call the methods of the
superclass of the
john
object.
var _super_ = Java.super(john);
The overridden method calls the
Person
class'
getFullName()
method, converts the
name to uppercase, and returns it. The code get the superclass reference again, as shown:
// Get the reference of john's super
var johnSuper = Java.super(john);
Finally, the code prints the returned value from the
Person
class and the extended
Person
class to show that you were really able to call the superclass method.
■
instead of using
Java.super(obj)
method to get the superclass object reference
of
obj
and call methods on it, you can also use
obj.super$MethodName(args)
syntax
to call the method of the superclass of the object named
obj
. For example, in the example,
you could have used
john.super$getFullName()
to call the
getFullName()
method
of the
Person
class on the object
john
.
Tip
Using Lambda Expressions
JavaScript supports anonymous functions that can be used as lambda expressions. The
following is an anonymous function that takes a number as an argument and returns its
square:
function (n) {
return n * n;
}
Search WWH ::
Custom Search