interface

22:10 ---
เครื่องมือติดต่อกับสิ่งที่ไม่มีความสัมพันธ์ต่อกัน
multiple inheritance คือ class สามารถ inherit จากinterfaceได้เพียง constant
แต่interface จะ inherit ทั้ง method และ constant จาก superinterfaces และแต่ละคลาสอาจมี interface เดียวกันได้

รูปแบบ interface


public interface ชื่อ interface extends superinterfaces
{
Interface body
}

public= แสดงว่าอินเตอร์เฟสสามารถใช้คลาสหรือแพ็กเกตใดๆ ก้ได้ ถ้าไม่ระบุ publicไว้  อินเตอร์เฟสใช้ได้เฉพาะคลาสที่อยู่ในอินเตอร์เฟซ

extend= สามารถ extendได้หลายinterface มี,คั่น

interface โดยใช้คลาส

public class ชื่อclass implements ชื่อInterface
{
......
}

ถ้าเราใช้ abstrace class แทน interface จะไม่สามารถ inherit classอื่นได้อีก เพราะมีข้อจำกัดว่า inheritได้class เดียว จริงๆแล้วเรามีclassที่ต้องinheritจาก sub class อยู่แล้ว

public interface MyInterface
{
Public void methodA();
Public void methodB();
}
pubilc class Myclass implement MyInterface
{
Public void methodA();
{
.......
}
Public void methodB();
{
.......
}
}

inheritance

20:19 ---

inheritance

 การลืบทอดลำดับชั้น สามารถนำคลาสที่มีอยู่แล้วหรือนำโปรแกรมที่มีอยู่แล้วมาเขียนใหม่ได้อีก สามารถสร้าคลาสใหม่สืบทอดคลาสที่มีอยู่แล้วได้เป็นการถ่ายทอดคุณสมบัติคลาสหนึ่งไปยังคลาสหนึ่ง  ทำให้คลาสใหม่นำคุณลักษณะคลาสเดิมมาใช้ได้ คลาสที่เป็นต้นแบบเรียกว่าsuperclass หรือbase class คลาสที่สืบทอดคือ subclassหรือchild class
      การสืบทอดทำให้เราสามารถสร้างคลาสที่ใช้คุณลักษณะต่างๆ เหมือนคลาสเดิม แต่เพิ่มคุณลักษณะเพิ่มเติมลงไป
sub class จะ inherit จาก super class ที่เป็นpublic หรือprotected แต่จะไม่ inheritจากsuper class เป็น private
sub class ที่มีชื่อตัวแปรหรือ method เหมือนอยู่ใน super class ของมัน sub class นั้นจะไม่ inherit ต่อตัวแปร หรือ method ของsuper classของมัน sub classนั้นจะไม่ inherit ต่อตัวแปรหรือ method ของsupper classนั้น methodก็เกิดการ Override ส่วนตัวแปรก็จะซ่อน
เช่นจากคลาสStudent ต้องการสร้างคลาสใหม่ให้เก็บคะแนนฟิสิกส์เพิ่มเติม   ซึ่งจะทำให้อ็อบเจ็กต์ของคลาสใหม่ยังมี id,name,gpa เหมือนเดิม มีคะแนนฟิสิกส์เพิ่มขึ้นมา

รูปแบบการ inheritance

public class subclass extends superclass
{
......
}
เช่น
public class Honda extends car
{
.....
}


การสืบทอดจากคลาสแม่สู่ลูกโดยที่คลาสลูกที่สร้างขึ้นมีลักษณธคล้ายคลาสคลาสแม่  จะมีคุณลักษณะพิเศษเฉพาะกว่าเช่น
มีคลาสรูปร่าง2มิติเป็นคลาสแม่
คลาสลูกเป็นคลาส 3เหลี่ยม 4เหลี่ยม วงกลมสืบทอดมาจากคลาส 2 มิติ การสร้างคลาสใช้คำว่า extends เพื่อบอกว่าสืบทอดมาจากคลาสใด


class TwoDShape{ //คลาส2มิติ
double  width;//ตัวแปรเก็บความกว้าง
double height;//ตัวแปรเก็บความสูงของรูปร่าง
void showDim(){ //เมธอดใช้แสดงความกว้างและสูง
System.out.println("Width and height are"+width+"and"+height);
}
}


Triangle สืบทอดมาจาก TwoDShape 

object นั้นสามารถใช้เมธอดและคุณลักษณะของTwoDShapeได้


class Triangle extends TwoDShape{
String style;
double area(){ //เมธอดคำนวณพื้นที่ความกว้างwidth
return width*height/2; //และตัวแปร heightจากคลาสแม่คำนวณ
}
void showStyle(){// เมธอดสำหรับแสดงstyle
System.out.println("Triangle is "+style);
}
}


Shapes.java สร้างobjectของTriangle คลาสของ3เหลี่ยมมา2ตัว แสดงการเรียกใช้เมธอดและคุณลักษณะภายในคลาส

class Shape{
public class void main(String args[]){
Triangle t1 = new Triangle();//สร้างobj 3เหลื่ยม t1
Triangle t2 = new Triangle();//สร้างobj 3เหลื่ยม t2
t1.width=4.0;//กำหนดความกว้างให้3เหลื่ยม t1
t1.height=4.0;//กำหนดความสูงให้3เหลื่ยม t1
t1.style  ="isosceles";//กำหนดstyle ให้3เหลื่ยม t1
t2.width=8.0;//กำหนดความกว้างให้3เหลื่ยม t2
t2.height=12.0;//กำหนดความสูงให้3เหลื่ยม t2
t2.style  ="right";//กำหนดstyle ให้3เหลื่ยม t2

System.out.println("info for t1");
t1.showStyle();เรียกshowStyleที่อยู่ในคลาส Triangle
t1.showDim();เรียกshowDim()ที่อยู่ในคลาส TwoDShape
System.out.println("Area is"+t1.area());// แสดงพื้นที่ของ3เหลี่ยมt1
System.out.println();
System.out.println("info for t2:");
t2.showStyle();
t2.showDim();
System.out.println("Area is"+t2.area());// แสดงพื้นที่ของ3เหลี่ยมt2
}
}

รูปแบบการ inheritance จากคลาสแม่

public class subclass extends superclass
{
......//โครงสร้างในคลาส
}
เช่น
public class Honda extends car
{
.....
}

คลาสลูกสามารถสร้างมากกว่าหนึ่งคลาสก็ได้ 

class Rectangle extends TwoDShape{
boolean isSquare(){//เมธอดทดสอบว่าเป็นสี่เหลี่ยมจริงหรือไม่
if (width==height )return true;//ถ้าความกว้างเท่ากับความสูงให้คืยค่สจริง
return false;//ถ้าความกว้างไม่เท่ากับความสูงให้คืนค่าเท็จ
}
double area (){ //เมธอดคำนวณพื้นที่
return width*height;
}
}

ถ้าคลาสTwoDShapeเป็นprivateคลาสลูกก็ไม่สามารถใช้ตัวแปรหรือสืบทอดได้

class TwoDShape{ //คลาส2มิติ
private double  width;//ตัวแปรเก็บความกว้าง
private double height;//ตัวแปรเก็บความสูงของรูปร่าง
void showDim(){ //เมธอดใช้แสดงความกว้างและสูง
System.out.println("Width and height are"+width+"and"+height);
}
}
//สร้างคลาสลูก
class Triangle extends TwoDShape{
String style;
double area(){ //เมธอดคำนวณพื้นที่ความกว้างwidth
return width*height/2; //และตัวแปร heightจากคลาสแม่คำนวณ
//จะเกิดการผิดพลาดไม่สามารถใช้ตัวแปรwidth,heightได้

}
void showStyle(){// เมธอดสำหรับแสดงstyle
System.out.println("Triangle is "+style);
}
}


ถ้าจะเข้าถึงwidth,height ต้องสร้างmethods ขึ้นมา
คลาสTwoDShape,Triangle เขียนใหม่

class TwoDShape{ //คลาส2มิติ
private double  width;//ตัวแปรเก็บความกว้าง
private double height;//ตัวแปรเก็บความสูงของรูปร่าง
//สร้างเมธอดข้าถึงwidth,height 
double getWidth (){return width;}
double getHeigth (){return heigth;}
void setWidth (double w){width = w;}
void setHeigth (double w){heigth = w;}
void showDim() //เมธอดใช้แสดงความกว้างและสูง
System.out.println("Width and height are"+width+"and"+height);
}
}

และสร้างTriangle โดยแก้ไขเมธอดที่คำนวณพื้นที่ใหม่
class Triangle extends TwoDShape{
String style;
double area(){ //เมธอดคำนวณพื้นที่ความกว้างwidth
return getwidth*getheight/2; //และตัวแปร heightจากคลาสแม่คำนวณ
//เรียกใช้ตัวแปรwidth,height 

}
void showStyle(){// เมธอดสำหรับแสดงstyle
System.out.println("Triangle is "+style);
}
}


การห้ามสืบทอด

final class TwoDShape {
//โครงสร้างในคลาส
}
หากมีการสืบทอดคลาสนี้ถ้าคอมไฟล์จะคอมไพล์ไม่ผ่าน

03:59 ---
\n ขึ้นบรรทัดใหม่
\t  แท็บแนวนอน
\bถอยหลัง1ช่องตัวอักษร
\r เลือนเคอร์เซอร์ไปซ้ายสุด
\\แสดง\
\' แสดง'
\"แสดง"

public class Tabs{
public static void main (String[] args) {
System.out.print("These are our top sellers:\n");
System.out.print("\tComputer grames\n\tCoffee\n");
System.out.println("\tAspirin");
}
}

ผล
These are our top sellers:
        Computer grames
        Coffee
        Aspirin
Press any key to continue . . .
=======================================================================
boolean  เก็บค่าtrue ,false
char        เก็บอักษรแบบUnicode
byte        -128 ถึง+127
short       -32,768ถึง +32,767
int           -2147483648ถึง +2147483647
long        -263ถึง+263 -1
float        -3.4E+38ถึง 3.4E+38
double    -1.7E+308ถึง1.7+308



public class Tabs{
public static void main (String[] args) {

double m= 3.12;
float x=12.f;

System.out.println(m);
System.out.println(x);
}
}
ผล
3.12
12.0
Press any key to continue . . .
======================================================================
public class increment{
public static void main (String[] args) {
int x,y;
x=10;
y=x++;
System.out.println("x="+x+" y= "+y);
y = ++x;
System.out.println("x="+x+" y= "+y);
}
}

ผล

x=11 y= 10
x=12 y= 12
Press any key to continue . . .

Objects and Primitive Data

03:55 ---

Variables


public class PianoKeys {
public static void main (String[] args) {
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
--------------------------------------------------------------------------------------------------------------------------
Compile:
A piano has 88 keys.
--------------------------------------------------------------------------------------------------------------------------
public class PianoKeysErr {
public static void main (String[] args) {
int keys; // declaration without initialization
System.out.println ("A piano has " + keys + " keys.");
}
}
 ก็คือไม่ประกาศint keysว่า=เท่าไหร่ runก็runไม่ได้
--------------------------------------------------------------------------------------------------------------------------
Compile:
C:\myDir\PianoKeysErr.java:6: variable keys might not have
been initialized
System.out.println ("A piano has " + keys + " keys.");
^
1 error
Tool completed with exit code 1
--------------------------------------------------------------------------------------------------------------------------

Assignment

กำหนดค่าตัวแปล ใช้เครื่องหมาย=

total = 5 * 8 - 3; // total is set to 37

ค่าก่อนหน้านี้จะถูกเขียนทับ

total = 10; // total is set to 10
total = 22; // now it is set to 22

สามารถกำหนดเพียงค่าให้กับตัวแปรที่เป็น สอดคล้องกับการประกาศชนิดของตัวแปร
int x = 10; // ok
int y = 10.5; // 10.5 is not an integer

Constants ค่าคงที่

ใช้finalในการประกาศค่าอย่างต่อเนื่อง
final int MIN_HEIGHT = 69;
ชื่อconvention:จะเป็นพิมพ์ใหญ่ทั้งหมด

Primitive Data

ชนิดข้อมูลในjava
แบบจำนวนเต็ม integers:
• byte, short, int, long
ทศนิยม
• float, double
ตัวอักษร
• char
เปรียบเทียบ
• boolean

Numeric Primitive Data

การเก็บข้อมูลตัวเลข
byte, short, int, long, float, double

int เก็บได้มากสุด 2,147,483,647.
floatเก็บได้เพียง7เลข 100,000.875ถ้ามากกว่านั้นใช้ double.

Characters

char เป็น String ได้ถ้ามี
'a'     'X'     '7'     '$'      ','    '\n‘

ความแตกต่างระหว่าง
int x = 1; // x is a number
char y = ‘1’; // y is a character

Boolean

จริงเท็จ
boolean done = false; // ok
boolean x = 1; // won’t compile

Arithmetic Expressions นิพจน์เลขคณิต

+ - * / %
จำนวนกับจำนวนเต็ม=จำนวนเต็ม
ทศนิยมกับทศนิยม=ทศนิยม
จำนวนเต็มกับทศนิยม=ทศนิยม
เช่น

3 * 5 = 15
3 * 5.0 = 15.0

Division and Remainder (Div and Mod)
If both operands to the division operator (/) are integers,
the result is an integer (the fractional part is discarded)
The remainder operator (%) returns the remainder after
dividing the second operand into the first
14 / 3 = 4
8 / 12 =0

14 % 3 =2
8 % 12 =8

Operator Precedence

x = 15 * 4 + 88 % 2 - 11;
result = total + count / max - offset;

• *, /, %
• +, -

What is the order of evaluation in the following expressions?
a + b + c + d + e
1 2 3 4
a + b * c - d / e
3 1 4 2
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1

Casting

int x = 10, y = 4;
float result;
result = (float) x / y; // result is 2.5
result = (float) (x / y); // result is 2.0

Creating Objects

สร้างวัตถุใหม่อ้างอิงวัตถุเก่า
public class Dog {
}
public void bark () {
...
}
...
public void wag () {
...
}

--------------------------------------------------------------------------------------------------------------------------
Dog myDog = new Dog ();

อ้างอิงและวัตถุยังสามารถสร้างแยกต่างหาก:
Dog myDog;
myDog = new Dog ();

Using Objects

public class Dog {
}
public void bark () {
...
}
...
public void wag () {
...
}

The null Reference

อ้างอิงวัตถุ ว่าง
Dog myDog = new Dog ();
myDog = null;
เมื่อตรวจสอบวัตถุไม่มี
if (myDog == null)
System.out.println ("Invalid");

Garbage Collection