• Tidak ada hasil yang ditemukan

THEADDRESS

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 50-54)

4 VARIABLES AND DATA TYPES

Exercise 4: Arithmetic

4.5 THEADDRESS

You should note that the class has no main() method, because the class is not a program, but merely defines a concept that can be used by any application.

With the class BankNote available, a program can create BankNote objects and do something with them. Below is a main() method from a program that creates two BankNote objects.

The first has the value of 100, while the next has the value of 200:

namespace BankProgram {

class Program {

static void Main(string[] args) {

BankNote note1 = new BankNote(100);

BankNote note2 = new BankNote(200);

Console.WriteLine(note1.Value);

Console.WriteLine(note2.Value);

} } }

You must specifically note how to create an object. An object has a name, for example note1, which is a variable. The object also has a type, which is here BankNote, and when the type is a class rather than a primitive type, the object must be created with the new operator. Here you specify the value of the banknote, and exactly it means that the class’s constructor is executed and transmits the value to the object’s instance variable value. If the Main() method is performed it prints the values of the two BankNote objects. For that to be possible, you have to refer to the BankNote object’s value. It is not possible because of data encapsulation, but a BankNote object has a behavior in terms of the property Value, so you can refer to the value.

When I click on Add Visual Studio adds a class with the name Address to the project:

using System;

using System.Collections.Generic;

using System.Text;

namespace TheAddress {

class Address {

} }

Visual Studio creates a skeleton for a class and three using statements, where only the first is necessary. The other two can be deleted if you do not want them to be there, but it does not matter to the finished program.

Then the code must be written. The class must have four variables for the properties that an address should have. They are instance variables and for data encapsulation they are all defined private. This means that the class’s variables can only be referenced by the class’s own methods, and thus it is the programmer that has to open up for the variables to be used by other objects. In order for the class to be used for something, it is usually necessary to read the values of the variables, and it happens by properties as shown in the class BankNote.

using System;

namespace TheAddress {

class Address {

private String name;

private String addr;

private String code;

private String city;

public string Name {

get { return name; } set { name = value; } }

public string Addr {

get { return addr; } set { addr = value; } }

public string Code {

get { return code; } set { code = value; } }

public string City {

get { return city; } set { city = value; } }

public void Print() {

Console.WriteLine();

Console.WriteLine(“Address:”);

Console.WriteLine();

Console.WriteLine(“ Name: “ + name);

Console.WriteLine(“ Address: “ + addr);

Console.WriteLine(“ “ + code + “ “ + city);

} } }

Here you should notice three things. First, the class, unlike the class BankNote, has no constructor. It is not absolutely necessary for a class to have a constructor, but will typically be the case. In this case, the variables are not initialized, and they are assigned default values, as here is null, which simply means that the variables have no value. Secondly, note that the properties has set methods and notes the syntax and the value to be assigned to a variable always is referenced as value. Finally there is the method Print() that prints the contents of the four instance variables on the screen. Note that the code is the same as that used in the application InputAddress.

With the class Address available, the program can then be written as follows:

using System;

namespace TheAddress {

class Program {

static void Main(String[] args) {

Address ad = EnterAddress();

Console.WriteLine();

ad.Print();

}

private static Address EnterAddress() {

Address address = new Address();

Console.Write(“Enter name: “);

address.Name = Console.ReadLine();

Console.Write(“Enter address: “);

address.Addr = Console.ReadLine();

Console.Write(“Enter zipcode: “);

address.Code = Console.ReadLine();

Console.Write(“Enter town: “);

address.City = Console.ReadLine();

return address;

} } }

and the program then works in exactly the same way as the application InputAddress. It is not clear what the advantage of this solution is, as the code fills far more than before, and seen solely in relation to this program, there is no benefit either, but as programs grow bigger and more complex, the division into classes are completely fundamental to the program architecture and for the maintenance of the program. The class Address represents a subject in the program’s problem area. Everything that concerns an address, especially the data presentation in the form of variables, has been moved to its own class. It is then the class’s methods that determines what is possible with an object of the class (an object is a specific address). You can then think of an Address as a type that defines a concept (a kind of objects) that the program needs, and then the program can create objects of that type as needed. In the program above, one object, called ad, is created. In that way to move everything about an address into its own class, you get a split of the code into parts where each part (class) has its own well-defined task and responsibility. It is a step towards modular programming.

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 50-54)