Sie sind auf Seite 1von 3

Introducing Classes and Objects

Class: instance variables and methods are two of the primary constituents of cl
asses.
The class is the foundation of C# because it defines
the nature of an object. Furthermore, the class forms the basis for object-orien
ted
programming. Within a class are defined both code and data.
A class is a template that defines the form of an
a well-designed class groups logically connected information.
class Building {
public int Floors; // number of floors
public int Area; // total square footage of building
public int Occupants; // number of occupants
}
Remember that a class declaration is only a type description; it does not creat
e an actual object.
To actually create a Class(Building) object, you will use a statement like the
following:
A class definition creates a new data type. In this case, the new data type is
called
Building(Class Name).
create a Building object:
Building house = new Building(); // create an object of type building
After this statement executes, house will be an instance of Building.
Each time you create an instance of a class, you are creating an object
Object:
object.member
house.Floors = 2;
dot operator to access both instance variables and methods.
It specifies both the data and the code that will operate on that data.
Objects are instances of a class.
data is contained in data members defined by the class - instance variables an
d static variables
code is contained in function members - methods, constructors, destructors, i
ndexers, events, operators, and properties
The access specifier is optional, and if absent, then the member is private to
the class.
Building house = new Building();
Building office = new Building();
Each object has its own copies
of the instance variables defined by its class. Thus, the contents of the variab
les in one object
can differ from the contents of the variables in another. There is no connection
between the

two objects except for the fact that they are both objects of the same type.
How Objects Are Created:
Building house = new Building();
house - a variable that can refer to an object.
the declaration creates an actual, physical copy of the object. This is done by
using the new operator.
Finally, it assigns to house a reference to that object.
Thus, after the line executes, house refers to an object of type Building.
new(operator) - dynamically allocates (that is, allocates at runtime) memory f
or an
object and returns a reference to it. all class objects must be dynamically allo
cated.
Building house; // declare reference to object
house = new Building(); // allocate a Building object
Thus, house is a
variable that can refer to an object, but it is not an object, itself. The next
line creates a new
Building object and assigns a reference to it to house. Now, house is linked wit
h an object.
assign one object reference variable to another:
Building house1 = new Building();
Building house2 = house1;
house1 and house2 will both refer to the same object.
Methods:
instance variables and methods are two of the primary constituents of classes.
Methods are subroutines that manipulate(handle/control) the data defined by th
e class and, in many cases, provide access to that data. Typically, other parts
of your program will interact with a class through its methods.
Each method has a name, and it is this name that is used to call
the method.
Main( ): reserved for the method that begins execution of your program.
access ret-type name(parameter-list) {
// body of method
}
default access: private
default return-type: void
Parameters: variables that receive the value of the arguments passed to the meth
od when it is called.
void Foo(int i, float f){}
void Bar(){int anInt = 1;
Foo(anInt, 2.0);}
Here i and f are the parameters, and anInt and 2.0 are the arguments.

public void AreaPerPerson() {}


Its return type is void.
Thus, AreaPerPerson( ) does not return a value to the caller.
When a method is called, program control is transferred to the method.When the m
ethod
terminates, control is transferred back to the caller, and execution resumes wit
h the line
of code following the call.
two forms of return: one for use in void methods (those that do not return a val
ue) and one
for returning values. The first form is examined here. The next section explains
how to
return values.
return; vs break;
Although methods with a return type of void are not rare, most methods will retu
rn a value.
In fact, the ability to return a value is one of a method s most useful features
return value;
Here, value is the value returned.

Das könnte Ihnen auch gefallen