Before we begin, I should comment that the content of my lectures is generally applicable across different computing platforms, operating systems, party affiliations, and most non-extremist ideologies. Nevertheless, to provide conceptual illustration, I will present code samples written in C$ (which, appropriately enough, is a mythical fusion of C# with Perl, our favorite dollar-sign-loving language). Furthermore, I will assume that you are at least vaguely liberal, otherwise you would have zipped away to www.nra.gov after the first paragraph. (Yes, .gov.)
Part 1: Creating Objects, Properties, and Methods
Unsurprisingly, object-oriented programming revolves around the notion of the Object. An Object, loosely defined, is a Thing: it can represent any Stuff that you desire using the primative buliding blocks of the programming language. Consider, for example, a War object, that you could describe in terms of the date it started, how many people were killed on each side, what the justification was, etc. In C$, using a class to define an object, we could write:
class War
{ DateTime $StartDate;
DateTime $EndDate;
int $NumberOfCasualties;
string $Justification;
}
Once defined, objects can be declared and instantiated, allowing you to describe them via their properties:
War $MyWar = New War();
$MyWar.$StartDate = new DateTime(2003, 3, 19);
$MyWar.$EndDate = new DateTime(2018, 6, 27);
$MyWar.$NumberOfCasualties = 26641;
$MyWar.$Justification = null;
Note that object values can be left undefined (null) in the event that they are unapplicable, inconsequential, unknown, or simply nonexistent.
In addition to having properties, objects can also have functions (or "methods") that perform a particular calculation or operation. For example, you could write a method to alter the value of a property in a particular, controlled way.
class War
{ string $Justification;
...
void ChangeJustification(string $NewJustification)
{
if ($NewJustification == "Oil") $Justification = "Spread liberty and freedom"else $Justification = $NewJustification;}}
In this example, once you've created a War object, you could invoke this method as follows:
$MyWar.ChangeJustification("Capture an evil dictator");
Thus we have seen that Objects, Properties, and Methods make up the fundamental underpinnings of object-oriented programming, and that I can quickly generate a conclusive-sounding sentence by simply prepending an earlier statement that I made with the words "thus we have seen". In our next lecture, we shall examine the powerful notion of object inheritence and parent/child object relations.
*** BELCH ***
No comments:
Post a Comment