|
|
||
| You are here: | ||
|
Mastery aspects: Objects as records. Recall that in Java the new keyword calls the constructor method of a class.
I know it isn't much fun to do the comments but better to do them now than go back over 2000 lines of dossier code and comment it all!
Again, we see the comments are shown in the create Object dialog.
The comments can be used to produce html documentation fo the class, saving a lot of effort later.
Standard level students shouldn't really worry that much about inheritance.
The toString() method is often useful for debugging code. Here is the current state of the Biscuit Class code .
|
on this page: [ constructors | accessors | inherited ] The constructor is a special method used to create a new object instance. Just as with other methods, we can have more than one constructor, always provided that the parameters are different. We have methods that change the properties of an instance, we can use these to make a constructor that creates a new biscuit object with properties we want to set: /** This method could be called from another Class to create a biscuit: Biscuit custardCream = new Biscuit ('S', "Creamy", 1.50, 4, false); Or directly from Bluej, if desired:
The data members which carry the properties of each biscuit instance are private and therefore cannot be accessed outside of the Class. This is generally considered good practice. It is possible that we will want to change the internal structure of the class at some future time but without affecting ("breaking") any code that might be using the Class. Therefore we use accessor methods to return the properties of a particular instance: /** It's quite acceptable to compress these methods if you like to save space, don't forget the comments though: /** Exercise Complete the accessor methods. You might have noticed the BlueJ reference to "methods inherited from object" when right-clicking a created object instance on the workbench. In Java, all Classes inherit from the Object Class which has several methods of its own. New Classes which are subclasses have access to all public (and protected ) methods of the superclass. We'll cover this further later on. One of the more important methods of Object is toString() which gives a String representation of the Object. It is generally a good idea to replace this inherited method with a description of your particular object. /** Now, we have a data Class, next we'll move on to using it inside another Class - a Class to display Biscuits in a Panel . If you are happier using the console for simple text-based programs you might simply want to alter the toString() method to make a prettier display on the console. Then youy won't need any of the code on the next page at all. Related: [ Java home | previous: biscuit class | next: biscuit display ] |
More about the Biscuit project. |
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |