In our Square class so far, we were using a
constructor to create a value for the field. This meant that we had to always make
sure that we knew the value of the field when we declared an
instance of the class. Sometimes, this is not possible. For example, you
cannot just call a constructor in the middle of the program, that is, after the
object has been declared, to assign a new value to the field. To
solve this kind of problem, you can provide a means of accessing
the field any time to change its value.
Besides, or instead of, retrieving the value
of a
field of a class, you may want external classes to be able to
change the value of that member. Continuing with our policy of
hiding a
field as private, you can create another type of property. a
property is referred to as write if it can change (or write) the value
of
its corresponding field.
To get a write property, create a method using the
following characteristics:
Here is an example:
public class Square {
private double side;
// This is a write property
public void setSide(double value) {
side = value;
}
}
As you see, clients of a class can change the
corresponding field of a member variable through the property writer. Based on
this relationship, it is not unusual for a client of a class to make an
attempt to "mess" with a field. For example, an
external object can assign an invalid value to a member variable. Consider
the following program:
class Square { private double side; // This is a read property public double getSide() { return side; } // This is a write property public void setSide(double value) { side = value; } public double calculatePerimeter() { return side * 4; } public double calculateArea() { return side * side; } } public class Exercise { public static void main(String[] args) { Square sqr = new Square(); sqr.setSide(-24.86); System.out.println("Square Characteristics"); System.out.printf("Side: %.2f\n", sqr.getSide()); System.out.printf("Perimeter: %.2f\n", sqr.calculatePerimeter()); System.out.printf("Area: %.2f", sqr.calculateArea()); System.out.println("\n"); sqr.setSide(24.86); System.out.println("Square Characteristics"); System.out.printf("Side: %.2f\n", sqr.getSide()); System.out.printf("Perimeter: %.2f\n", sqr.calculatePerimeter()); System.out.printf("Area: %.2f", sqr.calculateArea()); } }
This would produce:
Square Characteristics Side: -24.86 Perimeter: -99.44 Area: 618.02 Square Characteristics Side: 24.86 Perimeter: 99.44 Area: 618.02
Because of this, and since it is through the writer
that the external objects would change the value of the member variable,
you can use the write property, rather than the reader, to validate or
reject a new value assigned to the field. Remember that the
client objects of the class can only read the value of the field through the read
property. Therefore, there may be only little concern on
that side.
A property is referred to as read/write it has both
a reader and a writer.
You can create a property as a Boolean type. To do this,
first specify its data type as bool. When treating the property, make
sure its write property returns a Boolean type. A Boolean property is not
like the other regular properties. It must specify its value as true or false.
As done for Boolean methods, a Boolean property must produce only a true or
false value.
An enumeration is a technique of creating a data type that
mimics an integer. After creating it, you can use it to create a property based
on an enumeration. You use the same
formula as one of the primitive data types we have used previously. Keep it mind
that the property is of the type of an enumeration. This means that you cannot
request its value like that of a primitive type and, when manipulating it, you
must process it appropriately.
Remember that, after creating a class, it becomes a data
type in its own right. We have seen that you could declare a variable from it,
you could pass it as argument, and you could return it from a method. As a
normal data type, a class can be validated. This means that its value can be
evaluated, rejected, or retrieved. Based on these characteristics of a class,
you can create a property from it.
To create a property that is based on a class, primarily
follow the same formulas we have applied to the other properties. The most
important aspect to remember is that the class is composite. That is, it is
(likely) made of fields of various types.
|
The Properties of a Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment