Class
Class ���º��º��Ѻ��������Ǣͧ object ���ͧ�ҡ㹡�����ҧ object ˹�� � �е�ͧ��ҧ�ԧ������ҧ�Ҩҡ Class ����� � �Ѻ������ҧ��ҹ�ѡ��ѧ˹�� ��ͧ������ҡ������ҧẺ�Źŧ����������ǡ� ��ͨҡ��鹡�ŧ������ҧ���Ẻ ��觶��������ҧẺ��ҹ������� ����������ö���ҧ��ҹ�� �������������㨤���ᵡ��ҧ�����ҧ class ��� object ����ҡ��� ��� ������ʴ���������� class ���Ẻ�ٻ��Ƿ�����ҧ�ҡ�����(templates) ��� object ��� �ٻ��Ƿ�����ҧ�ҡ���Ẻ�ٻ��Ƿ���˹�ҷ���� class ��� ��� object ���١���ҧ����Ҩ����ѡɳ������Ѻ class ����繵�Ẻ ���¤س ���ѵԢͧ object ���Ǩ�����ö�����س���ѵ��Тͧ����ͧ������� 㹷�������ʴ�������������� object������� �� �繤س���ѵ��Тͧ���� object �ͧ ��س���ѵԾ�鹰ҹ�����Ѻ�����͡�Ҩҡ class ����繵�Ẻ
����ᵡ��ҧ�����ҧ Class & Object
�������ҹ��ҧ����ʴ��������� Java ���ʴ������繶֧����ᵡ��ҧ�����ҧ Class ��� Object �ѧ���
Attributes
Data �ͧ object �ж١᷹���� attribute �·������ object ���� attribute �����ʶҹТͧ object ����»á��������Ҩ����حҵ��� object �����Ҷ֧ attribute �� ���� object �� (�¡�˹�ʶҹ������ private) attribute �����¡���������¡��ҹ method ���� object ��ҹ�� �·�������� attribute �Ҩ���繤�Ҥ���� ���͵���÷���������� method �Ԩ�óҵ�����ҧ�ҡ Person class ���������� attribute ���� ����� name ��� address �ҡʶҹз���˹��� private �ʴ���� attribute name ��� address �ж١���¡��ҡ��¹͡ Person class ��� ��������� java �������ö��˹���� attribute �� public �������ö���¡��ҡ��¹͡
Code �ͧ Person class
|
public class Person { private String name, address ; public String getName () { return name ; } public void SetName (String n) { name = n ; } public String getaddress () { return address ; } public void setAddress (String adr) { address = adr ; } } |
Method
Method ������ͧ��ͷ��������� class ���� object ��� method ��������ҧ object �ѹ������ö���¡���觡ѹ��Сѹ�� (��� method ��������ʶҹ��� public ) 㹷ҧ Object-Orient programming ���� method ����ö���º��º��Ѻ function , procedure ���� subroutine � Precedural programming ��Шҡ������ҧ������ͧ Person class ���Ǣ�͡� ���������� method ��� getName( ), setName( ) , getAddress( ) ��� setAddress ( ) 㹡���͡Ẻ�ҧObject-Orient ���� method 㹡�äǺ��������Ҷ֧ attribute �ҡ object ���
Message
Message ��� ���㹡��������������ҧ object ������ҧ�� object A ��ͧ�� method �ͧ object B �¡�÷�� object A ���� message 价�� object B ��� object B ��еͺʹͧ���觤�ҷ�� object A ��ͧ��á�Ѻ�
|
public class Person { private String name , address ; public String getName () { return name; } public void SetName (String n) { name = n; } public String getAddress () { return address ; } public void setAddress(String adr) { address = adr ; } } |
Sequential operation
�������˵�㹡���� message ˹��� ����觼ŷ���� message ��蹶١�觵�ʹ��� ���¡�Ըա�ù����� sequential operation Ժ����ѧ��� ����� object ˹�� �� message �������ͧ�ͤ�Ҽ��Ѿ�רҡ�ա object ˹�觤�Ҽ��Ѿ���鹨����١�觤��Ѻ��㹷鹷� �����ҡ�кǹ��÷ҧ message ��蹨���������ó� ������ҧ�� object A �� message ��ѧ object B �� object B ������觤�Ҽ��Ѿ���Ѻ��ѧ object A 㹷ѹ�� ���ͧ�ҡ��кǹ������㹢ͧ object B ������� message �١����ѧ object C ��� ��ѡɳз������� �ѹ object C ���� message � �ѧ object D ���� object D ��Ф��Ҽ��Ѿ�����ѧ object C ��ͨҡ��� object C ����觤�ҹ����ѧ object B ��������ǡѹ object B ����觤�ҹ���Ѻ��ѧ object A �ҡ��кǹ��ù����������� object A �����Ѻ��ä��Ҩҡ object B ��������͡�кǹ�������ҹ����������ó�����
Encapsulation (Protection)
Encapsulation ����ҡ�ҹ���ҧ˹�觢ͧ�Ǥ����Դ��ԧ Object-Oriented ��觢�ʹբͧ Encapsulation ��͡�û�ͧ�ѹ attribute (data) �ͧ object �ҡ����������� ���ж����ǹ�ͧ����������� حҵ����ա����Ҷ֧ attribute ��������ͧ������ǹ�� ���觼���� attribute ��鹧��µ�͡�ö١�����ҧ�Դ� ������� attribute ����¹�ŧ� ��觡������Դ���� ������µ����Encapsulation ��� ���������� attributes ��� methods ��������¡ѹ
Encapsulation �з�˹�ҷ���ͧ�ѹ����� object ��蹷��������¹͡ ��Ҷ֧ object ˹��� �����ҧ����Ш����� method �������� object ��ҹ�鹷�������ö�Դ��͡Ѻ attribute �������� object ���ǡѹ�����¡��ҡ�� Encapsulation �դس���ѵԢͧ Information hiding
Information hiding ��͡�èӡѴ����ͧ��繢��������� Object �� ��á�˹��س�ѡɳ��繓public������������ö�������͡Ѻ object ��¹͡�� ��С�˹��� �private� ���ͨӡѴ attribute ����������� object ��ҹ��
��ʹ��ա���ҧ˹�觢ͧ��� Encapsulation ��� ������ attribute ��� method �����¡ѹ��˹�� object ��觶���ա������¹�ŧ�Դ��� ���� object ˹�觡������觼š�з� �͡�ҡ��� method �ͧ object ������ö�١����¹�ŧ�����ҧ������������ǡѺ object ��� Attribute ���/ ���� method �ͧ object ��� ��ǹ���ж١���ҡ object
������ҧ ��ҡ�˹���� video cassette recorder (VCR) �� object type ˹�觨��� Sony VCR �� object ���ػ�ó� electronics ��������ͧ ��� data (attribute) ���س�������ö��Ҷ֧�� ���ͧ�ҡ�����Դ, �������§��ͤ���������¢ͧ�ػ�ó� electronice �س����ö������§ operations (method) �� ����ŧ , ����Ѻ , �ѹ�֡ , �Թ˹�� , ��ش���Ǥ��� ��� ����˹������������ҹ�� ��觨��� �ٻẺ�ͧ��������觧ҹ�� interface (��觨������ǵ���) �ػ����ǹ������ Encapsulation ����ػ�ó� Encapsulation ����ػ�ó� electronics ��С�û�Ժѵԡ������ object ���ǡѹ
Page 1>>>Page 2 >>> Page 3 >>> Home
|
Class Employee { public int id: private string name; private string department ; public void setname (String n) { name = n; { public String getAddress () { return name ; } public void setdepartment (String d) { departmen = d; } public String getdepartment () { return department } |
Coppyright2004ByWebmasterAllRightReservedComments