531/notes-simple-classes.txt ---------------------------- Classes -- defining and using them. OOP - Object Oriented Programming program: objects of certain types (classes) send each other messages to tell them to carry out "methods" (procedures) main() method -- special case, to get program started. objects = data + "methods" (procedures) you can ask them to carry out. Examples: Class defines a *type* of object: Car Person Ant AntEater Firm Instance specific instances of the class One Ant instance for each "ant" Each represents a particular "entity" being modeled. Fields slots for storing data Ex: Age Height MaxSpeed Weight Instance variables (fields) -- slots in each instance Because different objects in program represent different entities Class (static) variables (fields) -- slots in the Class object itself same value for all instances of the class. MutationRate To Define a Class: YourClassName extends (subclass of) SomeClass fields: names, data types; access control; May be static; methods: names ( parameters) and what they do! May be static constructors: special methods invoked with "new" to create new instances To use a class Define a "reference" variable to hold address of (pointer to) object Use "new" to create an instance, store in the ref. variable Send the instance messages to ask it to do things (ie execute methods). General form: addressOfObject.methodName() Examples: double d = Math.cos(2.1); double r = rng.nextGuassian(); o.setWeight( 2.2 ); See SimpleClass1.java program for examples of all the above operations. Read in JavaTech (or elsewhere) Class Definition Data Fields; Methods; Constructors Note: method signature: overloading public void setWeight ( double wt ) ^^^^^^ ^^^^ ^^^^^^^^^ ^^^^^^^^^ access return name arguments (aka parameters) value type method body: everything it does, between the { ... } after the signature variables available: static (class) fields instance fields local fields ------------------------------------------------------------------------ ------------------------------------------------------------------------- Abstract view of classes/methods: (See the Chapter 3 Supplemental material) Class as new type of data, e.g., like "int" but more complex! fields -- what data is stored int -> store an integer methods -- like operators on the data: what can be done Thus define and use these new "building blocks" in your programs. --------------------------------------------------------------------------