Tutorial by Examples: classes

Declaration: class MyGenericClass<T1, T2, T3, ...> { // Do something with the type parameters. } Initialisation: var x = new MyGenericClass<int, char, bool>(); Usage (as the type of a parameter): void AnotherMethod(MyGenericClass<float, byte, char> arg) { ... } ...
When an object graph is finalized, the order is the reverse of the construction. E.g. the super-type is finalized before the base-type as the following code demonstrates: class TheBaseClass { ~TheBaseClass() { Console.WriteLine("Base class finalized!"); } } ...
To make a class support collection initializers, it must implement IEnumerable interface and have at least one Add method. Since C# 6, any collection implementing IEnumerable can be extended with custom Add methods using extension methods. class Program { static void Main() { va...
Type constraints are able to force a type parameter to implement a certain interface or class. interface IType; interface IAnotherType; // T must be a subtype of IType interface IGeneric<T> where T : IType { } // T must be a subtype of IType class Generic<T> where T...
Developers can be caught out by the fact that type inference doesn't work for constructors: class Tuple<T1,T2> { public Tuple(T1 value1, T2 value2) { } } var x = new Tuple(2, "two"); // This WON'T work... var y = new Tuple<int, string>(2, "t...
An abstract class is a class marked with the abstract keyword. It, contrary to non-abstract class, may contain abstract - implementation-less - methods. It is, however, valid to create an abstract class without abstract methods. An abstract class cannot be instantiated. It can be sub-classed (exten...
Data model public class Item { private String name; public String getName() { return name; } } Layout XML You must import referenced classes, just as you would in Java. <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="h...
min, max, and sorted all need the objects to be orderable. To be properly orderable, the class needs to define all of the 6 methods __lt__, __gt__, __ge__, __le__, __ne__ and __eq__: class IntegerContainer(object): def __init__(self, value): self.value = value def __rep...
When type is called with three arguments it behaves as the (meta)class it is, and creates a new instance, ie. it produces a new class/type. Dummy = type('OtherDummy', (), dict(x=1)) Dummy.__class__ # <type 'type'> Dummy().__class__.__class__ # <type 'type'> It is po...
A singleton is a pattern that restricts the instantiation of a class to one instance/object. For more info on python singleton design patterns, see here. class SingletonType(type): def __call__(cls, *args, **kwargs): try: return cls.__instance except AttributeErr...
Python 2.x2.2.0 New-style classes were introduced in Python 2.2 to unify classes and types. They inherit from the top-level object type. A new-style class is a user-defined type, and is very similar to built-in types. # new-style class class New(object): pass # new-style instance new = N...
In the same module Inside a module named "MyModule", Xcode generates a header named MyModule-Swift.h which exposes public Swift classes to Objective-C. Import this header in order to use the Swift classes: // MySwiftClass.swift in MyApp import Foundation // The class must be `public`...
If MyFramework contains Objective-C classes in its public headers (and the umbrella header), then import MyFramework is all that's necessary to use them from Swift. Bridging headers A bridging header makes additional Objective-C and C declarations visible to Swift code. When adding project files, ...
To allow the use of in for custom classes the class must either provide the magic method __contains__ or, failing that, an __iter__-method. Suppose you have a class containing a list of lists: class ListList: def __init__(self, value): self.value = value # Create a set of al...
class MultiIndexingList: def __init__(self, value): self.value = value def __repr__(self): return repr(self.value) def __getitem__(self, item): if isinstance(item, (int, slice)): return self.__class__(self.value[item]) r...
Assuming the page includes an HTML element like: <p class="small-paragraph"> This is a small <a href="https://en.wikipedia.org/wiki/Paragraph">paragraph</a> with a <a class="trusted" href="http://stackexchange.com">link</a>...
Let's say we have two classes, Cat and Dog. class Cat def eat die unless has_food? self.food_amount -= 1 self.hungry = false end def sound puts "Meow" end end class Dog def eat die unless has_food? self.food_amount -= 1 self.hungry = f...
Pseudo-classes are keywords which allow selection based on information that lies outside of the document tree or that cannot be expressed by other selectors or combinators. This information can be associated to a certain state (state and dynamic pseudo-classes), to locations (structural and target p...
#import <Foundation/Foundation.h> @interface Car:NSObject { NSString *CarMotorCode; NSString *CarChassisCode; } - (instancetype)initWithMotorValue:(NSString *) motorCode andChassisValue:(NSInteger)chassisCode; - (void) startCar; - (void) stopCar; @end @implementation Car...
Classes in MATLAB are divided into two major categories: value classes and handle classes. The major difference is that when copying an instance of a value class, the underlying data is copied to the new instance, while for handle classes the new instance points to the original data and changing val...

Page 1 of 6