CSC/ECE 517 Fall 2011/ch1 1h ps: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 30: Line 30:


== Method call / Instance Variable reference Syntax ==
== Method call / Instance Variable reference Syntax ==
<p>A running debate in O-O languages is whether method calls and instance-variable references should have the same syntax. Different programming languages implement this in different ways. Let's look at them by examples.</p>
=== Same Syntax ===
=== Same Syntax ===
Ruby
<p>Languages like Ruby use the same syntax for method calls and variable references. There is no distinction between these. We can see this in the example below:</p>
<pre>
class Song
  def duration=(newDuration)
    @duration = newDuration
  end
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.duration » 260
aSong.duration = 257  # set attribute with updated value
aSong.duration » 257
</pre>


The advantage of this is that it increases
=== Differing Syntax ===
=== Differing Syntax ===
Java
<p>Java


== Comparison of syntax between various Programming Languages ==
== Comparison of syntax between various Programming Languages ==

Revision as of 23:52, 7 September 2011

Common Attribute/Member Syntax

Different object oriented programming languages include different ways of accessing the member variables and methods. This page discusses the various usages and their advantages and disadvantages.

Introduction

The purpose of Object Oriented Programming is to make programs model things the way people think or deal with the world i.e it's modeled around data rather than logic. Objects form the basis of OOP. Let us now look at objects, their manipulation and the ways to do so.


Objects - Attributes and Methods

Similar to real world objects, software objects also consist of state and related behavior. An object stores it's state in attributes and exposes its behavior through methods. Let's look at the pseudo code for a bicycle object.

CLASS Bicycle
ATTRIBUTES:
	speed: NUMBER
	gear: NUMBER
	pedal_cadence: NUMBER
METHODS:
	ride(speed)
	changeGear(gear)
	changeCadence(pedal_cadence)

In the above pseudo code, the attributes define the object and the methods provide a way of achieving the desired functionality from that object. The methods use the attributes to manipulate the object and achieve the functionality. From this we can observe that this manipulation is the very essence of programming using objects. Therefore the access of attributes and way of defining method calls very important in OOP.

Method call / Instance Variable reference Syntax

A running debate in O-O languages is whether method calls and instance-variable references should have the same syntax. Different programming languages implement this in different ways. Let's look at them by examples.

Same Syntax

Languages like Ruby use the same syntax for method calls and variable references. There is no distinction between these. We can see this in the example below:

class Song
  def duration=(newDuration)
    @duration = newDuration
  end
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.duration	»	260
aSong.duration = 257   # set attribute with updated value
aSong.duration	»	257

The advantage of this is that it increases

Differing Syntax

Java

Comparison of syntax between various Programming Languages