1 1 5 Clases

19

Click here to load reader

Transcript of 1 1 5 Clases

Page 1: 1 1 5 Clases

1_1_5 Clases1_1_5 Clases

Apoyo SSD3Apoyo SSD3

Page 2: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 22

class Point class Point

Page 3: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 33

ClassesClasses

Just about any real-life object will require a Just about any real-life object will require a collection of primitives, rather than just collection of primitives, rather than just one, to model it. one, to model it.

To make such objects manageable and To make such objects manageable and coherent in a program, Java provides coherent in a program, Java provides the the classclass construct. construct.

Page 4: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 44

ClassesClasses

Not only can a class serve to collect Not only can a class serve to collect together related together related datadata into a single,  into a single, coherent unit, it can also include coherent unit, it can also include methodsmethods, , or descriptions of the behavior of that or descriptions of the behavior of that collection. collection.

Page 5: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 55

ClassesClasses

Collecting together the attributes and Collecting together the attributes and behaviors of an object into a coherent behaviors of an object into a coherent aggregate is called aggregate is called encapsulationencapsulation. .

This forms the basis of what is known as This forms the basis of what is known as object-based programmingobject-based programming

Page 6: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 66

DataData

Data are the "stuff" of classes-the Data are the "stuff" of classes-the tangibles.tangibles.

Data are collected together in a class so Data are collected together in a class so that they may exist and appear as a that they may exist and appear as a whole. whole.

Data in classes are often referred to as Data in classes are often referred to as fields of the class.fields of the class.

Page 7: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 77

Page 8: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 88

DataData

This is a class definition introducing a new This is a class definition introducing a new type, Point. type, Point.

It proceeds to define what objects of It proceeds to define what objects of type Point will contain, namely two type Point will contain, namely two integers, x and y. integers, x and y.

There is no relationship between, for There is no relationship between, for example, x in one object and x in another. example, x in one object and x in another.

This guarantees the integrity of individual This guarantees the integrity of individual objects objects

Page 9: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 99

Page 10: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1010

ConstructorsConstructors

Instance variables come into existence as Instance variables come into existence as objects are created. objects are created.

As stated, objects get their own personal As stated, objects get their own personal copies of instance variables. copies of instance variables.

In order to initialize instance variables, and In order to initialize instance variables, and hence, objects, constructors are used. hence, objects, constructors are used.

A constructor is a named group of A constructor is a named group of statements executed each time an object statements executed each time an object is created.is created.

Page 11: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1111

Page 12: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1212

ConstructorsConstructors

The keyword this is a reference to the The keyword this is a reference to the current object. current object.

When accessing instance variables within When accessing instance variables within an instance method or a constructor, the an instance method or a constructor, the keyword keyword thisthis can be used to make it clear  can be used to make it clear that you are referring to an instance that you are referring to an instance variable.variable.

A class may contain several constructors, A class may contain several constructors, so long as the argument lists are unique.so long as the argument lists are unique.

Page 13: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1313

Page 14: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1414

MethodsMethods

While data describe what an object "is", While data describe what an object "is", methods describe what an object "does". methods describe what an object "does".

Like data, two kinds of methods exist. Like data, two kinds of methods exist. Instance, or non-static, methods exist for Instance, or non-static, methods exist for the purpose of individual objects.the purpose of individual objects.

An instance method must be called by an An instance method must be called by an object, and it serves the individual object object, and it serves the individual object that made the call.that made the call.

Page 15: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1515

Page 16: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1616

Page 17: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1717

Page 18: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1818

MethodsMethods

Notice that the static method Notice that the static method getNumberOfInstancesgetNumberOfInstances does not access does not access instance data. instance data.

In fact, static methods do not even have access In fact, static methods do not even have access to such data, since they are not a part of any to such data, since they are not a part of any object. getX, getY, setX, and setY on the other object. getX, getY, setX, and setY on the other hand, are instance methods, and thus have hand, are instance methods, and thus have access to the data residing in the object that access to the data residing in the object that invokes them.invokes them.

Page 19: 1 1 5 Clases

Mtl Lourdes CahuichMtl Lourdes Cahuich 1919

MethodsMethods

Method definitions contain the following parts.Method definitions contain the following parts.return-type name(argument-list) bodyreturn-type name(argument-list) body

The return-type indicates the type of value returned The return-type indicates the type of value returned by the method. by the method.

A special primitive type, not previously mentioned, is A special primitive type, not previously mentioned, is the void type. void is used to indicate that a method the void type. void is used to indicate that a method returns no value. It is not possible to create variables returns no value. It is not possible to create variables of type void. Its role is limited.of type void. Its role is limited.