Cserép Máté
2013. november 21.
class Duck:
def quack(self):
print("Quaaaaaack!")
class Person:
def quack(self):
print("The person imitates a duck.")
def in_the_forest(duck):
duck.quack()
def game():
donald = Duck()
john = Person()
in_the_forest(donald)
in_the_forest(john)
game()
public class Duck {
public void Quack() {
Console.WriteLine("Quaaaaaack!");
}
}
public class Person {
public void Quack() {
Console.WriteLine("The person imitates a duck.");
}
}
class Program {
private static void InTheForest(dynamic duck) {
duck.Quack();
}
private static void Main() {
Duck donald = new Duck();
Person john = new Person();
InTheForest(donald);
InTheForest(john);
}
}
Nincsenek osztályok, csak objektumok, ezek létrejöhetnek
a "semmiből" (ex nihilo) vagy klónozással.
JavaScript:
var jakab = {
surname: "Gipsz",
firstname: "Jakab",
fullname: function() {
return this.firstname + " " + this.surname;
}
};
var dezso = Object.create(jakab);
dezso.firstname = "Dezső";
dezso.middlename = "János";
dezso.fullname = function() {
return this.firstname + " " + this.middlename + " " + this.surname;
}
var obj1 = { name: "object 1", one: 1, two: 2 };
var obj2 = { name: "object 2", three: 3 };
obj2.__proto__ = obj1;
var obj3 = Object.create(obj2);
obj3.name = "object 3";
obj3.four = 4;
obj3 = { name: "object 3", one: 1, two: 2, three: 3, four: 4 };
employee:
(#
computeSalary:<
(# salary: @integer
do noOfHours*80->salary; inner; 0->totalHours
exit salary
#)
#);
worker: employee
(#
computeSalary::< (# do seniority*4+salary->salary; inner #)
#);
salesman: employee
(#
computeSalary::<
(#
do noOfSoldUnits*6+salary ->salary;
0 ->noOfSoldUnits;
inner
#)
#)
Az adattagok és a metódusok is egyaránt ún. slot-ok
Másolás alapú klónozás, dinamikus típus
jakab = (|
name = 'Gipsz Jakab'.
uni = 'ELTE'.
|)
elek = jakab copy.
elek name: 'Remek Elek'.
Leszármazott vezérelt öröklődés
traits student = (|
show = name println uni println.
|)
jakab = (|
parent* = traits student.
name = 'Gipsz Jakab'.
uni = 'ELTE'.
|)
jakab show
my $obj1 = Class::Prototyped->new(
one => 1,
sub1 => sub { print "This is sub1 in obj1" },
sub2 => sub { print "This is sub2 in obj1" }
);
my $obj2 = Class::Prototyped->new(
'parent*' => $obj1,
two => 2,
sub2 => sub { print "This is sub2 in obj2" }
);
$obj2->sub1; # This is sub1 in obj1
$obj2->sub2; # This is sub2 in obj2
my $mirror = $obj2->reflect();
$mirror->addSlots(
sub1 => sub { print "This is sub1 in obj2" });
$mirror->deleteSlot("sub2");
$obj2->sub1; # This is sub1 in obj2
$obj2->sub2; # This is sub2 in obj1
Point := Object clone do(
x := 0
y := 0
)
a := Point clone
b := Point clone
a x = 5
a x println // 5
b x println // 0
a protos first == Point // true
b protos first protos first == Object // true
Objektum létrehozás: literál, klónozás vagy konstruktor
function Person(surname, firstname) {
this.surname = surname;
this.firstname = firstname;
this.fullname = function() {
return this.firstname + " " + this.surname;
}
}
function Student(surname, firstname, neptun) {
this.surname = surname;
this.firstname = firstname;
this.neptun = neptun;
}
Student.prototype = new Person();
var jakab = new Person("Gipsz", "Jakab");
var tamas = new Student("Tanulo", "Tamas", "JUH8RK");
document.writeln(tamas.fullname());
tamas.__proto__ == Student.prototype; // true
jakab.__proto__ == tamas.__proto__.__proto__; // true
Valójában nincs ex nihilo objektum létrehozás, csak klónozás
var obj = {};
obj.__proto__; // object
obj.__proto__.__proto__; // null
var obj = new Object();
obj.__proto__; // object
obj.__proto__.__proto__; // null
A konstruktor művelet is csak klónozás végső soron
function Person(surname, firstname) {
this.surname = surname;
this.firstname = firstname;
this.fullname = function() {
return this.firstname + " " + this.surname;
}
}
Person.prototype = new Object();
"Prototípus konstruktorhívás":
function Person(surname, firstname) {
this.surname = surname;
this.firstname = firstname;
this.fullname = function() {
return this.firstname + " " + this.surname;
}
}
Person.prototype = {
surname: null,
firstname: null,
constructor: Person
};
function Student(surname, firstname, neptun) {
this.__proto__.constructor.call(this, surname, firstname);
this.neptun = neptun;
}
Student.prototype = new Person();
var tamas = new Student("Tanulo", "Tamas", "JUH8RK");
Delegálás alapú klónozás, leszármazott vezérelt öröklődés
Dinamikus típusrendszer, a prototípus lista is dinamikus
var fac = {
calculate: function() { /* ... */ }
};
var fib = {
calculate: function() { /* ... */ }
};
var exec = {};
exec.__proto__ = fac;
document.writeln(exec.calculate());
exec.__proto__ = fib;
document.writeln(exec.calculate());
Dinamikus öröklődés
Account = { }
function Account:new (name, value = 0)
local object = {
owner = name,
balance = value
}
setmetatable(object, { __index = Account })
return object
end
function Account.deposit (self, value)
self.balance = self.balance + value
end
function Account:withdraw (value)
self.balance = self.balance - value
end
acc = Account:new("Gipsz Jakab");
acc.deposit(acc, 100000);
acc:withdraw(20000);