Upload
watcharin-puangplia
View
214
Download
1
Tags:
Embed Size (px)
DESCRIPTION
class and object1
Citation preview
Watcharin Puangplia 29/04/2011
Class attribute
method
Class ��� ��������� ���������������� 2 �� ���� Class ��� ��������� ���������������� 2 �� ���� - attributes ��� ���� ����!���� ��� ���� - methods ��� "#�����$%� ������������$��&��
Class attribute
Object A Object B
methodObject C Object D
Object '��� ���� (�)�����$%��������*��+�,-�����(�)����.�� +�Object .������ !�%�� ���� ����!���� (�)������(�� Object ��� ����$%��/��������*�!�(�%�� �� Instance �������
Rectangle
length
(�%� Diagram �������&������%*
length
setLength
width
setWidth
getWidth
getLength
Rectangle
lengthwigth
setLength()setWidth()getLength()getWidth()getArea()
Class name
Attribute
Method
getAreagetArea()
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
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
[AccessSpecifier] class Name{Attribute Member
� AccessSpecifier � ������,����(������ (-�� public
� class (�)��%0( ��0�,-������1����� Name -�������$%������1��*�
Attribute MemberMethod Meber
}
� Name -�������$%������1��*�� Member ��-������ 2 ,����� (-�� Atribute '��� Method
[AccessSpecifier] dataType Name;
� AccessSpecifier '��� modifier ������������� ��������������������������� � 4 �!���� public, protected, private,
default
dataType "#�" $��������%���� Attribute &'� int , char� dataType "#�" $��������%���� Attribute &'� int , char
� Name &������ Attribute ��� &����!��"
[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&��
� Static Method
◦ Method $%�%��� �� Static ���'��� �����(�%�,-�&��(�+�&���������� Object ◦ Method $%�%��� �� Static ���'��� �����(�%�,-�&��(�+�&���������� Object
����
� Instance Method
◦ Method $%�&�% Static ���'��� ��������� Object �������!�(�%�,-� Method &��
� Overloading Method
◦ Method '�� 2 Method $%�%-���('������ .��%!��� � '��� -������"����(���0��������������
(�)� 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.");
}
}
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.");
}
}
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.");
}
}
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
,�� ���(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
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
�������� 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();
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
��*��/����������'�� 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
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
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; //�!����"!#�$%& ����������
}
}
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
}
}
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
}
}