�����3. ����͡Ẻ������ԧ�ѵ��
Constructors
Constructors ���ǤԴ��������Ѻ�����ӡ����¹����� ���Фس���ѵԢͧ constructors ��鹨����������㹡����¹������Ẻ non Object-Orient �� C ���� Basic �������������� Object-Orient �� Java, C++, Perl ��� ���ԧ���� Constructors ���� method ����� ����� method� �繪������ǡѹ�Ѻ ����� class� ����ѡɳТͧ constructor ����� method �������ա�ä��� (return value) �������ö�ʴ� ������ҧ�ͧ Student class ����� constructor �ѧ�괴�ҹ��ҧ���
|
public class Student { String name ; Public static void main (String arg [ ]) { Student s1= new Student (); Student s2= new Student (); } public Student () { } public Student (String input) { name = input ; } } |
������ҧ Constructor
����� Object �������Ѻ������ҧ����� class ������������ ��蹤�͡���Դ��鹢ͧ constructor �ѧ�괴�ҹ��ҧ���
|
Cabbie myCabbie= new Cabbie |
����� new ��ͤ���觷����㹡�����ҧ���᷹�ͧ Cabbli class ��� new Cabbli()" ���繵��᷹�ͧ Cabbie object ������ҧ Cabbie method �����
Default Constructor
㹡�û����żš�÷ӧҹ���� class ˹�� � �������� Constructor �������� class ������ö�������ͧ�ҡ��� compiler �Ш֧����� constructor ������¡��� default constructor ����ѵ��ѵ� �ѧ�괴�ҹ��ҧ
|
public Cabbie () { Super (); } |
Multiple Constructor
�͡�ҡ default constructor ���١���ҧ�� compiler ���� ��ǹ�ͧ constructor ������ҧ����ͧ������ö���� ����ѧ����ö��ҹ��� parameter �����¤���ա���� ������ҧ�鴴�ҹ��ҧ����ʴ� constructor ������ҧ���������ա�ü�ҹ��� parameter
|
public class Count { int count ; public Count () { Count = 0 ; } } |
���� constructor ����ա�ü�ҹ��� parameter
|
public class Count { int count ; public Count (int number) { count = number ; } } |
Overloading Methods
Overloading ��͡���Թ������ programmer �� � ���� method� ���� method ��ӡѺ � ���� class �·������ method �����������ǡѹ�����ա�ü�ҹ��� parameter ����ҧ�ѹ �ѧ�괴�ҹ��ҧ
|
public void getCab () ; public void getCab (String cabbieName); public void getCab (int numberOfPassengers); |
�� UML 㹡�è��ͧ Classe
����ʴ���ǹ�ͧ constructor ����� UML ����ö¡������ҧ DatabaseReader ����ա����ҹ database 2 �Ը� �ѧ���
-��ҹ���ͧ͢ database ��� ���˹� cursor �����˹�������鹺� database
-��ҹ���ͧ͢ database ��� ���˹����� database �����ҵ�ͧ������ cursor����� � ���˹觹��
�ҡ�Ըշ�� 2 ������¹�� UML ���ʴ������� UML �ͧ Database class ����ѧࡵ������� UML ������ǹ�ͧ constructor 2 ��ǹ �������ʴ���ǹ�ͧ parameter �����繴���
� constuctor ˹�觨����¡�� this ����§�������� ��е��˹觢ͧ this �е�ͧ�������÷Ѵ�á�ͧ constuctor �����ҹ�� �е�ͧ�٨ҡ ����ҹ��
|
DatabaseReader |
|
DBName : String StartPosition : int |
|
+DatabaseReader; +DatabaseReader; +open : void +close : void +go ToFirst : void +goToLast : void +howManyRecords : int +areThereMoreRecords : boolean +possitionRecord : void +getRecord : String +getNextRecord : String |
�괨ҡ UML ��ҧ��
|
public class DataBaseReder { String DBMane; int startPosition; public DataBaseReader (String name) { DBName = name; StartPosition = =pos; }; } |
Superclass �Ѻ Inheritance
��� Inheritance ��� subclass����͡�ѡɳз������ͧ attribute ��� method �Ҩҡ Superclass ���� parentclass ������Ѻ constructor ���١���ҧ��鹨ҡ parent constructor �������¡����ʹẺ Inheritance ���ͧ�ҡ��ǹ�ͧ attribute ������Ѻ��û�С�������� parent contructor �������ö���¡������ constuctor ��� � �������繵�ͧ��ʹ�ѡɳз����� �����ѧ�ҡ������� new �Դ���㹡�����ҧ constructor ���� ������� class �����¹͡���¡��ҹ constructor ���բ�鹵�ѧ���
- - ����á����Դ��鹤�� constructor �����¡����ѧ parent constructor
- - ���� attribute �ͧ class ����С���麹 parent constructor �ء� constructor ������ö��ҹ attribute �ء attribute �ҡ parent constructor
- - ��÷ӧҹ����괢ͧ constructor
����͡Ẻ constructor
����͡Ẻ class ���չ�� ���������� attribute ����������� java ��ǹ�ͧ attribute ��������Ѻ��� compile �����Ҩ��������ҹ������ ����ǹ�ͧ attribute �������ǹ�������㹺�÷Ѵ�á � ��辺������ �ѧ���㹡����������͡Ẻ class �֧������ѧ����ǡѺ����ͧ���㹢ͧ attribute
�ѧ��鹡���͡Ẻ���� �֧������ѧ���ǹ�ͧ attribute ��������Դ����������ʶ����Ҿ��� �Ѻ�к� ������ѧ��ǹ�ͧ attribute ����ա�÷ӧҹ����� constructor ����
Construction ��� method
Construction ��� method ������ѡɳ���ҧ�ҡ method �����㹤���ᵡ��ҧ��鹨Ъ��ᨧ�ѧ���
����������˹�ҷ��
������¢ͧ constructor ��͡�����ҧ object �����͡�ѡɳ��Ҩҡ class
�ѭ�ѡɳ�
Construction ��� method ���ѭ�ѡɳ����ҧ�ѹ 3 �ش��� modifiere ��ä��� ��Ъ��� constructor ���� ����ö�Ӫ�Դ�ͧ modifiere ���Ẻ�ͧ public , prote ���� private ���� method �� constructor ���������ö�� abstract ���� method
����� This
Construction ��� method ���ա���� This ��ҧ�ѹ����ҧ�ҡ method ���� This 㹡����ҧ�֧ object �ͧ class �����Թ�ҹ�����Ѻ method ��ǹ constructor ���� This 㹡����ҧ�֧ constructor ���� class���ǡѹ ��
|
public class Mouse { String name ; Mouse (String input) { name = input ; } Mouse () { This ("Micky"); } public static void main (String arg [ ] ) { Mouse m 1= new Mouse ("disney"); Mouse m 1= new Mouse (); } } |
����� Super
��� method ��� constructor ���� Super 㹡����ҧ�֧ super class �����ѹ �����ҧ�ѹ��� method ���� super �Ѵ��� overloading method � super class �ѧ������ҧ���
|
Class Mammal { void getBirthInfo () { System.out .printfIn("warm blood"); } } class Cat extends Mammal { void getBirthInfo () { System.out.printIn("no from eggs"); System.out.printIn("a mammal is"); super.getBirthInfo () ; } } |
Compiler
��������ҧ class ��� ������� conetructor �������� class ��� �������ҧ��� compile ��� constructor �ж١���ҧ����� �ѵ��ѵ� �ѧ�ʴ���ҹ��ҧ���
|
public class Example () |
�м�ҹ��� compile ��ѧ���
|
public class Example { public void sayHI { System.out.printIn("Hi") } Exaple () { } public class Subclass extends Example { } |
Page1 >>> Page 2 >>> Page3 >>> Home
Coppyright 2004 By Webmaster All Right Reserved Comments