package com. example test; //A bicycle class Example taken from Oracle Docs public class(Bicycle public int cadence; public int gear public int speed publicBicycleint startCadence, int startSpeed, int startGear) gear startGearj 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 publicBicycleint startCadence, int startSpeed, int startGear) gear startGearj cadence startCadence; speed startspeed To create a bicycle object Bicycle myBicycle new ( Bicyclex5, 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
Object 厂家 Soldier a Soldier 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 e.g., Bicycle, Circl 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(, compute TotalWidtho 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 lerarchies package com. example test //a bicycle class Example taken from Oracle Docs public class Bicycle i 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