Defining Classes - hts.StevenWood.com Home

advertisement
Defining Classes
Why is Java
designed this
way?
Creating our
own class
Implementing
our class
Improving our
class
Using our class
Why did they design it that way?
Java has some awkward compositions:
To read from the keyboard we need:
•
•
•
•
System.in object
InputStreamReader object
A BufferedReader object
A String object
To read from a disk file we need:
• A FileReader object
• A BufferedReader object
• A String object
Complicated!
But powerful
Solving the problem
We can create our own classes that
provide the required behaviour
I want to describe a class that Java
doesn’t contain
It will do something I need
Woof
I want to keep track of dogs
Steps to create your own class
1. Decide on the behaviour that the class will
provide
2. Determine the way the class will be used by
others (its interface)
Determine the prototypes of the methods
3. Write a sample program using the class
Test the design
4. Write a skeleton of the class definition
Class boilerplate with prototypes and empty
method bodies
1. Determining the behavior
Actions
What can it do?
If we had a class called Dog we would
want it to perform these actions:
Characteristics / Attributes
Think about attributes or as they are
also called instance variables
Important information about the object
E.g. a chair: 4 legs?, colour?, material?
Instance variables are available to ALL
methods in the class!
What attributes do we want our dog to
know?
Attributes of the dog
2. Interface and Prototypes
Interface: way in which one can use
an object of a particular class
With Java we need the ability to:
a. Declare a Dog as follows:
Dog fluffie;
2. Interface and Prototypes…2
b. Instantiate (create) a new Dog object:
fluffie = new Dog(<<arguments>>);
•
Does this object need any arguments (or
parameters) to set it up?
2. Interface and Prototypes…3
c. Gather additional information about
itself :
d. Change its attributes:
2. Interface and Prototypes…4
d. Send the dog a message to do these
things:
2. Interface and Prototypes…5
Develop prototypes for the methods
Make sure method names follow java
conventions
2. Interface and Prototypes…6
constructor method:
Defines the “default” look or state of an
object
E.g. a student object must have a first
name, last name, student number (and
other items)
2. Interface and Prototypes…7
Constructor (continued)
public Dog( <<…>>)
constructor has same name as class
In our example it may need arguments
• Age?
Name?
Fur colour?
2. Interface and Prototypes…8
Returning information
Send information to the dog
• Get it to bark
• Get it to roll over
2. Interface and Prototypes…9
Getting information
Ask the object (Dog) to give us this
information
• What’s your name dog?
2. Interface and Prototypes…10
Change information
Ask the dog to change some information
• Change your dog’s name
3. Sample program using class
Program serves two purposes
Help clarify how our new class is to be
used
Checks our interface design to see if it is
satisfactory
• Will it do what we need it to do?
4. Class definition skeleton
class Dog {
// instance variables, if needed
…
// constructor no return type
public Dog(<<arguments>>) {
statements
}
Implementing the Triangle
Class
Implementing the Triangle class
Implementation:
writing the method bodies and
declaring any instance variables
Start with any method
Start with the easy methods!
For your class…
Determine it’s behaviour
Determine it’s attributes
Design 3 ‘get’ methods
Design 3 ‘set’ methods
Make sure you include any required
arguments (stuff to make the method work
e.g. Color c)
Download