|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
|
|
Duplicate :
|
Name: krT82822 Date: 04/11/99
Java does not explicitly support properties currently.
To simulate properties, the Java API uses the
setProperty()/getProperty() methods.
It would be nice if properties were part of the language.
Current state of Java without properties:
class XYZ
{
// property color
private Color color;
public Color getColor()
{ return color; }
public void setColor(Color newColor)
{ color = newColor; }
}
...
// client code
void mymethod(XYZ obj)
{
// use component
Color c;
// we have to use get/set methods
c = obj.getColor();
obj.setColor(Color.red);
}
As you can see using get/set methods is okay. But in my
opinion, it is not very readable.
Let's say Java supported properties. We might then have
a new keyword 'property' to indicate a property.
Here is a proposed solution:
class XYZ
{
// property color
// declare a property color of type Color
public property Color color;
// define a method for setting the color
public get property Color color()
{ return color; }
public set property void color(Color newColor)
{ color = newColor; }
}
The above definition of properties might seem more work at
first. But it is clear in its intent - that color is a
properties. The use of get/set methods is a hack.
Furthermore, using the properties is very readable.
void mymethod(XYZ obj)
{
// use component
Color c;
// Before, after scenarios for properties
// get the color
c = obj.getColor(); // before
c = obj.color; // after
// assign a new color
obj.setColor(Color.red); // before
obj.color = Color.red; // after
}
This feature might not seem much. But I'm sure that if such
a feature existed in Java, properties would be used more than
they are now.
I am of the opinion that properties are part of the
general Java language and not just to be used by JavaBeans.
This syntactic-sugar might make Java more readable.
(Review ID: 56803)
======================================================================
|