Java for Beginners – 2. OOP with Java.pdfการเข ยนโปรแกรมเช งว...

Preview:

Citation preview

เนอหาทตองเรยนร

โครงสรางภาษาจาวา

ตวแปรและขอมลชนดพนฐาน

หลกการประมวลผล

การควบคมประโยคค าสง

คลาสและออบเจกตคออะไร

Interfaces และ Nested Classes

คณสมบตเชงวตถ

information hiding และ Encapsulation

Inheritance และ Polymorphism

โครงสรางภาษาจาวา

หลกการประมวลผลภาษาจาวาดวยหนาตางอดเตอร (Text Editor)

จาก นน จงท าการแปลภาษาด วยคอมไพเลอร(Compiler)ซงจะเปนการแปลภาษาแบบทละชดค าสงเปนภาษาเครองหรอทเรยกวา ออบเจกตโคดหรอจะได ไฟลเปนนามสกล .class

ซงไฟลนจะท าใหเครองคอมพวเตอรหรอซพยสามารถท างานตามค าสงทเขยนไวได แสดงขนตอนการท างานดงภาพ

ER 314 การเขยนโปรแกรมคอมพวเตอรเชงวตถ

การประกาศตวแปรในภาษาจาวา

ขอมลชนดพนฐานในภาษาจาวา

การควบคมประโยคค าสง

การควบคมประโยคค าสง (Control Statement)หมายถง การเขยนชดค าสงเพอก าหนดโปรแกรมให ท างานตามทศทางทผใชตองการ สามารถแบงออกเปน2 ประเภทหลกๆ คอ

1. การควบคมประโยคค าสงแบบใหเลอกท า(Selection Control Statement) หรอ แบบใหตดสนใจท า (Decision Control Statement) จะประกอบดวยค าสง if – else และ switch – case

2. การควบคมประโยคค าสงแบบท าซ า(Repeating Control Statement) หรอ แบบใหมการวนซ าค าสง (Loop Control Statement) จะประกอบดวยค าสง for, while และ do-while

อยางไรกตามในสรางโปรแกรมท างานจรงอาจจ าเปนตองใชทงสองประเภทรวมกน ซงจะเรยก การ

ER 314 การเขยนโปรแกรมคอมพวเตอรเชงวตถ

คลาส และ ออบเจกต

การสรางคลาส<Access-Modifier> class MyClass {

// field, constructor, and method declarations}

การสรางออบเจกตหรอตวแทนใหกบคลาส

MyClass instance = new MyClass(<constructor params>);

CE 315 : การเขยนภาษาจาวาเพอการศกษา

การก าหนดการเขาถงคลาสและออบเจกต

Example:

public class Person {

private String name;

protected java.util.Date birthDate;

String id; // default accessibility = package

public Person() {}

}

ประเภทของการก าหนดการเขาถง

– public – (default) = “package” **

– protected * – private

* protected is also accessible by package

** called also “package-private” or “package-friendly”

การเขาถงโดยไมตองผานตวแทน

Example:

public class Widget {

static private int counter;

static public getCounter() {

return counter;

}

}

int number = Widget.getCounter();

Called sometimes “class variable”

as opposed to “instance variable”

การใช คยเวรด this

Example:

public class Point {

private int x, y;

public Point(int x, int y) {

this.x = x;

this.y = y;

}

}

The ‘this’ keyword is also used to call another constructor of

the same class – we will see that later

การก าหนดคาคงทใหกบตวแปร

Example:

public class Thingy {

public final static doodad = 6; // constant

public final id; // constant variable

public Thingy(int id) {this.id = id;} // OK

// public set(int id) {this.id = id;} // error!

}

คอนสตรทเทอร (Constructors)

คอนสตรทเทอร คอ เมธอดทชอเดยวกบคลาส

จะท างานเองอตโนมต เมอถกเขยน ตามหลง คยเวรค new

นยมใชก าหนดคาเร มตนใหกบตวแปรตางๆในโปรแกรม

แมไมมประกาศคอนสตรทเทอรไวในชดค าส ง แตโปรแกรมจะสราง คอ

นทรสเตอรแบบไมมการรบคาไวใหอตโนมต

สามารถรองรบการท า โอเวอรโหลดดงได (Overloading Method)

การสรางคอนสตรทเทอร

Example 1:

public class Person {

String name = ""; // fields can be initialized!

Date birthDate = new Date();

public Person() {} // empty constructor

public Person(String name, Date birthDate) {

this(name); // must be first instruction

this.birthDate = birthDate;

}

public Person(String name) {

this.name = name;

}

}

Example 2:

public class Person {

String name = "";

Date birthDate = new Date();

public Person(String name, Date birthDate) {

this.name = name;

this.birthDate = birthDate;

}

}

Person p; // OK

p = new Person(); // not good – compilation error

การสรางคอนสตรทเทอร

เดสคอนสตรคเตอร (Deconstrutor)

คณสมบตของการเขยนโปรแกรมเชงวตถ การปกปดและซอนเรนขอมลขาวสาร (Information Hiding) การท าหบหอขอมล (Encapsulation) การสบทอดขอมล (Inheritance) การพองรป (Polymorphism)

ER 314 การเขยนโปรแกรมคอมพวเตอรเชงวตถ

การสบทอด (Inheritance)เปนการสบทอดคณลกษณะและพฤตกรรม จากคลาสหลก (Super class) สคลาสยอย (Sub class) ซงคณลกษณะและพฤตกรรมของคลาสหลกจะตองไมก าหนดการเขาถงแบบสวนตว (Private)

ER 314 การเขยนโปรแกรมคอมพวเตอรเชงวตถ

การสรางการสบทอด

Example 1:

public class Person {

private String name;

public Person(String name) {

this.name = name;

}

// Override toString in class Object

public String toString() {

return name;

}

}

CE 315 : การเขยนภาษาจาวาเพอการศกษา

Example 1 (cont’):

public class Employee extends Person {

private Employee manager;

public Employee(String name, Employee manager) {

super(name); // must be first

this.manager = manager;

}

// Override toString in class Person

public String toString() {

return super.toString() +

(manager!=null? ", reporting to: " + manager :

" - I'm the big boss!");

}

}

การสรางการสบทอด

Example 2:

abstract public class Shape {

// private Color line = Color.Black;

// private Color fill = Color.White;

public Shape() {}

/* public Shape(Color line, Color fill) {

this.line = line;

this.fill = fill;

} */

abstract public void draw();

abstract public boolean isPointInside(Point p);

}

การสรางการสบทอด

CE 315 : การเขยนภาษาจาวาเพอการศกษา

Example 2 (cont’):

public class Circle extends Shape {

private Point center;

private double radius;

public Circle(Point center, double radius) {

this.center = center; this.radius = radius;

}

public void draw() {…} // use Graphics or Graphics2d

public boolean isPointInside(Point p) {

return (p.distance(center) < radius);

}

}

การสรางการสบทอด

Super and This Keyword

ER 314 การเขยนโปรแกรมคอมพวเตอรเชงวตถ