Tutorial by Examples: class

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...
class TrivialClass {} A class consists at a minimum of the class keyword, a name, and a body, which might be empty. You instantiate a class with the new operator. TrivialClass tc = new TrivialClass();
All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated. /** * Brief summary of this class...
The getClass() method can be used to find the runtime class type of an object. See the example below: public class User { private long userID; private String name; public User(long userID, String name) { this.userID = userID; this.name = name; } } pu...
The fundamental part of most classes is its constructor, which sets up each instance's initial state and handles any parameters that were passed when calling new. It's defined in a class block as though you're defining a method named constructor, though it's actually handled as a special case. cla...
As mentioned in the introduction, a decorator is a function that can be applied to another function to augment its behavior. The syntactic sugar is equivalent to the following: my_func = decorator(my_func). But what if the decorator was instead a class? The syntax would still work, except that now m...
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...
You can define a new class using the class keyword. class MyClass end Once defined, you can create a new instance using the .new method somevar = MyClass.new # => #<MyClass:0x007fe2b8aa4a18>
There are several special variable types that a class can use for more easily sharing data. Instance variables, preceded by @. They are useful if you want to use the same variable in different methods. class Person def initialize(name, age) my_age = age # local variable, will be destroyed ...
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...
// zoo.php class Animal { public function eats($food) { echo "Yum, $food!"; } } $animal = new Animal(); $animal->eats('meat'); PHP knows what Animal is before executing new Animal, because PHP reads source files top-to-bottom. But what if we wanted to create ...
// Animal.php class Animal { public function eats($food) { echo "Yum, $food!"; } } // zoo.php require 'Animal.php'; $animal = new Animal; $animal->eats('slop'); // aquarium.php require 'Animal.php'; $animal = new Animal; $animal->eats('shrimp'); H...
// autoload.php spl_autoload_register(function ($class) { require_once "$class.php"; }); // Animal.php class Animal { public function eats($food) { echo "Yum, $food!"; } } // zoo.php require 'autoload.php'; $animal = new Animal; $animal->e...
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...
You can use a custom toJSON method and reviver function to transmit instances of your own class in JSON. If an object has a toJSON method, its result will be serialized instead of the object itself. 6 function Car(color, speed) { this.color = color; this.speed = speed; } Car.prototype.to...
Instance variables are unique for each instance, while class variables are shared by all instances. class C: x = 2 # class variable def __init__(self, y): self.y = y # instance variable C.x # 2 C.y # AttributeError: type object 'C' has no attribute 'y' c1 = C(3) c1....
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, ...

Page 2 of 28