23
Watcharin Puangplia 29/04/2011

class and object1

Embed Size (px)

DESCRIPTION

class and object1

Citation preview

Page 1: class and object1

Watcharin Puangplia 29/04/2011

Page 2: class and object1

Class attribute

method

Class ��� ��������� ���������������� 2 �� ���� Class ��� ��������� ���������������� 2 �� ���� - attributes ��� ���� ����!���� ��� ���� - methods ��� "#�����$%� ������������$��&��

Page 3: class and object1

Class attribute

Object A Object B

methodObject C Object D

Object '��� ���� (�)�����$%��������*��+�,-�����(�)����.�� +�Object .������ !�%�� ���� ����!���� (�)������(�� Object ��� ����$%��/��������*�!�(�%�� �� Instance �������

Page 4: class and object1

Rectangle

length

(�%� Diagram �������&������%*

length

setLength

width

setWidth

getWidth

getLength

Rectangle

lengthwigth

setLength()setWidth()getLength()getWidth()getArea()

Class name

Attribute

Method

getAreagetArea()

Page 5: class and object1

Class Rectangle

length

width

Object myRec

10

setLength

5

setWidth

setLength

setWidth

getWidth

getLength

getArea

getWidth

getLength

getArea

20

Object yourRec

setLength

15

setWidth

getWidth

getLength

getArea

Page 6: class and object1

Music player state:current songvolumebattery life

behavior:power on/offchange station/songchange volumechange volumechoose random song

Music player #1

state:song = "Thriller"volume = 17battery life = 2.5 hrs

Music player #2

state:song = "Lovesong"volume = 9battery life = 3.41 hrs

Music player #3

state:song = "Closer"volume = 24battery life = 1.8 hrs

creates

battery life = 2.5 hrs

behavior:power on/offchange station/songchange volumechoose random song

battery life = 3.41 hrs

behavior:power on/offchange station/songchange volumechoose random song

battery life = 1.8 hrs

behavior:power on/offchange station/songchange volumechoose random song

Page 7: class and object1

[AccessSpecifier] class Name{Attribute Member

� AccessSpecifier � ������,����(������ (-�� public

� class (�)��%0( ��0�,-������1����� Name -�������$%������1��*�

Attribute MemberMethod Meber

}

� Name -�������$%������1��*�� Member ��-������ 2 ,����� (-�� Atribute '��� Method

Page 8: class and object1

[AccessSpecifier] dataType Name;

� AccessSpecifier '��� modifier ������������� ��������������������������� � 4 �!���� public, protected, private,

default

dataType "#�" $��������%���� Attribute &'� int , char� dataType "#�" $��������%���� Attribute &'� int , char

� Name &������ Attribute ��� &����!��"

Page 9: class and object1

[AccessSpecifier] return_type Name([arguments]){Method body

� AccessSpecifier '��� modifier ������������� ������ Method ������������� � 4 �!���� public, protected,

private, default

� return_type (�)����(3$���/�$%��������(��� method $�����(��4!

Method body}

� return_type (�)����(3$���/�$%��������(��� method $�����(��4!� Name -��� Method ��!�(�������� ������� (-�� get , set

� arguments (�)��� .�����0�� (��0$%�,-�������/�(����/� method ��!!�&�%�4&��

Page 10: class and object1

� Static Method

◦ Method $%�%��� �� Static ���'��� �����(�%�,-�&��(�+�&���������� Object ◦ Method $%�%��� �� Static ���'��� �����(�%�,-�&��(�+�&���������� Object

����

� Instance Method

◦ Method $%�&�% Static ���'��� ��������� Object �������!�(�%�,-� Method &��

� Overloading Method

◦ Method '�� 2 Method $%�%-���('������ .��%!��� � '��� -������"����(���0��������������

Page 11: class and object1

(�)� Method $%������(�%�,-����&��+�&���������� Object �� ����(-��

public class SimpleMethod

�� %$%��/�,� Class (�% ��� (�%�,-�-��� Method ��*�&��(�

public class SimpleMethod{ // �5���(����������+��.�� public static void main(String[] args) // (6��'��� { System.out.println("Hello from the main method.");

displayMessage(); // (�%�(6�� displayMessage() $%��������*�

System.out.println("Back in the main method."); }

public static void displayMessage() // (6��,'�$%��������*� { System.out.println("Hello from the displayMessage method.");

}

}

Page 12: class and object1

public class LoopCall{

public static void main(String[] args)

{

System.out.println("Hello from the main method.");

��/�(�%�(6��$%��������*��$����� 5 ���*�for (int i = 0; i < 5; i++)// ��/�(�%�(6��$%��������*��$����� 5 ���*�

displayMessage();

System.out.println("Back in the main method.");

}

public static void displayMessage() // (6��$%��������*� {

System.out.println ("Hello from the displayMessage System.out.println ("Hello from the displayMessagemethod.");

}

}

Page 13: class and object1

public class LoopCall{

public static void main(String[] args)

{

�� %$%��/����� Class ��� (�%�,-�-��� Class ���� !�� . .��-��� Method

{

System.out.println("Hello from the main method.");

for (int i = 0; i < 5; i++) // ��/�(�%�(6��$%��������*��$����� 5 ���*�

LoopMethod.displayMessage();

System.out.println("Back in the main method.");

}

}

class LoopMethod { class LoopMethod {

public static void displayMessage() // (6��$%��������*�

{

System.out.println("Hello from the displayMessage me thod.");

}

}

Page 14: class and object1

Method &�����������.��&�% parameter

,�� void '���-��� method

public void displayMessage(){

System.out.println(“Hello”);

}

Method &�����������.��&�% parameter

Method &�����������.��% parameter

public void welcome( Srting name){

System.out.println(“Hello ” + name );

}

Method &�����������.��% parameter

Page 15: class and object1

,�� ���(3$���/�$%�������� $%�'���-��� method

.��,�� return ���$%�������� ,����$�����$����� method

public double getLength(){

return length;

}

Method ����������.��&�% parameter ,-�,���������� Attribute ��� ����

Method ����������.��% parameter

public int add( int x,int y){

return x+y;

}

Method ����������.��% parameter

Page 16: class and object1

public class Rectangle{

private double length;

private double width;

Data Member (Attribute)

Method Member

public void setLength(double len){

length = len;

}

}

class Sawasdee{

Method Member

Data Member (Attribute)class Sawasdee{String data="Sawasdee ";

public void show(String s){System.out.println(data+s);

}}

Data Member (Attribute)

Method Member

Page 17: class and object1

�������� Object !������ $��&�����.������%*

�6%$%� 1. ���'��-������ Object ����.�� !������� Object

ClassName ObjectName;

ObjectName = new ClassName([arguments]);

�6%$%� 1. ���'��-������ Object ����.�� !������� Object

Rectangle box ;

box = new Rectangle();

ClassName ObjectName = new ClassName([arguments]);

�6%$%� 2. ���'��-������ Object "����������� Object

Rectangle box = new Rectangle();

Page 18: class and object1

Rectangle box ;

box

����� Reference ������� Rectangle $%�-��� box

������������� Object box ������ Rectangle

adress

box = new Rectangle();

adress

boxlength :0.0

Object Rectangle

���'��,'� Reference $%�-��� box ,'�-%*&��� Object ������� Rectangle

adresssetLength

Width:0.0

setWidth

getWidth

getLength

getArea

Page 19: class and object1

��*��/����������'�� AccessSpecifier ��� Attribute .�� Method $%�&�����& �,�����Method $%�&�����& �,�����- private !�(�%�,-����&��!��3�,�����(�% ���($����*�- public !�(�%�,-����&��!��$�*�3�,�����(�% ���.���������

�������ObjectName.AttributeMember; //������� Atrribute

ObjectName.MethodMember(); //������� Method

�/�.�� ����-��� ����.�� ���� (������'�!�� . .�� ���� ��-���������

box.setLength(10.0);

(-�����(�%�,-� Method setLength() ��� Object box

ObjectName.MethodMember(); //������� Method

Page 20: class and object1

box.setLength(10.0);

box.setWidth(5.5);

adress

boxlength :0.0

Width:0.0

Object Rectangle

length :10.0

Width:5.5setLength

setWidth

getWidth

getLength

getArea

Width:5.5

Page 21: class and object1

public class Rectangle{

private double length;

private double width;

public void setLength(double len){ //����������������������������

length = len; //����������������������� length = len; //�����������������������

}

public void setWidth(double w){ //������������������

width = w;

}

public double getLength(){ //������������������������� ����������

return length; //�!����������� ����������

}

public double getWidth(){ //�������������������������� ���������� public double getWidth(){ //�������������������������� ����������

return width;

}

public double getArea(){ //������������������"!#�$%& ����������

return length * width; //�!����"!#�$%& ����������

}

}

Page 22: class and object1

public class RectangleDemo

{

public static void main(String[] args)

{{

Rectangle box = new Rectangle();// ��������(!4��0-��� box ,'�(�)��%�('�%�

box.setLength(10.0); // ���'��� �� ��� box

box.setWidth(20.0); // ���'��� �� ������ box

System.out.println("The box's length is " +

box.getLength());// .���9�� �� ��� box

System.out.println ("The box's width is " +System.out.println ("The box's width is " +

box.getWidth());// .���9�� �� ������ box

System.out.println("The box's area is " +

box.getArea());// .���"�*�$%���� box

}

}

Page 23: class and object1

class CalScore{ // ��������� calscore

public void calculate(){ // (6�����'���'�(���

double score = Math.random()*100; // ��������.����' ��� 0 ��� 100

if (score >= 80){ // �����.����� �� 80if (score >= 80){ // �����.����� �� 80

System.out.println("Grade is A"); // &��(��� A

}else if (score >= 70){ // ���&���� �� 80 .����� ��'���($����� 70

System.out.println("Grade is B"); // &��(��� B

}

else{ // '��(�)���.������2

System.out.println("Grade is C"); // &��.���

}

} // !�(6�����'���'�(���

} //!����� CalScore} //!����� CalScore

public class CalGrade{ // (6�����+��.��'���

public static void main(String args[]){

CalScore obj = new CalScore(); // ��������(!4��0-��� obj ������� CalScore

obj.calculate(); // (�%�,-�(6�� calculate

}

}