|
Microsoft AJAX Library - C# and JavaScript Classes |
|
Page 1 of 2
C# and JavaScript Classes
For the purpose of demonstrating a few more OOP-related concepts, we'll use another class. Our new class is named Table, and it has two public fields (rows, columns), and one method, getCellCount(). The getCellCount() method should return the number of rows multiplied by the number of columns. The class constructor should receive two parameters, used to initialize the rows and columns fields. This class could be represented by the class diagram in Figure 3-3.

Figure 3-3. Class diagram representing the Table class
The C# version of this class would look like this:
public class Table { // public members public int rows = 0; public int columns = 0; // constructor public Table(int rows, int columns) { this.rows = rows; this.columns = columns; } // method returns the number of cells public int getCellCount() { return rows * columns; } }
public%20class%20Table%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20public%20members%20%0A%0D%0Apublic%20int%20rows%20%3D%200%3B%20%0A%0D%0Apublic%20int%20columns%20%3D%200%3B%20%0A%0D%0A%2F%2F%20constructor%20%0A%0D%0Apublic%20Table%28int%20rows%2C%20int%20columns%29%20%0A%0D%0A%7B%20%0A%0D%0Athis.rows%20%3D%20rows%3B%20%0A%0D%0Athis.columns%20%3D%20columns%3B%20%0A%0D%0A%7D%20%0A%0D%0A%2F%2F%20method%20returns%20the%20number%20of%20cells%20%0A%0D%0Apublic%20int%20getCellCount%28%29%20%0A%0D%0A%7B%20%0A%0D%0Areturn%20rows%20%2A%20columns%3B%20%0A%0D%0A%7D%20%0A%0D%0A%7D
You'd instantiate and use the class like this:
Table t = new Table(3,5); int cellCount = t.getCellCount();
Table%20t%20%3D%20new%20Table%283%2C5%29%3B%20%0A%0D%0Aint%20cellCount%20%3D%20t.getCellCount%28%29%3B
|