Repository navigation
Abstract & Anonymous
An abstract class is a class that primarily exists to be extended and can contain incomplete behavior. Within one you can define abstract methods which will error if not overridden by the inheritor.
abstract class Animal:
public abstract makeNoise()
class Pig extends Animal:
public override makeNoise():
broadcast "oink"Animal can't be created directly without its methods being implemented, but we can create an instance of Pig.
set {_animal} to new Animal # error :(
set {_pig} to new Pig
{_pig}::makeNoise() # oinkNow creating a whole class structure to extend this template can be annoying which is where anonymous classes can be useful. You can override any method on an abstract class within the new Class section itself. This effectively creates a temporary subclass of Animal.
set {_dog} to new Animal:
public override makeNoise():
broadcast "bark"
{_dog}::makeNoise() # barkYou can also use local variables from the previous context within anonymous methods, the state of which is saved once the instance is created.
abstract class Weapon:
public abstract use(p: player)set {_speed} to 5
set {_arrow} to spectral arrow
set {_bow} to new Weapon:
public override use(p: player):
shoot {_arrow} from {_p} with speed {_speed}
set {_speed} to 6 # does not affect the bowThe access and override keywords are optional in anonymous classes because only overrides are possible in this scenario, and because it might make your code less unnecessarily verbose. You may want to use different combinations depending on what information is important to somebody reading your code.
# all of these are valid and mean the same
public override method():
public method():
override method():
method():- Methods will revert back to their non-overridden state when the server restarts for anonymous instances that were saved (fields will always be saved). If you need to keep implemented behavior between restarts use a concrete class instead. Anonymous classes are best suited for custom registries etc.
- In anonymous classes,
supertargets the code from the abstract class you are implementing, not whatever that class may be extending since it behaves like a subclass. - The access-type of anonymous methods cannot be altered from the origin, you'll get an error since it's impossible for the parser to predict.
- Both abstract and concrete methods can be overridden anonymously, as long as the class is abstract. You can't add completely new methods or fields to an anonymous class.