การโปรแกรมเชิงวัตถุด้วยภาษา JAVA

29
http://www.thaiall.com/class Page: 1 กกกกกกกกกกกกกกกกกกกกกกกกกกก JAVA กกกกกกกก กกก กกกกกกกก . ปปปปปปปป 18 Introduction

description

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA. Introduction. มหาวิทยาลัยเนชั่น http:// www. nation. ac.th. บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 18 ตุลาคม 2551. ความหมายของภาษาจาวา. - PowerPoint PPT Presentation

Transcript of การโปรแกรมเชิงวัตถุด้วยภาษา JAVA

http://www.thaiall.com/class Page: 1

การโปรแกรมเชิงวั ตถุ�ด้�วัยภาษา JAVA

บุ�รนทร� ร�จจนพั นธุ์�� .ปรั�บปรั�ง 18

ตุ�ลาคม 2551

Introduction

http://www.thaiall.com/class Page: 2

ควัามหมายของภาษาจาวัา ภาษาจาวา (Java Language )ค"อ ภาษาโปรแกรมท#$ถุ%กพั ฒนาโด้ยบุรษ ท ซั นไมโครซัสเต*มส� เป+นภาษาส,าหร บุเข#ยนโปรแกรมท#$สน บุสน�นการเข#ยนโปรแกรมเชิงวั ตถุ� (OOP =Object-Oriented Programming ) โปรแกรมท#$เข#ยนข-.นถุ�าเป+นคลาสก*จะเป+นต�นแบุบุของวั ตถุ�สามารถุม#เมทธุ์อด้ (Method ) ท#$ประกอบุด้�วัยพัฤตกรรม (Behavior )ร%ปพัรรณ (Identity ) และสถุานะ (State)

http://www.thaiall.com/class Page: 3

อะไรต3อมอะไรในจาวัา

ข�อม%ลจาก http://www.thaiall.com/class/indexr.htm

http://www.thaiall.com/class Page: 4

ต วัแปลภาษาจาวัาบรั�ษ�ท Sun Microsystems, Inc.

- 2 2J SE (Java Platform, Standard

:/)- 2 2J EE (Java Platform

, EnterpriseEdition:Enterprise/Server)

- 2 2J ME (Java Platform, Micro

Edition :Mobile/Wireless)

- RRRRRR R RRRRRRRR RRRR( )

- RRRRRRR R RRRRRRR ( )

http://java.sun.com

http://www.thaiall.com/class Page: 5

Language ภาษาคอมพัวัเตอร� (Computer ) ค"อ ภาษาท#$ใชิ� หร"อเก#$ยวัข�องก บุคอมพัวัเตอร� ซั-$งม กใชิ�ร3วัมก บุภาษาโปรแกรม แต3ภาษาคอมพัวัเตอร�น .นม#ควัามหมายท#$กวั�างกวั3า โด้ยไม3จ,าเป+นท#$จะต�องเป+นภาษาโปรแกรม ซั-$งเป+นท#$ร% �ก นวั3าภาษาอย3าง HTML หร"อ SQLไม3ใชิ3ภาษาโปรแกรม แต3ถุ"อวั3าเป+นภาษาคอมพัวัเตอร� ภาษาโปรแกรม (Language) ค"อ วัธุ์#การมาตรฐานในการส"$อสารส,าหร บุแสด้งค,าส $งไปย งคอมพัวัเตอร� ภาษาโปรแกรมก,าหนด้ไวัยากรณ�และการต#ควัามหมายจากโปรแกรมคอมพัวัเตอร�ท#$เข#ยนข-.น ภาษาโปรแกรมท,าให�โปรแกรมเมอร�สามารถุระบุ�อย3างชิ ด้เจนถุ-งข�อม%ลท#$คอมพัวัเตอร�จะท,างาน และวัธุ์#การท#$คอมพัวัเตอร�จะประมวัลผลข�อม%ลเหล3าน .น

http://www.thaiall.com/class Page: 6

เรั��มตุ�นเขี�ยนโปรัแกรัมท�าอย�างไรั•เล"อกภาษา ส,าหร บุน กเร#ยน น กศึ-กษาน .นง3ายท#$จะเล"อก เพัราะ

อาจารย�คอยชิ#.แนะ •หาแหล3งข�อม%ลอ�างองจากห�องสม�ด้ ถุ�าท3านไม3ม#อะไรอย%3ในม"อ คงน-กโครงสร�างภาษาไม3ออก•หาต วัแปลภาษา ท�กภาษาต�องม#ต วัแปลภาษา ม#หลายภาษาท#$ถุ%กสร�างเป+น Free compiler•เข#ยนโปรแกรมต วัแรกท#$ง3าย เชิ3น พัมพั�เลข 5 เป+นต�น •ศึ-กษาการท,าซั,.า และการเล"อกตามเง"$อนไข เชิ3นพัมพั�ส%ตรค%ณ หร"อพัมพั� 1 ถุ-ง 10 เป+นต�น •ตด้ต3อแฟ้;มข�อม%ล เพั"$ออ3านมาแสด้งผล หร"อปร บุปร�งข�อม%ลได้� •เข#ยนเมน% เพั"$อเล"อกกระท,าโปรแกรมตามต วัเล"อก •ท,ารายงานจากการเชิ"$อมแฟ้;มหลายแฟ้;ม โด้ยก,าหนด้ได้�หลายต วัเล"อก •เข#ยนโปรแกรมเพั$มข�อม%ล เชิ3น ซั".อ ขาย ย"ม ค"น หร"อโปรแกรมลงทะเบุ#ยนน กศึ-กษา •สร�างโปรแกรมข-.นมาระบุบุหน-$งให�สมบุ%รณ� (สมบุ%รณ�ค"อ การสนองควัามต�องการของผ%�ใชิ� )

http://www.thaiall.com/class Page: 7

การเข#ยนโปรแกรมโครงสร�าง 1. การัท�างานแบบตุามล�าดั�บ (Sequence) ร%ปแบุบุการเข#ยนโปรแกรมท#$ง3ายท#$ส�ด้ค"อ เข#ยนให�ท,างานจากบุนลงล3าง เข#ยนค,าส $งเป+นบุรรท ด้ และท,าท#ละบุรรท ด้จากบุรรท ด้บุนส�ด้ลงไปจนถุ-งบุรรท ด้ล3างส�ด้ สมมตให�ม#การท,างาน

3 กระบุวันการค"อ อ3านข�อม%ล ค,านวัณ และพัมพั� จะเข#ยนเป+นผ งงาน (Flowchart) ในแบุบุตามล,าด้ บุได้�ตามภาพั

http://www.thaiall.com/class Page: 8

การเข#ยนโปรแกรมโครงสร�าง 2. การัเล"อกกรัะท�าตุามเง"�อนไขี (Decision) การต ด้สนใจ หร"อเล"อกเง"$อนไขค"อ เข#ยนโปรแกรมเพั"$อน,าค3าไปเล"อกกระท,า โด้ยปกตจะม#เหต�การณ�ให�ท,า 2กระบุวันการ ค"อเง"$อนไขเป+นจรงจะกระท,ากระบุวันการหน-$ง และเป+นเท*จจะกระท,าอ#กกระบุวันการหน-$ง แต3ถุ�าซั บุซั�อนมากข-.น จะต�องใชิ�เง"$อนไขหลายชิ .น เชิ3นการต ด้เกรด้น กศึ-กษา เป+นต�น ต วัอย3างผ งงานน#. จะแสด้งผลการเล"อกอย3างง3าย เพั"$อกระท,ากระบุวันการเพั#ยงกระบุวันการเด้#ยวั

http://www.thaiall.com/class Page: 9

การเข#ยนโปรแกรมโครงสร�าง 3 . การัท�าซ้ำ�%า (Repeation or Loop)

การท,ากระบุวันการหน-$งหลายคร .ง โด้ยม#เง"$อนไขในการควับุค�ม หมายถุ-งการท,าซั,.าเป+นหล กการท#$ท,าควัามเข�าใจได้�ยากกวั3า 2 ร%ปแบุบุแรก เพัราะการเข#ยนโปรแกรมแต3ละภาษา จะไม3แสด้งภาพัอย3างชิ ด้เจนเหม"อนการเข#ยนผ งงาน () ผ%�เข#ยนโปรแกรมต�องจนตนาการ ถุ-งร%ปแบุบุการท,างาน และใชิ�ค,าส $งควับุค�มด้�วัยตนเอง ต วัอย3างผ งงานท#$น,ามาแสด้งน#.เป+นการแสด้งค,าส $งท,าซั,.า (dowhile) ซั-$งหมายถุ-งการท,าซั,.าในขณะท#$เป+นจรง และเลกการท,าซั,.าเม"$อเง"$อนไขเป+นเท*จ

http://www.thaiall.com/class Page: 10

แนวค�ดัขีองการัโปรัแกรัมเชิ�งว�ตุถุ� (OOP Concepts)

1. การัปกป(อง (Encapsulation) - การัรัวมกล��มขีองขี�อม*ล และกล��มขีองโปรัแกรัม เพื่"�อการัปกป(อง และเล"อกตุอบสนอง

2 . การัส"บทอดั (Inheritance) - ยอมให้�น�าไปใชิ� ห้รั"อเขี�ยนขี/%นมาทดัแทนขีองเดั�ม

3 . การัพื่�องรั*ป (Polymorphism)

มาจากภาษากรั�ก ห้มายถุ/ง Many Shapes

(Overriding) - Overloading ม�ชิ"�อโปรัแกรัมเดั�ยวก�น แตุ�รัายการัตุ�วแปรั

(Parameter List) ตุ�างก�น - Overriding ม�ชิ"�อโปรัแกรัม และตุ�วแปรัเห้ม"อนก�น เพื่"�อเขี�ยน behavior ขี/%นมาให้ม�

http://www.thaiall.com/class Page: 11

2Java Platform, ( 2 )http://java.sun.com/j2 se/1 .4 /

. ม#ต วัแปรท#$หลายบุรษ ทสร�างข-.น แต3ต วัท#$เป+นมาตรฐานค"อของ sun และ RRRR ก,าล งอย%3ในระหวั3างการพั ฒนาอย3างต3อเน"$อง ผมขอเล"อก 2J SE ของ sun เพัราะใชิ�งานใน RRRR RRRR น,าไปใชิ�ใน linux ก*ม#หล กการเหม"อนก น แต3ถุ�าเป+นของบุรษ ทอ"$น ส3วันใหญ่3ต�องใชิ�ใน WiRRRRR ไม3อย3างย-ด้ตด้ก บุ R R จ-งเล"อกท#$จะใชิ�

2J SE สอนน กศึ-กษา แต3บุรษ ทต3าง ๆ พั ฒนาต วัแปลเป+นระบุบุ RRRRRRRRRRR RRRRRRRR RRR RRRRR(

) ซั-$งใชิ�ง3ายกวั3า ส3วัน 2J SE แม�ไม3ม# IDE ในต วั แต3ม#โปรแกรมหลายต วัท#$น,ามาใชิ�เป+น ediRRR แล�วัเร#ยก 2J SE มาแปล และใชิ�งานได้�สะด้วักข-.น เชิ3น RRRR เป+นต�น

http://www.thaiall.com/class Page: 12

ร%ปแบุบุของ Classclass declaration { variable constructor method nested class or inner class}class a { int a = 0; a() { } void b () { } class c { }}

http://www.thaiall.com/class Page: 13

ชิ"$อเร#ยกเก#$ยวัก บุ ObjectObject Model

State Behavior Identity

General, UML

Value of Attribute Behavior/Action Attribute

OOP

Value of Variable Method Property

Coding in Java

Value of Attribute Method Variable/Attr.

http://www.thaiall.com/class Page: 14

เข#ยนโปรแกรมแรกstart, run, cmdcd\dircd c:\j2sdk1.4.2_03\binc:\j2sdk1.4.2_03\bin>edit x.java

class x { public static void main(String args[]){ System.out.println("hello"); }}

http://www.thaiall.com/class Page: 15

แปลโปรแกรม และประมวัลผลc:\j2sdk1.4.2_03\bin>javac x.javac:\j2sdk1.4.2_03\bin>java xhello

c:\j2sdk1.4.2_03\bin>

http://www.thaiall.com/class Page: 16

ค,าท#$ข#ด้เส�นใต� เปล#$ยนได้�ตามต�องการclass xxx {

public static void main(String args[]){ System.out.println("hello"); }}class aaa { void bbb(String[] ccc){ System.out.println("ddd"); System.out.println(eee("ddd")); } int eee(String fff){ return ("xxx"); }}

http://www.thaiall.com/class Page: 17

Java Applet//<applet code=x.class width=200 height=200></applet>

//DOS>appletviewer x.java

import java.awt.Graphics;

public class x extends

java.applet.applet {

public void paint(Graphics g) {

g.drawString("test",10,20);

}

}

http://www.thaiall.com/class Page: 18

RRRR RRRRRRRRRRR// javac y.java// java yimport java.lang.*;import java.awt.*;import java.awt.event.*;public class y implements ActionListener{Frame fs = new Frame("header");Button bexit = new Button("exit");public static void main(String args[]) { y s = new y(); s.init();}public void init() { fs.setSize(200,100); fs.setLayout(null); bexit.setBounds(10,50,50,20); fs.add(bexit); bexit.addActionListener(this); fs.show();}public void actionPerformed(ActionEvent a) { System.exit(0);}}

http://www.thaiall.com/class Page: 19

RRRRRRRRR RRRRRR Product ID - - 310035CX

Price 20000$ .

The Sun Certified Programmer for Java 2 Platf

14orm . certification exam i s f or pr ogr ammer s exper i e

nced i n usi ng t he RRRRR RRRRRR and st r uct ur e of the Java

RRRRRRRRR RRRR. ification is available for the Ja

RRRRRRRR R2 .

http://www.sun.com/training/catalog/- -courses/CX 310 035.xml

http://www.thaiall.com/class Page: 20

RRRRRRRRR RRRRRRTopics

1. Declaration .. 2 . Flow Control 3. Garbage Collection 4 . Language

Fundamentals 5 . Operators &

Assi gnment s 6 . Overloading,

RRRRRRRRRR 7 . Threads 8 . . ...java lang 9 . Collections Framework

http://www.thaiall.com/class Page: 21

Certified Topics Section 1: Declarations and Access Control

• Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initi

alization.• Declare classes, nested classes, methods, instance variables, static v

ariables and automatic (method local) variables making appropriate us e of all permitted modifiers (such as public, final, static, abstract, etc.).

State the significance of each of these modifiers both singly and in co mbination and state the effect of package relationships on declared ite

ms qualified by these modifiers.• For a given class, determine if a default constructor will be created an

d if so state the prototype of that constructor.• Identify legal return types for any method given the declarations of al l related methods in this or parent classes.

http://www.thaiall.com/class Page: 22

Certified Topics 2Section : Flow control, Assertions, and Exception Handling

• Write code using if and switch statements and identify legal argumen t types for these statements.

• Write code using all forms of loops including labeled and unlabeled, u se of break and continue, and state the values taken by loop counter v

ariables during and after loop execution.• Write code that makes proper use of exceptions and exception handli

ng clauses (try, catch, finally) and declares methods and overriding me thods that throw exceptions.

• Recognize the effect of an exception arising at a specified point in a c ode fragment. Note: The exception may be a runtime exception, a che

cked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination).

• Write code that makes proper use of assertions, and distinguish appr opriate from inappropriate uses of assertions.

• Identify correct statements about the assertion mechanism.

http://www.thaiall.com/class Page: 23

Certified Topics Section 3: Garbage Collection

• State the behavior that is guaranteed by the garb age collection system.

• Write code that explicitly makes objects eligible f or garbage collection.

• Recognize the point in a piece of source code at which an object becomes eligible for garbage colle ction.

http://www.thaiall.com/class Page: 24

Certified Topics Section 4: Language Fundamentals

• Identify correctly constructed package declarations, import statemen ts, class declarations (of all forms including inner classes) interface dec

larations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers.

• Identify classes that correctly implement an interface where that inte rface is either java.lang.Runnable or a fully specified interface in the q

uestion.• State the correspondence between index values in the argument arra

y passed to a main method and command line arguments.• Identify all Java programming language keywords. Note: There will no t be any questions regarding esoteric distinctions between keywords a

nd manifest constants.• State the effect of using a variable or array element of any kind when

no explicit assignment has been made to it.• State the range of all primitive formats, data types and declare literal

values for String and all primitive types using all permitted formats bas es and representations.

http://www.thaiall.com/class Page: 25

Certified Topics Section 5: Operators and Assignments

• Determine the result of applying any operator (including assignment operators and instance of) to operands of any t

ype class scope or accessibility or any combination of thes e.

• Determine the result of applying the boolean equals (Obje ct) method to objects of any combination of the classes jav

a.lang.String, java.lang.Boolean and java.lang.Object.• In an expression involving the operators &, |, &&, || and v

ariables of known values state which operands are evaluat ed and the value of the expression.

• Determine the effect upon objects and primitive values of passing variables into methods and performing assignmen

ts or other modifying operations in that method.

http://www.thaiall.com/class Page: 26

Certified Topics Section 6: Overloading, Overriding, Runtime

Type and Object Orientation • State the benefits of encapsulation in object orie

nted design and write code that implements tightl y encapsulated classes and the relationships "is a"

and "has a".• Write code to invoke overridden or overloaded m

ethods and parental or overloaded constructors; a nd describe the effect of invoking these methods.

• Write code to construct instances of any concrete class including normal top level classes and neste

d classes.

http://www.thaiall.com/class Page: 27

Certified Topics Section 7: Threads

• Write code to define, instantiate and start new th reads using both java.lang.Thread and java.lang.R

unnable.• Recognize conditions that might prevent a thread

from executing.• Write code using synchronized wait, notify and n

otifyAll to protect against concurrent access probl ems and to communicate between threads.

• Define the interaction among threads and object locks when executing synchronized wait, notify or

notifyAll.

http://www.thaiall.com/class Page: 28

Certified Topics 8Section : Fundamental Classes in the java.lang Package

• Write code using the following methods of the java.lang.M ath class: abs, ceil, floor, max, min, random, round, sin, cos

, tan, sqrt.• Describe the significance of the immutability of String obj

ects.• Describe the significance of wrapper classes, including m

aking appropriate selections in the wrapper classes to suit specified behavior requirements, stating the result of exec

uting a fragment of code that includes an instance of one o f the wrapper classes, and writing code using the following

methods of the wrapper classes (e.g., Integer, Double, etc. ): doubleValue o floatValue o intValue o longValue o parse

Xxx o getXxx o toString o toHexString

http://www.thaiall.com/class Page: 29

Certified Topics Section 9: The Collections Framework

• Make appropriate selection of collection classes/i nterfaces to suit specified behavior requirements.

• Distinguish between correct and incorrect imple mentations of hashcode methods.