copyright© 2009 kuwata-lab.com all right reserved.
日付
Basic Mechanism ofObject-Oriented
Programming Language
makoto kuwata <[email protected]>http://www.kuwata-lab.com/
2009-09-11 : Released2009-09-14 : Fixed
copyright© 2009 kuwata-lab.com all right reserved.
Purpose
✤ Describes basic mechanism of Object-Oriented Programming Language (OOPL)
✤ Not only Perl but also Python, Ruby, Java, ...
✤ You must be familiar with terms of Object-Oriented
✤ Class, Instance, Method, Override, Overload, ...
copyright© 2009 kuwata-lab.com all right reserved.
Agenda
✤ Part 1. Basics about OOPL Mechanism
✤ Part 2. More about OOPL Mechanism
✤ Part 3. Case Study
copyright© 2009 kuwata-lab.com all right reserved.
Part 1. Basics about OOPL Mechanism
copyright© 2009 kuwata-lab.com all right reserved.
Overview
Variable
Instance obj
Class obj
Class obj
x: 10y: 20
Method Tbl
Method Tbl
m4
m2m3
m3
m1m2
func ( ) { .....}
Z: 123
Z: 456
Function
func ( ) { .....}
func ( ) { .....}
self
self
self
copyright© 2009 kuwata-lab.com all right reserved.
Instance Object
✤ Instance variables + Is-a pointer
✤ Instance object knows "what I am"
x: 10
y: 20
Instance obj Class objVariable
Pointer toClass object
Hash data (in script lang) or Struct data (in Java or C++)
Class Object
✤ Class variables( + Is-a pointer) + Parent pointer+ Method Table
Class obj
Z: 35
Class obj
Method Table
Method Table
Z: 30
Instance methods belong to Class
copyright© 2009 kuwata-lab.com all right reserved.
Method Lookup Table
✤ Method signatures(≒names) + function pointers
✤ May have pointer to parent method table
Method Table
m1m2m3
func (self) { print "hello";}
func (self, x) { return x + 1;}
Class obj
copyright© 2009 kuwata-lab.com all right reserved.
✤ Instance object is passed as hidden argument
Method Function
class Point { var x=0, y=0; def move(x, y) { this.x = x; this.y = y; }}p = new Point();p.move(10, 20);
def move(self, x, y) { self['x'] = x; self['y'] = y;}
p = {isa: Point, x: 0, y: 0};func = lookup(p, 'move');func(p, 10, 20);
OOPL non-OOPL
almostequiv.
copyright© 2009 kuwata-lab.com all right reserved.
Difference bet. method and function
✤ Method requires instance object
✤ Typically, instance obj is passed as 1st argument
✤ Method call is dynamic, function call is static
✤ Method signature(≒name) is determined statically
✤ Method lookup is performed dynamically(This is why method call is slower than function call)
copyright© 2009 kuwata-lab.com all right reserved.
Overview (again)
Variable
Instance obj
Class obj
Class obj
x: 10y: 20
Method Tbl
Method Tbl
m4
m2m3
m3
m1m2
func ( ) { .....}
Z: 123
Z: 456
Function
func ( ) { .....}
func ( ) { .....}
self
self
self
copyright© 2009 kuwata-lab.com all right reserved.
Conslusion
✤ Instance object knows "what I am" (by is-a pointer)
✤ Instance variables belong to instance object
✤ Instance methods belong to class object
✤ function-call is static, method-call is dynamic
copyright© 2009 kuwata-lab.com all right reserved.
Part 2.More about OOPL Mechanism
copyright© 2009 kuwata-lab.com all right reserved.
Method Signature
✤ Method name + Argument types (+ Return type)(in static language)
✤ Or same as Method name (in dynamic language)
void hello(int v, char ch)void hello(String s)
hello(IC)Vhello(Ljava.lang.String;)V
Example (Java):
Method Declaration Method Signature
copyright© 2009 kuwata-lab.com all right reserved.
Method Overload
✤ One method name can have different method signature
hello(IC)hello(Ljava.lang.String;)
Class obj Method Table
copyright© 2009 kuwata-lab.com all right reserved.
Method Override
✤ Child class can define methods which has same method signature as method in parent class
hello(IC)hello(Ljava.lang.String;)
:hello(Ljava.lang.String;)
Child Class
Parent Class Method Table
Method Table
copyright© 2009 kuwata-lab.com all right reserved.
Super
✤ 'Super' skips method table lookup once
Class obj
Class obj
Method Table
Method Table
m1
m1
Instance obj
x: 10
def m1() { super.m1();}
X
:
:
copyright© 2009 kuwata-lab.com all right reserved.
Polymorphism (1)
✤ Polymorphism depends on ...✤ Receiver object's data type✤ Variable data type
Instance obj Class obj Method Tbl
:
bark(...):
func (self) { .....}
Method Func
::
Determined dynamically
Animal a;a = new Dog(); a.bark();a = new Cat(); a.bark();
not usedused
copyright© 2009 kuwata-lab.com all right reserved.
Polymorphism (2)
✤ Polymorphism depends on ...✤ Data type of formal args✤ Data type of actual args
Instance obj Class obj Method Tbl
:
bark(...):
func (self) { .....}
Method Func
::
Determined statically
void bark(Object o) { ... }a.bark("Wee!");
copyright© 2009 kuwata-lab.com all right reserved.
"Object" class vs. "Class" class (1)
"Object" class
x: 10
y: 20
Instance obj"Foo" class
"Class" classNULL
"Class" extends "Object"class Class extends Object {...}
class Foo extends Object {...}
"Foo" extends "Object"
copyright© 2009 kuwata-lab.com all right reserved.
"Object" class vs. "Class" class (2)
"Object" class
x: 10
y: 20
Instance obj"Foo" class
"Class" classNULL
"Class" is-a "Class"
"Object" is-a "Class"Object = new Class()Foo = new Class()Instobj = new Foo()
Instance is-a "Foo"
"Foo" is-a "Class"
copyright© 2009 kuwata-lab.com all right reserved.
Conclusion (1)
✤ Method Signature = Method Name + Arg Types
✤ Method Overload : Same method name can have different method signature
✤ Method Override : Child class can have same method signature as parent class
✤ Super : Skips current class when method lookup
copyright© 2009 kuwata-lab.com all right reserved.
Conclusion (2)
✤ Polymorphism
✤ Depends on Receiver's data type, Method Name, and Temporary Args' data type
✤ Not depends on Variable data type and Actual Args' data type
✤ Relation between "Object" class and "Class" class is complicated
copyright© 2009 kuwata-lab.com all right reserved.
Part 3.Case Study
copyright© 2009 kuwata-lab.com all right reserved.
Case Study: Ruby
struct RBasic { unsigned long flags; VALUE klass;};
struct RObject { struct RBasic basic; struct st_table *iv_tbl;};
struct RClass { struct RBasic basic; struct st_table *iv_tbl; struct st_table *m_tbl; VALUE super;};
Is-a pointer
Instance variables Method table
Parent pointer
from ruby.h: from ruby.h:
copyright© 2009 kuwata-lab.com all right reserved.
Case Study: Perl
package Point;sub new { my $classname = shift @_; my ($x, $y) = @_; my $this = {x=>$x, y=>$y}; return bless $this, $classname;}sub move_to { my $this = shift @_; my ($x, $y) = @_; $this->{x} = $x; $this->{y} = $y;}
bless() binds hash data with class name(instead of is-a pointer)
Instance object is the first argument of instance method
copyright© 2009 kuwata-lab.com all right reserved.
Case Study: Python
class Point(object): def __init__(self, x=0, y=0): self.x = x self.y = y
def move_to(self, x, y): self.x = x self.y = y
p = Point(0, 0)p.move_to(10, 20)Point.move_to(p, 10, 20)
Instance object is the first argument of instance method
Python uses function as method, andRuby uses method as function.
Possible to call instance method as function
copyright© 2009 kuwata-lab.com all right reserved.
Case Study: Java
Class obj
Class obj
Method Table
Method Table
m1
m2m1
m2
m3
func (this) { ... }
func (this) { ... }
func (this) { ... }
Copy parent entries not to follow parent table (for performance)
copyright© 2009 kuwata-lab.com all right reserved.
Case Study: JavaScript (1)
anim
Animal
__proto__name
prototype
__proto__barkprototype
this.name = name;
prototypealert(this.name);
(1) (2)
(3)
/// (1)function Animal(name) { this.name = name;}/// (2)Animal.prototype.bark = function() { alert(this.name); }/// (3)var anim = new Animal("Doara");
✤ instance.__proto__ points prototype object (= Constructor.prototype)
✤ Trace __proto__ recursively(called as 'prototype chain')
('__proto__' property is available on Firefox)
copyright© 2009 kuwata-lab.com all right reserved.
Case Study: JavaScript (2)
anim
Animal
__proto__bark2
prototype
__proto__
prototype
bark
Animal.call(this);
prototypethis.name = name;
dog
__proto__name
Dog
prototypealert(this.name);
prototypealert("BowWow");
/// (4)function Dog(name) { Animal.call(this, name);}Dog.prototype = anim;delete Dog.prototype.name;
/// (5)Dog.prototype.bark2 = function() { alert("BowWow"); };/// (6)var dog = new Dog("so16");
(4) (5)
(6)
copyright© 2009 kuwata-lab.com all right reserved.
thank you