From: Michael Tsang on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

A property is defined as a member that is read and written like a field, but
the read and the write actually calls the getter and setter.

http://en.wikipedia.org/wiki/Property_(programming)

Some languages have native support for property, e.g. C#, Python, PHP.
Although C++ doesn't, I always emulate them by defining helper classes that
overload the copy constructor and the conversion operator. In Java, there is
no operator overloading, how do I emulate them? If I can't do this, I will
have trouble doing OO in Java.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkuGbykACgkQm4klUUKw07CfGgCfbxx86pgfdqbZZX6KemAOOoSW
HCcAniLS80puL8spZxB+mtMaCVzDrIYA
=yRUE
-----END PGP SIGNATURE-----

From: Joshua Cranmer on
On 02/25/2010 07:38 AM, Michael Tsang wrote:
> Some languages have native support for property, e.g. C#, Python, PHP.
> Although C++ doesn't, I always emulate them by defining helper classes that
> overload the copy constructor and the conversion operator. In Java, there is
> no operator overloading, how do I emulate them? If I can't do this, I will
> have trouble doing OO in Java.

The standard way of implementing properties in Java is with get and set:

public class Foobar {
private String name;

public String getName() { return name; }
public void setName(String newName) { name = newName; }
}

Yes, it requires typing about 4 more characters, but otherwise, it would
be pretty much the same.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
From: Jean-Baptiste Nizet on
Michael Tsang a �crit :
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> A property is defined as a member that is read and written like a field, but
> the read and the write actually calls the getter and setter.
>
> http://en.wikipedia.org/wiki/Property_(programming)
>
> Some languages have native support for property, e.g. C#, Python, PHP.
> Although C++ doesn't, I always emulate them by defining helper classes that
> overload the copy constructor and the conversion operator. In Java, there is
> no operator overloading, how do I emulate them? If I can't do this, I will
> have trouble doing OO in Java.

Properties are, as you wrote yourself, just syntax sugar in order to
call getters and setters without having to type the get- or the set-
prefix and the parentheses. In Java, there aren't properties. You call
the getters and setters explicitely. I don't see how your code would be
less OO by doing so. I see, however, how your Java code will be easier
to understand, maintain, debug and refactor (no hidden side-effects).

JB.
From: Tom Anderson on
On Thu, 25 Feb 2010, Michael Tsang wrote:

> A property is defined as a member that is read and written like a field, but
> the read and the write actually calls the getter and setter.
>
> http://en.wikipedia.org/wiki/Property_(programming)
>
> Some languages have native support for property, e.g. C#, Python, PHP.
> Although C++ doesn't, I always emulate them by defining helper classes
> that overload the copy constructor and the conversion operator. In Java,
> there is no operator overloading, how do I emulate them? If I can't do
> this, I will have trouble doing OO in Java.

If you think getters and setters are required for OO, then you will have
trouble doing OO in any language.

tom

--
24-Hour Monkey-Vision!
From: Lew on
Michael Tsang wrote:
>> A property is defined as a member that is read and written like a
>> field, but
>> the read and the write actually calls the getter and setter.

Don't believe everything you read on the Internet. Certainly you cannot take
such a definition as comprehensive.

>> http://en.wikipedia.org/wiki/Property_(programming)

Strangely, this link you provide also gives the answer to your question. This
Wikipedia entry, with its narrow and incomplete definition of "property",
tells you how to implement properties in Java.

>> Some languages have native support for property, e.g. C#, Python, PHP.
>> Although C++ doesn't, I always emulate them by defining helper classes
>> that overload the copy constructor and the conversion operator. In
>> Java, there is no operator overloading, how do I emulate them? If I
>> can't do this, I will have trouble doing OO in Java.

Properties are a concept of which C# "properties" are an implementation.
Don't confuse the Platonic ideal with the shadows on the cave wall.

Tom Anderson wrote:
> If you think getters and setters are required for OO, then you will have
> trouble doing OO in any language.

--
Lew