Interfaces ��� Implement
���� object ���չ����������ʴ�����ᵡ��ҧ�ͧ��ǹ��Сͺ��ҧ � ���� object ��� ��ǹ�ͧ interface ��� implement �·����ǹ interface ��᷹˹�ҡҡ�����Դ��͡Ѻ object ��� ��ǹ implemen �����ǹ���Դ�ѧ��������˹�ҡҡ�ͧ interface ��� object �����¹͡���������ö�����
Interface
����� Java ��ǹ Interface �����ǹ���١��С���� public ��蹤������ǹ���حҵ��� object �������ö��ͧ �͡�÷ӧҹ�� �����ͧ����� ��ǹ�ҡ���� interface �ж١���ҧ������Ѻ method �ͧ object ������Դ��͡Ѻ object ��� ��� interface �ͧ menhod �����ç���ҧ�ѧ���
-���ͧ͢ method �����͡��֧��÷ӧҹ�ͧ method ���
-Parameters �����㹡��Ѻ��ǹ��������ҧ method
¡������ҧ ��
|
public calculateGrade (int ID) public studentName (string sName) pulic book () |
Implementations
㹷ҧ���� Java ��ǹ�ͧ implement �����ǹ����С���� private ��������ǹ�ͧ���������������ö��Ҷ֧��ҡ��¹͡ ��� object ��� ���������ö�����ǹ����� implement �ͧ������� �µ����ѡ�ͧ Object-Orient progromming ���� object ��� ��èз�Һ��§��� object ��鹷��������ҧ��ҹ�� �¡�á�зӷ����Դ��鹡Ѻ object ��鹨��Դ��ҹ interface ��§�������
������ҧ�ͧ Interface/Implementation 㹷ҧ Java
|
IntSquare |
|
- - - SquareValue : int |
|
+getSquare : int - -calculateSquare : int |
�������ö��¹ code �ҡ class diagram ��ҹ����ѧ���
|
public class intSquare { private int SquareValue ; public int getSquare (int value) { Squarevalue = calculateSquare (value) return SquareValue; } private int calculateSquare (int value){ } }
|
�ѧࡵ���������������������§��ǹ���Ƿ�� object ��� ����ö��Ҷ֧�� ����� interface �ͧ class ���
|
public int getSquare (int value) |
�����ǹ�ͧ implement ������ǹ����С���� private ��������ǹ���١���ҡ��¹͡���� �ѧ���
|
private int SquareValue ; private int calculateSquare (int value) { SquareValue = calculateSquare (value) return SquareValue } |
Inheritance
� procedural programming �зӡ�èѴ�����ǹ�ͧ���������ա�����Ӻ���� ���ӡ�Ѻ�������� (reuse) ��ٻ�ͧ��������� (procedure ���� function ) �����Ҩ�����ö���¡���������¤��駵����ͧ��� ���ҧ�á��� �object oriented programming ���դس���ѵԢ����蹹�� ���ѹ�ѧ�ա���͡Ẻ��������ҡ��鹡��ҡ�� reuse ��� ��������ö㹡�èѴ��áѺ class ��С�����ͧ class ��ҧ� �¤س���ѵ� ������¡��� �Inheritance� ����Сͺ������ѡ��âͧ Superclass/Subclass, Abstraction ��� is-a relationship �����ѡ��õ�ҧ� ����ҹ��Ъ���������͡Ẻ�ç���ҧ�����Ẻobject oriented ������¢�� ��觨������Ƕ֧���� Inheritance ��ͤس���ѵԷ�� class � ˹�� ����ö��ѡɳТͧ attribute ��� method �ͧ�ա class ˹�����÷� �蹹������س����ö create class �������¹������Ӥѭ��������ѹ�ͧattribute ���behavior(method)�ҡclass���
Single Inheritance ���¶֧ ��÷�� object ˹��� ���Ѻ�����ʹ�ѡɳШҡ object ��� ��§ 1 object
Multiple inheritance ���¶֧ ��÷�� object ˹��� ���Ѻ�����ʹ�ѡɳШҡ object ����ҡ���� 1 object
Superclassses & Supclassses
Superclass �繤س���ѵ�˹�觷���Դ�ҡ��� Inheritance �� Superclass ���� Perent class ���¶֧ class � �� �� attrinute ��� method ������� class �������ʹ�س���ѵԨҡ����ͧ ��ǹ subclass ���� child class ���¶֧ class �����س���ѵ��Ҩҡ parent class ��� class dog ��� class cat ���դس���ѵԢͧ eyeColor ��駤���觤س���ѵԹ������ö��ʹ�Ҩҡ class Mammal ���������繵�ͧ���ҧ attribute ��� method ������� ��觨ҡ�ѡɳдѧ����Ƿ��������ö��ػ����� mammal class ��� superclass �ͧ dog ��� cat ���� subclass ���� child class
㹡����ʹ�س���ѵԨҡ Superclass ��ѧ Subclass ��� Subclass ���դس���ѵԢͧ attribute ��� method �ء���ҧ�ҡ Superclass �� object Cat ���� attribute ��� behavior ��������� mammal �� �����ʴ���� �ѹ�� mammal ��Դ˹�� ��й�˨ҡ��鹡��ѧ����ö�����س���ѵԾ����ŧ仴������ͺ觺͡�֧�ѡɳФ�����cat���ᵡ��ҧ�ҡ�ѵ�쪹Դ���
Is-a Relationship
�����ʹ�ѡɳШҡ Superclass ��ѧ Subclass ��蹤�� subclass ������ö�����觷�� superclass ���� ��觨���ҧ�ԧ��պ��շ����� is-a relationship �������� ���� ������ҧ�� �� class ���� 1 class ���� class shape ����� object �����ʹ�س���ѵԨҡ class shape ��� circle object , square object , star object �������ö��¹����� circle is a shape , square is a shape , star is a shape �繵� ����������ѡɳй���� circle , square ���star����ǹ˹�觢ͧ shape �ѧ����ʴ���� cirale , square ��� stre ���Ѻ����� ʹ�Ҩҡ class shape
Composition
Object ���������� object ˹������·���ش ������ object ����Сͺ��鹨ҡ object �������� object �������� �ѡɳй�����¡����� composition �� object computer ��Сͺ仴��� object videa card , object keyboards , object drive , object power supply �繵� ��������ѹ��ͧ object ��ѡɳТͧ composition ��鹨���º��Ѻ��շ����� has-a relationship
Has-a Relationships
�����ͧ�ͧ Inheritance ��ٴ�֧��շ����� Is-a relationship ����� ��ǹ�����ͧ�ͧ composition ������շ����� Has-a relationship ���������� ����������ҧ�� computer Has-a video card , computer Has-a keyboards , computer Has-a drive �繵� ��觤�������ѹ�� ��ѡɳй���ʴ���������� object computer ���� video card , keyboards ��� drive ����ǹ��Сͺ
Page 1>>> Page 2 >>> Page 3 >>> Home
Coppyright2004ByWebmasterAllRightReservedComments