On Mon, 19 Sep 2005, Ed Hotchkiss wrote:

> Thanks for the link! It is EXACTLY what I have been looking for. When I
> used to use Flash a bit, and did some actionscript they had a similiar
> setup for OOP (Do all languages use OOP so similiarly?) which I never
> learned.

Hi Ed,


I believe that most things are fairly close among the languages that claim
to support OOP.  Just for comparison, let's see what this looks like in
some other languages.  For example, let's say we have a Person class:

### Python ###
class Person(object):
    def __init__(self, name):
        self.name = name
    def sayHello(self):
        print "hello, my name is", name

p = Person("ed")
p.sayHello()
##############

This creates a Person class, who extends the behavior of the base 'object'
class.  This class has a bit of state to remember what the respective
'name' is.  We also a method called 'sayHello' to make sure the person can
respond to a "sayHello" message.


Here's what things look like in Java:

/*** Java ***/
public class Person extends java.lang.Object {
    String name;
    public Person(String name) {
        this.name = name;
    }
    public void sayHello() {
        System.out.println("hello, my name is " + this.name);
    }

    static public void main(String[] args) {
        Person p = new Person("ed");
        p.sayHello();
    }
}
/************/


Pretty much the same thing: we define what things an instance of the class
needs to store in its personal state, and we also define a method that
instances can respond to.


Finally, just to make this idea concrete, let's switch to PLT Scheme:

;;; PLT Scheme ;;;
(require (lib "class.ss"))

(define person%
  (class object%
    (init-field name)
    (public say-hello!)
    (define (say-hello!)
      (printf "hello, my name is ~a~%" name))
    (super-new)))

(define p (new person% (name "ed")))
(send p say-hello!)
;;;;;;;;;;;;;;;;;;

Same thing, just more parentheses.  *grin*



Hope this helps!

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to