SlideServe
Menu
Préférences
© SlideServe · v2026

Classes

sinclairrobert avatar
8 diapositives · 124 Views · Jan 02, 2020

Classes. we can write our own classes to represent and define our objects e.g. class for Complex objects represents a complex number defines its properties real part imaginary part defines its behaviors constructor(s) for a new number math functions drawing

Classes - slide 1
01 / 8Classes
1 / 17

Télécharger la présentation

Classes
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Classes • we can write our own classes • to represent and define our objects • e.g. class for Complex objects • represents a complex number • defines its properties • real part • imaginary part • defines its behaviors • constructor(s) for a new number • math functions • drawing • we can then create new objects such class and use their functionality in an arbitrary context

  2. Why Classes? • to represent a logical entity • what makes sense as an object • functionality from same context • to structure software • divide and conquer • smaller pieces are easier to test and debug • software architecture • software engineering • to define a library • Java APIs • our own module that we may reuse • because Java and Eclipse support them

  3. Class Types • a class can be defined in 3 ways: • in a separate .java file • top-level class • can be public • it can be used everywhere • within the same file as a top-level class • local • can be used only by classes defined in the same file • nested within another class • local • can be used within the same scope • within the declaration where it's defined • within nested declarations

  4. Anatomy of a Class • every class has a name and a body • e.g. public class Complex {} • body is enclosed in braces { and } • body contains • instance variables, also fields • constructors • methods • possibly other nested classes • e.g. Complex contains • fields realPart and imaginaryPart • constructors • Complex(), Complex(double real, double imaginary) • methods • read(), write(), add (Complex number), etc. • e.g. ComplexMath contains • nested class Complex

  5. Fields • represent properties of an object • every object has the same fields • but the fields typically have different values • when an objectceases to exist its fields don't exist anymore, too • their values may exist it they are referenced elsewhere • can be used directly by all methods in the class • their scope is the entire class • are typically declared as private • visible only within the class • outside, accessible only through accessor methods • tip: initialize a field in its declaration • e.g. String name = "N/A"; • this prevents errors • NullPainterException because of method call on uninitialized field • such initialization occurs before a constructor is executed • be a good citizen • give a field good name • document it in preceding /** */ Javadoc comment

  6. Constructors • called when a new object is created • typically used to initialize the new object • considered as a special type of a method • same syntax in declaration • except name and return type • constructors can be overloaded • class can define more than one constructor • their parameter list must be different • a constructor can call another constructor • using this(); • e.g. public Complex() {this(0,0);} • calls public Complex(double real, double imaginary) {…} • use this for a field that has the same name as a parameter • e.g. this.real = real;

  7. Methods • used to define what an object can do • every object has the same methods • we call a method on an object • e.g. number.write() calls method write() on object number • within a method we can use this as reference to the object on which the method was called, i.e. within write(), this means number • a method can be public or private • public methods can be called on any object • private methods can be called only within the class • they are called on this object • publicmethods are meant to be used by other objects • privatemethods are auxiliary, used only within the class

  8. Accessor and Mutator Methods • it is dangerous to declare a field as public • because anyone has access to it • they can modify it (assign it another value) • we may want to prevent snoopers to read it's value • we may want to know who and when its accessed • e.g. for debugging purposes • instead, we declare the field as private, and supply • a publicaccessor method to access the field's value • a publicmutator method to assign the field's value • accessor method typically • is named getXyz for a field xyz • returns the field's value, e.g. • public double getReal() {return real;} • mutator method typically • is named setXyz for a field xyz • assigns the field's value, e.g. • public void setReal(double real) {this.real = real;}