- doing sth. with state: data structure
- doing sth. with methods: algorithms
- We do not trust clients! When they pass us parameters, we need to check its' correctness
- String: check null
- How to decide the state: clients are god! we give what they need!
- The reason we are using an object class is not say the class including main might be too long. It is because of the efficiency and share.
-
Object Init Constructors
-
basic
- obj's init state
- public <Class name>(para) {<statement>}
- default constructor new Point();
-
common mistake
- NO return types
- NO redeclare fields
-
The keyword this
-
this
- refer to implicit parameter
- this.<field name>
- this.<method name>(<expre>)
- shadowed variables
-
Multiple Constructors
- give clients different way to construct
- different signature
- public Point() {this(0,0);}
-
Encapsulation
-
Basics
-
encapsulation
- encapsulate is not totally hiding the field
- It is just not giving the client access to modify it
- abstraction
- private <type> <name> = <value>;
- + public int getX(){return x;}
- The accessor methods give the client read-only access
-
Sun's style
- fields>constructors>methods
-
class invariant
- An assertion about an obj's state remain true for the lifetime
- precondition to constructors and postcondition for method
- throw new IllegalArgumentException();
- Changing internal Implementations
-
Case Study
-
Object-Oriented Design Heuristics
-
1. cohesion
- single abstraction
- objects should NOT handle console I/O for less interwind
- 2. reduce unnecessary coupling
- 3. Related data and behavior should be in the same place
-
CRC card
- class
- responsibilities
-
collaborators
- which class it couples
-
Stock Fields and Method Headers
-
store values
- void
-
process values
- return type
-
Stock Method and Constructor Implementation
-
constructor
- init
- pass string cannot be null
- throw new NullPointerException();
- pass value
- throw new IllegalArgumentException();
-
Object State and Behavior
-
Object State: Fields
-
Field
- variables in an obj of its inner state
- every obj will have it
- client code will use Point class by create Point object
-
record/struct
- state, no behavior
-
Obj behavior: Method
-
Instance method
- call: <obj name>.<method name>(para)
- without specify the obj in the method
- obj contains state and methods, they are stored as an entity
- cause it is "instance"'s methods
-
static method
- <class name>.<method name>(para)
- if in the class, <method name>(para)
-
the Implicit Parameter
-
implicit parameter
- a.<method>(b)
- a is impl, b is expli.(kind of like y=kx+b)
-
Mutators and Accessors
-
Mutator
- Common convention:
- methodName
- set***; setID; setTitle
- modify internal state
- read-write
-
Accessor
- 1.info no modify 2. return a related value without changing obj inner values
- e.g. subString, length, String...
- return station
- common convention
- get***; is***
- read only
-
the toString method
- auto trans to String, when concatenated with String
- .toString
- inheritance
- usually println does not include in class because it limits the output format
- toString returns a string that can be used by println
-
Object-Oriented Programming
-
Basics
-
OOP
- actions --> objects
- e.g. GUI
-
Objects
- entity = state + behavior
- instances
-
State
- values
-
Behavior
-
actions
- reporting
- modifying
-
Client (Code)
- code, interact with objects or class
-
class
- blueprint
-
Classes and Objects
-
create object-->class
- state
- behavior
- how to construct
- class-->objects-->client prog
-
Point Objects
- 2D