15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

556<br />

Using Objects<br />

The preceding Java code would print the following:<br />

First = “Milk”<br />

First = “Eggs”<br />

Creating C## data structures<br />

C# includes queues, stacks, and hashtables, which you can create by using<br />

the same syntax, such as<br />

Queue queuename = new Queue();<br />

Stack stackname = new Stack();<br />

Hashtable tablename = new Hashtable();<br />

Each type of data structure uses different methods <strong>for</strong> adding and removing<br />

data. A queue uses the Enqueue and Dequeue methods to add and remove<br />

data. A stack uses the Push and Pop methods to add and remove data. A<br />

hashtable uses the Add and Remove methods to add and remove data.<br />

Using Objects<br />

Both Java and C# are true object-oriented <strong>programming</strong> languages (unlike<br />

C++), so you have no choice but to create and use objects in your programs.<br />

To create an object, you must define a class stored in its own file. A typical<br />

class definition looks like this:<br />

class ClassName<br />

{<br />

datatype propertyname;<br />

void methodname()<br />

{<br />

Commands;<br />

}<br />

};<br />

The class lists one or more properties and the type of data that property can<br />

hold, such as an integer or a floating point number. A class also lists one or<br />

more method names, which contains code <strong>for</strong> manipulating an object in<br />

some way.<br />

After you define a class, you can create an object from that class by declaring<br />

a variable as a new class type, such as<br />

className objectname = new className();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!