How to set a default value of an attribute in a subclass? [message #787494] |
Tue, 31 January 2012 11:42  |
Eclipse User |
|
|
|
Hi all,
is it possible to set the default value of an attribute in a subclass?
For example:
class A
String name;
class B extends A
class C extends A
How can I set the default values for the attribute name of the classes B and C?
Thanks!
[Updated on: Tue, 07 February 2012 05:06] by Moderator
|
|
|
|
|
Re: How to set a default value of an attribute in a subclass? [message #959234 is a reply to message #958795] |
Fri, 26 October 2012 10:58  |
Eclipse User |
|
|
|
One problem with this is that the feature is considered set (eIsSet will
return true) but a newly created object should have eIsSet false for all
of its features. Worse yet, if you set it back to the real default,
nothing will be serialized and when you deserialize, it will take on
this artificial default, i.e., setting to the real default won't
preserve it's state when you serialize and then serialize (or similarly
when copying)
On 26/10/2012 10:10 AM, jschoedt Mising name wrote:
> I know this is an old post but I think there is a better way:
>
> We have a feature "visibility" on our NamedObject. All sub-classes can can have its own default value using an Eannotation. In this case called "defaultValues" with a Details Entry "visibility -> 500".
>
>
>
>
> Then in the constructor of NamedObject we check for the annotation and set it:
>
>
> /**
> * <!-- begin-user-doc --> <!-- end-user-doc -->
> * @generated NOT
> */
> protected NamedObjectImpl() {
> super();
> EAnnotation annotation = eClass().getEAnnotation("map");
> if (annotation != null) {
> String vis = annotation.getDetails().get("defaultValues");
> setVisibility(Integer.parseInt(vis));
> }
> }
>
|
|
|
Powered by
FUDForum. Page generated in 0.03870 seconds