package com.example.test; //A bicycle class:Example taken from Oracle Docs... public class Bicycle{ public int cadence; public int gear; public int speed; public(Bicycleint startCadence,int startSpeed,int startGear) gear startGear; cadence startCadence; speed startSpeed; Constructor A special method which instantiates an object,it has the same name as the class.Must be used with the“new” operator. Code for initialization of the object can be found here.It can have initialization parameters. 11
11 Constructor • A special method which instantiates an object, it has the same name as the class. Must be used with the “new” operator. • Code for initialization of the object can be found here. It can have initialization parameters
package com.example.test; //A bicycle class:Example taken from Oracle Docs... public class Bicycle{ public int cadence; public int gear; public int speed; public(Bicycleint startCadence,int startSpeed,int startGear) { gear startGear; cadence startCadence; speed startSpeed; To create a Bicycle object Bicycle myBicycle new (Bicycle5,10,12); An object is an instance of a class. Class are types of objects. Objects are actual things with memory space 12
12 Bicycle myBicycle = new Bicycle(5, 10, 12); To create a Bicycle object An object is an instance of a class. • Class are types of objects. • Objects are actual things with memory space
20501193/2007 Object Class EEEE随照 Soldier aSoldier new Soldier(); 13
13 Soldier aSoldier = new Soldier(); Class Object
Naming Conventions ● Class Names:Class names representing types must be in mixed case starting with upper case. oe.g.,Bicycle,Circle Variable or Object Names:Variable or object names must be in mixed case starting with lower case. o e.g.,bicycle,circle ● Method Names:Names representing methods or functions must be verbs and written in mixed case starting with lower case. oe.g.,getName(),computeTotalWidth() 14
Naming Conventions • Class Names: Class names representing types must be in mixed case starting with upper case. o e.g., Bicycle, Circle • Variable or Object Names: Variable or object names must be in mixed case starting with lower case. o e.g., bicycle, circle • Method Names: Names representing methods or functions must be verbs and written in mixed case starting with lower case. o e.g., getName(), computeTotalWidth() 14
Package Include class Bicycle in the package com.example.test Java includes the package at a level above classes Packages can contain more than one class definition. Packages often contain libraries and can be defined in hierarchies. package com.example.test; //A bicycle class Example taken from Oracle Docs... public class Bicycle public int cadence; public int gear; public int speed; 15
Package • Include class Bicycle in the package com.example.test • Java includes the package at a level above classes. • Packages can contain more than one class definition. • Packages often contain libraries and can be defined in hierarchies. 15