The following notes are based on the book: << Head First JAVA >> and video series: 手把手教你用Java基础教程 - 北京尚学堂 - 马士兵
Process - oriented programming
- divde into processes
- process by process
Object - oriented programming
- divide into objects(tools)
- realized by methods within a object
- everthing in java is a object
- Resuseable, Extensibility, easy to maintain and replace
Object and class
- A object is an instance of a class.
- A class is a template.
- A object has attribute (member variable) and method.
Relationships between classes (more later)
- Inheritance
- Association
- Composition
- Aggregation
- Realization
Define a class
- Member variables can have a default.
- otherwise java automatically init them (0, ‘0’, null).
1 | public class Person(){ |
Construction method
- same name as class
- no
return
, novoid
1 | public class Person{ |
Default construction method
- if no construction method is specified, default is used:
1 | Person(){} |
- otherwise, default is overridden.
Overload construction method
- Mutiple construction methods are allowed.
- Method names have to be same (same as class name).
- Can be distinguished by either:
- number of parameters
- paremeters’ types
1 | // init person with specific id and age |
Overload normal method
- same as above
- Method return type also need to be same.
Reference type
- takes two space in ram. (address and instance)
- address is saved in stack ram.
- new instance is saved in heap ram.
- method does not use space until called!
1 | String s; // create a reference type, is |
Rule of naming
- Class names begin with upper case.
- Variable names and method names begin with lower case.
- Use camelSignal if a name contains mutiple words.