Tutorial by Examples: class

class Car { public position: number = 0; private speed: number = 42; move() { this.position += this.speed; } } In this example, we declare a simple class Car. The class has three members: a private property speed, a public property position and a public met...
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 ...
Imports System.Reflection Public Class PropertyExample Public Function GetMyProperties() As PropertyInfo() Dim objProperties As PropertyInfo() objProperties = Me.GetType.GetProperties(BindingFlags.Public Or BindingFlags.Instance) Return objProperties End Fun...
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...
Class constants provide a mechanism for holding fixed values in a program. That is, they provide a way of giving a name (and associated compile-time checking) to a value like 3.14 or "Apple". Class constants can only be defined with the const keyword - the define function cannot be used in...
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...
A static class is lazily initialized on member access and lives for the duration of the application domain. void Main() { Console.WriteLine("Static classes are lazily initialized"); Console.WriteLine("The static constructor is only invoked when the class is first accessed&...
Constants can be defined inside classes using a const keyword. class Foo { const BAR_TYPE = "bar"; // reference from inside the class using self:: public function myMethod() { return self::BAR_TYPE; } } // reference from outside the class using <Class...
A generic class with the type parameter Type class MyGenericClass<Type>{ var value: Type init(value: Type){ self.value = value } func getValue() -> Type{ return self.value } func setValue(value: Type){ self.value = value }...
Managed resources are resources that the runtime's garbage collector is aware and under control of. There are many classes available in the BCL, for example, such as a SqlConnection that is a wrapper class for an unmanaged resource. These classes already implement the IDisposable interface -- it's u...
It's important to let finalization ignore managed resources. The finalizer runs on another thread -- it's possible that the managed objects don't exist anymore by the time the finalizer runs. Implementing a protected Dispose(bool) method is a common practice to ensure managed resources do not have t...
public class UnsafeLoader { public static Unsafe loadUnsafe() { return Unsafe.getUnsafe(); } } While this example will compile, it is likely to fail at runtime unless the Unsafe class was loaded with the primary classloader. To ensure that happens the JVM should be loaded with...
In C# (and .NET) a string is represented by class System.String. The string keyword is an alias for this class. The System.String class is immutable, i.e once created its state cannot be altered. So all the operations you perform on a string like Substring, Remove, Replace, concatenation using + o...
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...
Access ModifierVisibilityInheritancePrivateClass onlyCan't be inheritedNo modifier / PackageIn packageAvailable if subclass in packageProtectedIn packageAvailable in subclassPublicEverywhereAvailable in subclass There was once a private protected (both keywords at once) modifier that could be appli...
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...
A common use case for extension methods is to improve an existing API. Here are examples of adding exist, notExists and deleteRecursively to the Java 7+ Path class: fun Path.exists(): Boolean = Files.exists(this) fun Path.notExists(): Boolean = !this.exists() fun Path.deleteRecursively(): Bool...
With this declaration: fun Temporal.toIsoString(): String = DateTimeFormatter.ISO_INSTANT.format(this) You can now simply: val dateAsString = someInstant.toIsoString()
Two Random class created at the same time will have the same seed value. Using System.Guid.NewGuid().GetHashCode() can get a different seed even in the same time. Random rnd1 = new Random(); Random rnd2 = new Random(); Console.WriteLine("First 5 random number in rnd1"); for (int i = 0...

Page 6 of 28