Java Reference
In-Depth Information
return this.fName + " " + this.lName;
},
set fullName(name) {
names = name.split(" ");
if(names.length === 2) {
this.fName = names[0];
this.lName = names[1];
}
else {
throw new Error("Full name must be in the form
'fName lName'.");
}
}
};
// Get the full name using the fullName accessor property and print it
print("Full name is " + john.fullName);
// Set a new full name
john.fullName = "Ken McEwen";
// Get the new full name and print it
print("New full name is " + john.fullName);
Full name is John Jacobs
New full name is Ken McEwen
Notice that you set the first and the last names of the person by setting the fullName
property. When you set the value of the fullName property, the value is passed to the
setter method of the property that sets the first and last names, provided the value follows
the "fName lName" format.
Setting Property Attributes
You can set attributes for data and accessor properties of an object. Table 4-10 lists
the attributes of properties. Note that not all attributes are applicable to all types of
properties.
 
 
Search WWH ::




Custom Search