If a Method Accepts an Object Object, What Other Types of Objects Can It Accept?

Previous Contents Next

Classes, Objects, and Methods

The object-oriented extension of Objective CAML is integrated with the functional and imperative kernels of the language, as well as with its blazon system. Indeed, this last point is unique to the linguistic communication. Thus we accept an object-oriented, statically typed language, with blazon inference. This extension allows definition of classes and instances, course inheritance (including multiple inheritance), parameterized classes, and abstruse classes. Class interfaces are generated from their definition, but may exist made more precise through a signature, similarly to what is done for modules.

Object-Oriented Terminology

Nosotros summarize beneath the main object-oriented programming terms.
class:
a class describes the contents of the objects that belong to it: it describes an aggregate of data fields (called instance variables), and defines the operations (called methods).
object:
an object is an chemical element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs, while the grade specifies how instances are created and how they behave.
method:
a method is an action which an object is able to perform.
sending a message
sending a message to an object means asking the object to execute or invoke ane of its methods.

Course Annunciation

The simplest syntax for defining a form is every bit follows. Nosotros shall develop this definition throughout this affiliate.

Syntax


class proper noun p ane ...p due north =
object
      :
example variables
      :
methods
      :
end

p 1 , ..., p due north are the parameters for the constructor of the grade; they are omitted if the class has no parameters.

An instance variable is declared every bit follows:

Syntax


val proper noun = expr
or
val mutable name = expr

When a data field is declared mutable, its value may be modified. Otherwise, the value is always the one that was computed when expr was evaluated during object creation.

Methods are alleged equally follows:

Syntax


method name p one ...p n = expr

Other clauses than val and method tin can be used in a class announcement: we shall introduce them as needed.

Our beginning grade example.
We commencement with the unavoidable course indicate:
  • the data fields x and y contain the coordinates of the point,
  • two methods provide access to the data fields (get_x and get_y),
  • two deportation methods (moveto: absolute displacement) and (rmoveto: relative displacement),
  • ane method presents the data every bit a string (to_string),
  • one method computes the altitude to the point from the origin (altitude).
        
# class signal (x_init,y_init) =
object
val mutable x = x_init
val mutable y = y_init
method get_x = x
method get_y = y
method moveto (a,b) = x <- a ; y <- b
method rmoveto (dx,dy) = ten <- x + dx ; y <- y + dy
method to_string () =
"( " ^ (string_of_int x) ^ ", " ^ (string_of_int y) ^ ")"
method altitude () = sqrt (float(x*x + y*y) )
cease ;;
Note that some methods practice not demand parameters; this is the example for get_x and get_y. We usually access example variables with parameterless methods.

After nosotros declare the class

signal, the system prints the following text:
        class signal :        
int * int ->
object
val mutable 10 : int
val mutable y : int
method distance : unit of measurement -> float
method get_x : int
method get_y : int
method moveto : int * int -> unit
method rmoveto : int * int -> unit of measurement
method to_string : unit -> string
stop

This text contains ii pieces of information. First, the blazon for objects of the class; this type will exist abbreviated every bit point. The type of an object is the listing of names and types of methods in its class. In our example, point is an abbreviation for:

                                        
< altitude : unit -> unit of measurement; get_x : int; get_y : int;
moveto : int * int -> unit; rmoveto : int * int -> unit;
to_string : unit of measurement -> unit >
Next, we accept a constructor for instances of class point, whose type is int*int -> oint. The constructor allows us to construct point objects (we�ll just say ``points'' to be cursory) from the initial values provided as arguments. In this instance, we construct a bespeak from a pair of integers (meaning the initial position). The constructor point is used with the keyword new.

It is possible to define grade types:

        
# type simple_point = < get_x : int; get_y : int; to_string : unit of measurement -> unit > ;;
type simple_point = < get_x : int; get_y : int; to_string : unit -> unit of measurement >

Note


Type signal does not repeat all the informations shown later a form annunciation. Instance variables are non shown in the type. Just methods have admission to these instance variables.

Warning


A class declaration is a type annunciation. Every bit a upshot, it cannot contain a free type variable.

We will come back to this point later when we deal with type constraints (folio ??) and parameterized classes (folio ??).

A Graphical Note for Classes

We adapt the UML notation for the syntax of Objective CAML types. Classes are denoted by a rectangle with three parts:
  • the top office shows the name of the grade,
  • the heart function lists the attributes (data fields) of a class instance,
  • the lesser part shows the methods of an case of the class.
Effigy 15.1 gives an example of the graphical representation for the class caml.

Figure 15.1: Graphical representation of a class.


Type information for the fields and methods of a class may be added.

Instance Creation

An object is a value of a course, chosen an example of the grade. Instances are created with the generic structure archaic new, which takes the form and initialization values as arguments.

Syntax


new name expr 1 ...expr n
The following case creates several instances of class point, from various initial values.
        
# let p1 = new betoken ( 0 , 0 );;
val p1 : point = <obj>
# allow p2 = new point ( iii , iv );;
val p2 : betoken = <obj>
# let coord = ( 3 , 0 );;
val coord : int * int = three, 0
# permit p3 = new point coord;;
val p3 : point = <obj>

In Objective CAML, the constructor of a class is unique, but you may define your own specific part

make_point for point cosmos:
        
# let make_point x = new bespeak (10,x) ;;
val make_point : int -> point = <fun>
# make_point 1 ;;
- : point = <obj>

Sending a Message

The notation # is used to ship a message to an object. ii

Syntax


obj 1 # name p i ...p n
The message with method name ``name'' is sent to the object obj. The arguments p 1 , ..., p n are every bit expected by the method name. The method must be defined by the class of the object, i.east. visible in the blazon. The types of arguments must suit to the types of the formal parameters. The following example shows several queries performed on objects from the class betoken.
        
# p1#get_x;;
- : int = 0
# p2#get_y;;
- : int = 4
# p1#to_string();;
- : cord = "( 0, 0)"
# p2#to_string();;
- : string = "( 3, 4)"
# if (p1#distance()) = (p2#distance())
and then print_string ( "That's just take a chance\n" )
else print_string ( "Nosotros could bet on it\north" );;
We could bet on it
- : unit of measurement = ()

From the type point of view, objects of blazon signal tin can be used by polymorphic functions of Objective CAML, simply equally whatever other value in the language:

        
# p1 = p1 ;;
- : bool = truthful
# p1 = p2;;
- : bool = faux
# let fifty = p1::[];;
val l : point list = [<obj>]
# List.hard disk drive l;;
- : point = <obj>

Warning


Object equality is defined as physical equality.

Nosotros shall clarify this point when we study the subtyping relation (page ??).


Previous Contents Next

martinezfonesto.blogspot.com

Source: https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora140.html

0 Response to "If a Method Accepts an Object Object, What Other Types of Objects Can It Accept?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel