1 / 29

Defining Classes I

Defining Classes I. Part A. Class definitions. OOP is the dominant programming methodology in use today. Classes are the most important language feature of OOP. Instance (value) of the class = a particular object (a particular value of a class type)

talib
Télécharger la présentation

Defining Classes I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Defining Classes I Part A

  2. Class definitions • OOP is the dominant programming methodology in use today. • Classes are the most important language feature of OOP. • Instance (value) of the class = a particular object (a particular value of a class type) • A class is a type and you can declare variables of a class type.

  3. Class definitions • Attributes, fields, or properties = data • Methods = function • Members = data + functions (or attributes + methods)

  4. Example class • Consider dates such as December 31, 2007 and July 4, 1776. • We would like to develop a class to represent dates. public class Date { } • What’s our next step?

  5. Example class • What attributes (data) do we need? • To represent the month? • To represent the day? • To represent the year?

  6. Example class public class Date { public String mMonth; public int mDay; public int mYear; } • What does “public” mean for the month?

  7. Conventions (mandatory) • One class per file. File name is always classname.java. • What does “public” mean for the month? • Let’s adopt the convention that all class names will begin with an uppercase letter. • Let’s adopt the convention that all attributes will begin with ‘m’ (to distinguish them from local variables).

  8. Example class public class Date { public String mMonth; public int mDay; public int mYear; } • How do we declare an object of type Date? • How do we create an instance of this class?

  9. Example class public class Date { public String mMonth; public int mDay; public int mYear; } • How do we declare an object of type Date? Rule of Date dt; • How do we create an instance of this class? dt = new Date(); Date d2 = new Date(); //both ops

  10. Class definition • In general, a class definition is of the form: public class Class_Name { Instance_Variable_Declaration_1 Instance_Variable_Declaration_2 … Instance_Variable_Declaration_Last Method_Definition_1 Method_Definition_2 … Method_Definition_Last }

  11. Example class • A common operation is to ask the object to create a string that represents it values. We’d like to say: Date dt = new Date(); System.out.println( “date is “ + dt.toString() ); • What must we add to Date to make this happen?

  12. Example class public class Date { public String mMonth; public int mDay; public int mYear; public String toString ( ) { return mMonth + “ “ + mDay + “, “ + mYear; } }

  13. Methods in general • (aka function, procedures) • Method definition vs. method invocation • May have an optional return statement to optionally return a value to the caller.

  14. Methods in general • Definition: public type_returned method_name ( optional_parameter_list ) { //body of method (containing local var definitions and other // statements. //if type_returned is not void, there must be at least one return // statement that returns a value to the caller. }

  15. Example methods public int getDay ( ) { return mDay; } /** yet another overloaded version of setDate with * a single parameter of type int */ public void setDate ( int year ) { setDate(1, 1, year); }

  16. Return statement • The return statement exits the current function and optionally returns a value to the caller. public int getDay ( ) { return mDay; } … int d = dt.getDay();

  17. Returning boolean • Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; }

  18. Retuning boolean • Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; } public boolean checkRange2 ( int i ) { return (1<=i && i<=6); }

  19. Types of variables • Local • Declared w/in methods • Function parameters can be thought of as local variables • Class • Declared w/in a class • Not declared w/in any method • We will adopt the convention of starting all of these class variables with m. • Ex. String mMonth;

  20. The this parameter • Every (non static) method has an implicit, pre-defined parameter called this. • this can be used to refer to class variables or methods. • this is a keyword. • this refers to the current object instance.

  21. When do we use this? • When a function parameter (or local variable) name masks a class variable name. • But our convention avoids this situation in most cases. So you shouldn’t need to use this very often. • But it doesn’t hurt: public int getDay ( ) { return this.mDay; }

  22. When we must use this int where = 12; public void foo ( int where ) { where = 92; } Did the class variable called where change to 92?

  23. When we must use this int where = 12; public void foo ( int where ) { this.where = 92; where = 7; }

  24. Determine if two dates are equal • Recall what happened when we used == to test if two strings are equal. • What method did we use? • Let’s write our own for dates.

  25. Determine if two dates are equal public boolean equals ( Date other ) { return ( mDay == other.mDay && mMonth.equals( other.mMonth ) && mYear==mYear ); }

  26. Function to determine if one date precedes another date • Note that December 31, 1970 precedes January 1, 1971. • Note that December 31, 1971 does not precede January 1, 1970. • Note that January 1, 1970 does not precede January 1, 1970. • Write a function to determine if one date precedes another date.

  27. public boolean precedes ( Date other ) { return ( ( mYear<other.mYear) || (mYear==other.mYear && getMonth()<other.getMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) && mDay<other.mDay) ); } I personally would never write it this way!

  28. public boolean precedes ( Date other ) { return ( ( mYear<other.mYear) || (mYear==other.mYear && getMonth()<other.getMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) && mDay<other.mDay) ); } public boolean precedes2 ( Date other ) { if (mYear < other.mYear) return true; //year must be the same if (getMonth() < other.getMonth()) return true; //year and month must be the same return (mDay<other.mDay); }

  29. Testing • Use a separate program called a driver to test the classes that you develop. • Drivers typically have a main. Your classes should not.

More Related