PowerPoint **

Download Report

Transcript PowerPoint **

What is an object?
What is an object(物件)?
• An object is a software bundle of variables and
related methods.
• Objects are related to real life scenario
• Class(類別) is the general thing and object is the
specialization of general thing.
• Objects are instance (實例) of classes.
• Declaration of an Object in JAVA
– ClassName objectName=new ClassName();
– E.g.: Person objPerson= new Person();
Characteristics of an Object
• An object is characterized by concepts like:
– Attribute or field(欄位)
– Behavior or method(方法)
– Identity (標識)
What is an Attribute?
• Attributes define the characteristics of a class.
• The set of values of an attribute of a particular
object is called its state.
• In Class Program attribute can be a string or it
can be a integer.
Example of Attribute
What is a Behavior?
• Every object has behaviors.
• In JAVA, behaviors of objects are written in
methods.
• If a behavior of an object needs to be
performed, then the corresponding method is
called.
Example of behavior
What is an Identity?
• Each time an object is created the object
identity is been defined.
• This identity is usually created using an
identifier which is derived from the type of
item.
Example of Identity
Example in Java
class Bicycle{
String brakes; //煞車
String color; //顏色
int gear; //檔位
int cadence; //踏頻
public void changeSpeed(){}; //改變速度
public void changeColor(){}; //改變顏色
public void changeGear(){}; //改變檔位
public void changeCadence(){}; //改變踏頻
}
Instance of a Bicycle
Bicycle objBic2009 = new Bicycle();
objBic2009.color = “#FF0000”;
objBic2009.gear = 5;
objBic2009.cadence = 60;
objBic2009.changeColor();