2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,4 +1,5 @@
|
|||
In [[object-oriented programming]] '''class''' is a set (a [[wp:Transitive_closure|transitive closure]]) of types bound by the relation of [[inheritance]]. It is said that all types derived from some base type T and the type T itself form a class T.
|
||||
|
||||
The first type T from the class T sometimes is called the '''root type''' of the class.
|
||||
|
||||
A class of types itself, as a type, has the values and operations of its own.
|
||||
|
|
@ -6,6 +7,7 @@ The operations of are usually called '''methods''' of the root type.
|
|||
Both operations and values are called [[polymorphism | polymorphic]].
|
||||
|
||||
A polymorphic operation (method) selects an implementation depending on the actual specific type of the polymorphic argument.
|
||||
|
||||
The action of choice the type-specific implementation of a polymorphic operation is called '''dispatch'''. Correspondingly, polymorphic operations are often called '''dispatching''' or '''virtual'''.
|
||||
Operations with multiple arguments and/or the results of the class are called '''multi-methods'''.
|
||||
A further generalization of is the operation with arguments and/or results from different classes.
|
||||
|
|
@ -13,6 +15,7 @@ A further generalization of is the operation with arguments and/or results from
|
|||
* single-dispatch languages are those that allow only one argument or result to control the dispatch. Usually it is the first parameter, often hidden, so that a prefix notation ''x''.''f''() is used instead of mathematical ''f''(''x'').
|
||||
* multiple-dispatch languages allow many arguments and/or results to control the dispatch.
|
||||
|
||||
<br>
|
||||
A polymorphic value has a type tag indicating its specific type from the class and the corresponding specific value of that type.
|
||||
This type is sometimes called '''the most specific type''' of a [polymorphic] value.
|
||||
The type tag of the value is used in order to resolve the dispatch.
|
||||
|
|
@ -24,5 +27,7 @@ In some languages they are distinct (like in [[Ada]]).
|
|||
When class T and T are equivalent, there is no way to distinguish
|
||||
polymorphic and specific values.
|
||||
|
||||
The purpose of this task is to create a basic class with a method,
|
||||
a constructor, an instance variable and how to instantiate it.
|
||||
|
||||
;Task:
|
||||
Create a basic class with a method, a constructor, an instance variable and how to instantiate it.
|
||||
<br><br>
|
||||
|
|
|
|||
30
Task/Classes/Elena/classes.elena
Normal file
30
Task/Classes/Elena/classes.elena
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#import system.
|
||||
#import extensions.
|
||||
|
||||
#class MyClass
|
||||
{
|
||||
#field _variable.
|
||||
|
||||
#method Variable = _variable.
|
||||
|
||||
#method someMethod
|
||||
[
|
||||
_variable := 1.
|
||||
]
|
||||
|
||||
#constructor new
|
||||
[
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
// instantiate the class
|
||||
#var instance := MyClass new.
|
||||
|
||||
// invoke the method
|
||||
instance someMethod.
|
||||
|
||||
// get the variable
|
||||
console writeLine:"Variable=":(instance Variable).
|
||||
].
|
||||
28
Task/Classes/PowerShell/classes-1.psh
Normal file
28
Task/Classes/PowerShell/classes-1.psh
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
Add-Type -Language CSharp -TypeDefinition @'
|
||||
public class MyClass
|
||||
{
|
||||
public MyClass()
|
||||
{
|
||||
}
|
||||
public void SomeMethod()
|
||||
{
|
||||
}
|
||||
private int _variable;
|
||||
public int Variable
|
||||
{
|
||||
get { return _variable; }
|
||||
set { _variable = value; }
|
||||
}
|
||||
public static void Main()
|
||||
{
|
||||
// instantiate it
|
||||
MyClass instance = new MyClass();
|
||||
// invoke the method
|
||||
instance.SomeMethod();
|
||||
// set the variable
|
||||
instance.Variable = 99;
|
||||
// get the variable
|
||||
System.Console.WriteLine( "Variable=" + instance.Variable.ToString() );
|
||||
}
|
||||
}
|
||||
'@
|
||||
17
Task/Classes/PowerShell/classes-2.psh
Normal file
17
Task/Classes/PowerShell/classes-2.psh
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class MyClass
|
||||
{
|
||||
[type]$MyProperty1
|
||||
[type]$MyProperty2 = "Default value"
|
||||
|
||||
# Constructor
|
||||
MyClass( [type]$MyParameter1, [type]$MyParameter2 )
|
||||
{
|
||||
# Code
|
||||
}
|
||||
|
||||
# Method ( [returntype] defaults to [void] )
|
||||
[returntype] MyMethod( [type]$MyParameter3, [type]$MyParameter4 )
|
||||
{
|
||||
# Code
|
||||
}
|
||||
}
|
||||
33
Task/Classes/PowerShell/classes-3.psh
Normal file
33
Task/Classes/PowerShell/classes-3.psh
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
class Banana
|
||||
{
|
||||
# Properties
|
||||
[string]$Color
|
||||
[boolean]$Peeled
|
||||
|
||||
# Default constructor
|
||||
Banana()
|
||||
{
|
||||
$This.Color = "Green"
|
||||
}
|
||||
|
||||
# Constructor
|
||||
Banana( [boolean]$Peeled )
|
||||
{
|
||||
$This.Color = "Green"
|
||||
$This.Peeled = $Peeled
|
||||
}
|
||||
|
||||
# Method
|
||||
Ripen()
|
||||
{
|
||||
If ( $This.Color -eq "Green" ) { $This.Color = "Yellow" }
|
||||
Else { $This.Color = "Brown" }
|
||||
}
|
||||
|
||||
# Method
|
||||
[boolean] IsReadyToEat()
|
||||
{
|
||||
If ( $This.Color -eq "Yellow" -and $This.Peeled ) { return $True }
|
||||
Else { return $False }
|
||||
}
|
||||
}
|
||||
5
Task/Classes/PowerShell/classes-4.psh
Normal file
5
Task/Classes/PowerShell/classes-4.psh
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
$MyBanana = [banana]::New()
|
||||
$YourBanana = [banana]::New( $True )
|
||||
$YourBanana.Ripen()
|
||||
If ( -not $MyBanana.IsReadyToEat() -and $YourBanana.IsReadyToEat() )
|
||||
{ $MySecondBanana = $YourBanana }
|
||||
19
Task/Classes/Simula/classes.simula
Normal file
19
Task/Classes/Simula/classes.simula
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
BEGIN
|
||||
CLASS MyClass(instanceVariable);
|
||||
INTEGER instanceVariable;
|
||||
BEGIN
|
||||
PROCEDURE doMyMethod(n);
|
||||
INTEGER n;
|
||||
BEGIN
|
||||
Outint(instanceVariable, 5);
|
||||
Outtext(" + ");
|
||||
Outint(n, 5);
|
||||
Outtext(" = ");
|
||||
Outint(instanceVariable + n, 5);
|
||||
Outimage
|
||||
END;
|
||||
END;
|
||||
REF(MyClass) myObject;
|
||||
myObject :- NEW MyClass(5);
|
||||
myObject.doMyMethod(2)
|
||||
END
|
||||
29
Task/Classes/SuperCollider/classes-1.supercollider
Normal file
29
Task/Classes/SuperCollider/classes-1.supercollider
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
SpecialObject {
|
||||
|
||||
classvar a = 42, <b = 0, <>c; // Class variables. 42 and 0 are default values.
|
||||
var <>x, <>y; // Instance variables.
|
||||
// Note: variables are private by default. In the above, "<" creates a getter, ">" creates a setter
|
||||
|
||||
*new { |value|
|
||||
^super.new.init(value) // constructor is a class method. typically calls some instance method to set up, here "init"
|
||||
}
|
||||
|
||||
init { |value|
|
||||
x = value;
|
||||
y = sqrt(squared(a) + squared(b))
|
||||
}
|
||||
|
||||
// a class method
|
||||
*randomizeAll {
|
||||
a = 42.rand;
|
||||
b = 42.rand;
|
||||
c = 42.rannd;
|
||||
}
|
||||
|
||||
// an instance method
|
||||
coordinates {
|
||||
^Point(x, y) // The "^" means to return the result. If not specified, then the object itself will be returned ("^this")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
3
Task/Classes/SuperCollider/classes-2.supercollider
Normal file
3
Task/Classes/SuperCollider/classes-2.supercollider
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
SpecialObject.randomizeAll;
|
||||
a = SpecialObject(8);
|
||||
a.coordinates;
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
MyClass {
|
||||
classvar someVar, <another, <>thirdVar; // Class variables.
|
||||
var <>something, <>somethingElse; // Instance variables.
|
||||
// Note: variables are private by default. In the above, "<" enables getting, ">" enables setting
|
||||
|
||||
*new {
|
||||
^super.new.init // constructor is a class method. typically calls some instance method to set up, here "init"
|
||||
}
|
||||
|
||||
init {
|
||||
something = thirdVar.squared;
|
||||
somethingElse = this.class.name;
|
||||
}
|
||||
|
||||
*aClassMethod {
|
||||
^ someVar + thirdVar // The "^" means to return the result. If not specified, then the object itself will be returned ("^this")
|
||||
}
|
||||
|
||||
anInstanceMethod {
|
||||
something = something + 1;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue