Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java 5
Class Anatomy
User Defined Classes
• To this point we’ve been using classes that
have been defined in the Java standard
class library.
• Creating classes provides us with more
flexibility.
• See book example, p. 214
Visualizing Class Files 1
public class account
Instance variables
or the data fields
(properties) of the
object. These are
private and can
only be accessed
by local methods.
{
private int acctNumber;
private double balance;
public account(int a, double b) {
acctNumber = a;
balance = b;
}
public void printBalance() {
System.out.println(“Bal: “ + balance);
}
}
Visualizing Class Files 2
public class account
Constructor builds
the object and
initializes any
default values.
Objects do not
require a
constructor.
{
private int acctNumber;
private double balance;
public account(int a, double b) {
acctNumber = a;
balance = b;
}
public void printBalance() {
System.out.println(“Bal: “ + balance);
}
}
Visualizing Class Files 3
public class account
{
private int acctNumber;
private double balance;
public account(int a, double b) {
acctNumber = a;
balance = b;
}
Methods are what
an object can do.
This method prints
out the balance of
our account.
public void printBalance() {
System.out.println(“Bal: “ + balance);
}
}
Using a Class (driver)
Notice the driver
file has a main
method and
creates a new
object using the
constructor and
rules defined in the
account class.
public class accountRun
{
public static void main(String[] args)
{
account a = new account(325,63000.27);
a.printBalance();
}
}
Instance Data
• An object is an instance of a class.
• Variables defined in classes are governed
by the concept of “variable scope”
• See code example on web site.
• Java automatically initializes class variables
but it is good practice to do it anyway.
Encapsulation
• At the object level we think of how to design
the class.
• At the project level we think of how to design
objects and how those objects will interact
with each other.
• Objects should be “self-governing”
• Only the class that declares a variable
should be able to change that variable!
Encapsulation Pt. 2
• Keywords help us set up this protection.
public
&
private
private int total = 5;
public void setTotal(int a) {
total = a;
}
Basic Methods
•
•
•
•
Modifier (public or private)
Return type (void, int, float, string, etc.)
Identifier (its name)
Parameters (arguments to method)
public void sayHello() {
System.out.println(“Hello”);
}
Constructors
• Methods with the same name as the class
called when new objects are initialized.
• Return nothing.
• Not required, Java has a built-in constructor
if a class doesn’t define one.
Jason myObj = new Jason();
Return Values
• Methods can return values to the calling
code.
public int even(int a) {
if(a % 2 == 0)
return 1;
else
return 0;
}
Method Parameters
• Parameters must be passed into a method
in the proper order.
• The parameter along with the method’s
name is called its “signature”.
• Signature does not include return type.
• Method parameters are local to the method
in scope.
Method Overloading
• A class can contain several methods with
the same name that differ only by their
parameters.
• This is called method overloading.
• Methods are distinguished by their
signature.
• An example of an overloaded method we’ve
used frequently is println().
Method Decomposition
• Sometimes we want to write a method that
is too big and cumbersome and so we break
it down into multiple, smaller methods.
• Always try to break your code down into the
smallest sensible blocks to make
maintenance easier.
• Too many small methods may actually
complicate things so be careful.