Tutorial by Examples: classes

Every case class defines an extractor that can be used to capture the members of the case class when pattern matching: case class Student(name: String, email: String) def matchStudent1(student: Student): String = student match { case Student(name, email) => s"$name has the following...
Inheritance allows classes to define specific behaviour based on an existing class. class Animal def say_hello 'Meep!' end def eat 'Yumm!' end end class Dog < Animal def say_hello 'Woof!' end end spot = Dog.new spot.say_hello # 'Woof!' spot.eat ...
Firstly, make sure that the agent being used has the following attributes in the Manifest.mf: Can-Redefine-Classes: true Can-Retransform-Classes: true Starting a java agent will let the agent access the class Instrumentation. With Instrumentation you can call addTransformer(ClassFileTransformer...
It's a very common extension that allows type classes with multiple type parameters. You can think of MPTC as a relation between types. {-# LANGUAGE MultiParamTypeClasses #-} class Convertable a b where convert :: a -> b instance Convertable Int Float where convert i = fromIntegr...
Type hinting for classes and interfaces was added in PHP 5. Class type hint <?php class Student { public $name = 'Chris'; } class School { public $name = 'University of Edinburgh'; } function enroll(Student $student, School $school) { echo $student->name . ' is b...
If you have an existing class that you'd like to use, perform Step 2 and then skip to Step 5. (For some cases, I had to add an explicit #import <Foundation/Foundation.h to an older ObjC File) Step 1: Add Objective-C Implementation -- .m Add a .m file to your class, and name it CustomObje...
Step 1: Create New Swift Class Add a .swift file to your project, and name it MySwiftObject.swift In MySwiftObject.swift: import Foundation class MySwiftObject : NSObject { var someProperty: AnyObject = "Some Initializer Val" init() {} func someFunction(someArg...
In Python 3.x all classes are new-style classes; when defining a new class python implicitly makes it inherit from object. As such, specifying object in a class definition is a completely optional: Python 3.x3.0 class X: pass class Y(object): pass Both of these classes now contain object in ...
The "static" keyword when referring to a class has three effects: You cannot create an instance of a static class (this even removes the default constructor) All properties and methods in the class must be static as well. A static class is a sealed class, meaning it cannot be inherite...
The typical example is an abstract shape class, that can then be derived into squares, circles, and other concrete shapes. The parent class: Let's start with the polymorphic class: class Shape { public: virtual ~Shape() = default; virtual double get_surface() const = 0; virtual vo...
C++11 Deriving a class may be forbidden with final specifier. Let's declare a final class: class A final { }; Now any attempt to subclass it will cause a compilation error: // Compilation error: cannot derive from final class: class B : public A { }; Final class may appear anywhere in cl...
An abstract class is a class that cannot be instantiated. Abstract classes can define abstract methods, which are methods without any body, only a definition: abstract class MyAbstractClass { abstract public function doSomething($a, $b); } Abstract classes should be extended by a child cla...
Using Razor @functions keyword gives the capability of introducing classes and methods for inline use within a Razor file: @functions { string GetCssClass(Status status) { switch (status) { case Status.Success: return "alert-success&qu...
With this declaration: fun Temporal.toIsoString(): String = DateTimeFormatter.ISO_INSTANT.format(this) You can now simply: val dateAsString = someInstant.toIsoString()
Create generic class instance: var stringRunnable = new Runnable<string>(); Run generic function: function runSafe<T extends Runnable<U>, U>(runnable: T); // Specify the generic types: runSafe<Runnable<string>, string>(stringRunnable); // Let typescript figu...
Implicit classes make it possible to add new methods to previously defined classes. The String class has no method withoutVowels. This can be added like so: object StringUtil { implicit class StringEnhancer(str: String) { def withoutVowels: String = str.replaceAll("[aeiou]", &quo...
Information The ABSTRACT and FINAL additions to the METHODS and CLASS statements allow you to define abstract and final methods or classes. An abstract method is defined in an abstract class and cannot be implemented in that class. Instead, it is implemented in a subclass of the class. Abstra...
The Scala compiler prefixes every argument in the parameter list by default with val. This means that, by default, case classes are immutable. Each parameter is given an accessor method, but there are no mutator methods. For example: case class Foo(i: Int) val fooInstance = Foo(1) val j = fooIn...
Describes ranges of symbols You can enumerate symbols explicitly /[abc]/ # 'a' or 'b' or 'c' Or use ranges /[a-z]/ # from 'a' to 'z' It is possible to combine ranges and single symbols /[a-cz]/ # 'a' or 'b' or 'c' or 'z' Leading dash (-) is treated as charachter /[-a-c]/ # '-' or 'a' o...
Unlike a named class or struct, unnamed classes and structs must be instantiated where they are defined, and cannot have constructors or destructors. struct { int foo; double bar; } foobar; foobar.foo = 5; foobar.bar = 4.0; class { int baz; public: int buzz; ...

Page 2 of 6