31
copyright© 2009 kuwata-lab.com all right reserved. 日付 Basic Mechanism of Object-Oriented Programming Language makoto kuwata <[email protected]> http://www.kuwata-lab.com/ 2009-09-11 : Released 2009-09-14 : Fixed

Basic Mechanism of OOPL

  • Upload
    kwatch

  • View
    6.939

  • Download
    0

Embed Size (px)

DESCRIPTION

(English) Basic mechanism of Object-Oriented Program Language.

Citation preview

Page 1: Basic Mechanism of OOPL

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

Page 2: Basic Mechanism of OOPL

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, ...

Page 3: Basic Mechanism of OOPL

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

Page 4: Basic Mechanism of OOPL

copyright© 2009 kuwata-lab.com all right reserved.

Part 1. Basics about OOPL Mechanism

Page 5: Basic Mechanism of OOPL

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

Page 6: Basic Mechanism of OOPL

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++)

Page 7: Basic Mechanism of OOPL

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

Page 8: Basic Mechanism of OOPL

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

Page 9: Basic Mechanism of OOPL

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.

Page 10: Basic Mechanism of OOPL

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)

Page 11: Basic Mechanism of OOPL

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

Page 12: Basic Mechanism of OOPL

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

Page 13: Basic Mechanism of OOPL

copyright© 2009 kuwata-lab.com all right reserved.

Part 2.More about OOPL Mechanism

Page 14: Basic Mechanism of OOPL

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

Page 15: Basic Mechanism of OOPL

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

Page 16: Basic Mechanism of OOPL

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

Page 17: Basic Mechanism of OOPL

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

:

:

Page 18: Basic Mechanism of OOPL

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

Page 19: Basic Mechanism of OOPL

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!");

Page 20: Basic Mechanism of OOPL

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"

Page 21: Basic Mechanism of OOPL

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"

Page 22: Basic Mechanism of OOPL

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

Page 23: Basic Mechanism of OOPL

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

Page 24: Basic Mechanism of OOPL

copyright© 2009 kuwata-lab.com all right reserved.

Part 3.Case Study

Page 25: Basic Mechanism of OOPL

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:

Page 26: Basic Mechanism of OOPL

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

Page 27: Basic Mechanism of OOPL

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

Page 28: Basic Mechanism of OOPL

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)

Page 29: Basic Mechanism of OOPL

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)

Page 30: Basic Mechanism of OOPL

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)

Page 31: Basic Mechanism of OOPL

copyright© 2009 kuwata-lab.com all right reserved.

thank you