Classes -
Class Object / Instance This Reference Constructors Encapsulation Mutators & Accessors
Watcharin puangplia puangplia:: 26/ 26/12/ 12/53
Class
!" #$%&'()$( +!, - %& .$()$(#01- 2"# 14 Object Object # 14$1516 Class !6,1 ' instance +&4 Class Class -'8่4 Class #1$1 :'01; # 14 Object ;<-=1! Object >' Class >%่& Student &15# 14 ' Object >%่& s1, s2, …, sN ;< F8่4 ' Object G่$1516 Class <!,62'H%& Student Class
Student -Name -Major
Obj
Student A
Obj
Obj
Student D
Student C
Obj
Student B
Class
+ď&#x153;&#x2039;&$I= - %& HJK#$"2L) data - %& property - %& variable 61 6 OG01 - %& (PL)6 $ method - %& behavior
int x, y, z; char a, b, c; String nm, lastNm;
Class Car
+๏ &$I= - %& HJK#$"2L) : data !เน -๏ &, J๏ ', #, +'1< H %เน &4!'L๏ , ๏ Gเน T=)L, 501','G' เน 2เน 4, 1H1 U=U 61 6 OG01 - %& (PL)6 $ : behavior <)'-'๏ 1, :&!-=24, " H, =เน !,F๏ 1!, =เน !,+,1, ๏ < OLI, "".L , ๏ <;X-'๏ 1, ๏ <6 O56 :, #L1 ๏ G H %เน &4 U=U
Class Car Car ! %, &'), *!, +),-./&0"%1, "# 24!"5678, 9:,);)4!")<"1, &,/,
<)'-'1 :&!-=24 " H #L1 G H %่&4 "".L
Class public class Person { private long idCard; private String nm, lastNm; public void setIdCard(long idCard) public void setNm(String nm) public void setLastNm(String lastNm) public long getIdCard() public String getNm() public String getLastNm() }
data
method
Object or Instance
',2L:JZ'G14[ .6 $G่:I6# 14$1516 Class '#)่4G่Z>.#<4 - %& G01Z- Class 6)< ' I \ $ Car
- !"#% - &') - *! - +),-./&0"%1 +.-7)#), +D% #6<1 +.E&/
Car1 Car 1
Car2 Car 2
Car3 Car 3
- Toyota - Corolla - -:, - 1600 +.-7)#), +D% #6<1 +.E&/
- Honda - Civic - .4, - 1800 +.-7)#), +D% #6<1 +.E&/
- Nissan - Cefiro - E&%)K - 2000 +.-7)#), +D% #6<1 +.E&/
Instance Variables
8<;M2&+%1 Instance L2,. !่-& J' # +'1< H %่&4 Z' class Car !6,1 Instance variable ( 1O,1.L=O Instance - %& Object G่ 6)<516 class Car 5O$H1+&4L2,. <246=1, '+&4L2, &4
Car - !"#% - &') - *! - +),-./&0"%1
Car1 Car 1 - Toyota - Corolla - -:, - 1600
Car2 Car 2 - Honda - Civic - .4, - 1800
Car3 Car 3 - Nissan - Cefiro - E&%)K - 2000
Static Variables
8<;M2&+%1 Class L2,. count Z' class Car !6,1 class variable 6L).L=O Object 5O$L2,. '+&4L2, &4 .L:1L2,. G่ ' class variable 5O 6็"H1&!IG่ <!,&!IG่ class GJ6^ Object G่ 6)<516 class <!,62'5OZ> static variable ,$62' -16$61 =่!'. =4H1 static variable [<! object Z< object -'8่4 H1G่ =่!'. =4<246=1,5O$T=L& object GJ6^ object
Static Variables 8<;M2&+%1 Class Car - count = 0 - !"#% - &') - *! - +),-./&0"%1
?
Car1 Car 1
Car2 Car 2
Car3 Car 3
- Toyota - Corolla - -:, - 1600
- Honda - Civic - .4, - 1800
- Nissan - Cefiro - E&%)K - 2000
setCount(1) setCount(1 getCount()
setCount(2 setCount(2) getCount()
setCount(3 setCount(3) getCount()
- Object
61 O61_ [<modifier>] <ClassName> ref_nm; L2,&!14 public Car myCar; 61 # 14 // main class ref_nm = new <ClassName>([arguments]); L2,&!14 myCar = new Car(); - %& Car myCar = new Car();
Object S,&.&! STUď&#x153;&#x2039;+ď&#x153;&#x2039;%VW6 #&0% /'X*VE<87+%1 Object I ."" ref_nm.dataName; L2,&!ď&#x153;&#x160;14 // main class Car myCar = new Car(); String carColor = myCar.color; int carMfgYear = myCar.mfgYear; * 61 !6Z>ď&#x153;&#x2039;+ď&#x153;&#x2039;&$I=+&4 Object Lď&#x153;&#x2039;&4L ,5#&" 4%ŕš&#x2C6;&';++&4 modifier Z-ď&#x153;&#x2039;:I6Lď&#x153;&#x2039;&4
Object S,&.&! STUď&#x153;&#x2039; method 4<";Y2+%1 Object I ."" ref_nm.methodName([arguments]); L2,&!ď&#x153;&#x160;14 // main class Car myCar = new Car(); String carColor = myCar.getColor();// return String value myCar.doBreak();// void method * 61 !6Z>ď&#x153;&#x2039; method +&4 Object Lď&#x153;&#x2039;&4L ,5#&" 4%ŕš&#x2C6;&';++&4 modifier Z-ď&#x153;&#x2039;:I6Lď&#x153;&#x2039;&4
Static S,&.&! STUď&#x153;&#x2039;+ď&#x153;&#x2039;%VW64!".2ď&#x153;&#x2019;) static variable I ."" ref_nm.dataName; ClassName.dataName; L2,&!ď&#x153;&#x160;14 // main class Car myCar = new Car(); String carColor = myCar.color;// call through ref_nm - %& String carColor = Car.color;// call through className
Static S,&.&! STUď&#x153;&#x2039; method 4!".2ď&#x153;&#x2019;) static variable I ."" ref_nm.methodName([arguments]); ClassName.methodName([arguments]); L2,&!ď&#x153;&#x160;14 // main class Car myCar = new Car(); String carColor = myCar.getCount();// call through ref_nm - %& String carColor = Car.getCount();// call through className
: Scope of variable Car private static int count; private String carColor; method setColor(String color) String color
method break() int speed;
count ' static variable carColor ' instance variable GJ6 method #1$1 : +1:84 count .=O carColor ;< color .=O speed ' local variable • color 5O:I6Z>;< q(1OZ' method setColor G1'2้' • speed 5O:I6Z>;< q(1OZ' method break G1'2้'
: Scope of variable
+&" +L+&4L2,. -$1!:84(%้'G่Z'[ .6 $G่L2,. '2้'^ 5O:I6Z> - %& :I6&14&)4:84;< L2,. G่:I6 O61_Z' O<2" class ( >' count, carColor) 5O:I6Z>;<516GJ6 method Z' class '2้' L2,. G่:I6 O61_&!Ir1!Z' method ( >' color, speed) 5O:I6Z>;< q(1OZ' method G่ O61_L2,. ;, G1'2้'
The this Reference : this
61 Z> this 5O:I6Z> $%่& object L&461 &14&)4:84L2, &4 [<!#,'Z-s5O '61 Z>Z' method +&4 object Z'6 K G่L2,. Z' method $>%่&F้0162"L2,. +&4 object (instance variable) public class Car { private String color; public void setColor(String color) { this.color = color; } }
Constructors
'#)่4G่Z>601-'<H1 )่$L'Z-62"+&$I=L14^ r1!Z' object +KOG่# 14.L=O object +8้'$1 ' method G่$>%่& -$%&'62" class F8่45O:I6 !6Z- 6 OG01 #$& $%่&$61 # 14 object new ClassName(); --> constructor :I6 !6Z-G0141' [<! 6L) constructor L&4:I6601-'< modifier ' public (%่&Z-#1$1 ::I6 !6;<516 class &%่'^ .L Java 5O:%&,1 constructor ' public [<! default 584;$ 501 'L&4Z#6็;< -16;$601-'< constructor .=, [<! default [ .6 $ 5O601-'< constructor Z- ' empty constructor
Constructor public class Car { private String color;
public class Car { private String color;
public Car() { color = â&#x20AC;&#x153;Blackâ&#x20AC;?; } public Car(String color) { this.color = color; }
Car() { color = â&#x20AC;&#x153;Blackâ&#x20AC;?; } Car(String color) { this.color = color; }
}
}
Car myCar = new Car();
Car myCar = new Car(â&#x20AC;&#x153;Blueâ&#x20AC;?);
Encapsulation
object -'8่4^ &15$;<-=1! method &!Ir1!Z' Z'$J$$&4+&4 client - %&TIG่ !6Z> object '2้' method <246=1, !" #$%&'" )61 G่ object L !$;,Z-Z>41' Z'=26yKO'้&156=1,;<,1 object :I6 encapsulate method - %&" )61 <246=1, 5O !6,1 ' interface ; #I object Object G่:I6 encapsulate ;,5O$#r1(H=1!62"6=&4<01 G่F&' 1!=O &!<61 G0141'L14^ ;,r1!Z';$Z- client I [<! client 5O !6Z>" )61 L14^ +&4 object ;<T1' interface G1'2' ้
Encapsulation : Black box Encapsulate field & method Object encapsulated
Client
+ď&#x153;&#x2039;&$I= / HJK#$"2L) data / property
(PL)6 $ / 61 6 OG01 behavior / method
Encapsulation : Visibility Modifiers
61 encapsulation Z' Java 5OZ>61 601-'<61 +1:84T1' modifier (visibility modifiers) - public : method .=O variable :I6&14&)4;<516 class &%่'^ - private : method .=O variable :I6&14&)4;<516r1!Z' class G1'2' ้ - protected : method .=O variable :I6&14&)4;<516 class G่&!IZ' package <!,62'- %& class =I6 / -=1' - default : method .=O variable :I6&14&)4;<516 class G่ &!IZ' package <!,62'
Encapsulation : Visibility Modifiers .+,D^1Y-9,S
public
protected default private
Class <!,62'
Class &%่'Z' package <!,62'
X
Class =I6 / -=1'
X
X
Class &%่'^
X
X
X
Mutators and Accessors
.$,15O#1$1 :601-'<Z-#$1>)6Z' class Z<^ ' public - %& private ;<6็L1$ .LZ'61 &&6."" class '2้' H, Z- data G2้4-$< ' private (%่& &462'61 +1:84;<[<!L 4 516r1!'&6 61 +1:84#$1>)6+&4 object 516r1!'&6 H, 6 OG01T1' method F8่4$ 2 O rG - mutators / setter ' method G่Z>#01- 2"601-'<- %& =่!'. =4H1L14^ r1!Z' object - accessors / getter ' method G่&1'H1L14^ r1!Z' object
Mutators and Accessors public class Car { private String color; public void setColor(String color) { // setter method this.color = color; } public String getColor() { // getter method return color; } }
Program public class Car { private String color, brand; private int year; public void setColor(String color) { this.color = color; } public void setBrand(String brand) { this.brand = brand;} public void setYear(int year) { this.year = year; } public String getColor() { return color; } public String getBrand() { return brand; } public int getYear() { return year; } }
Program public class TestCar { public static void main(String[] args) { Car myCar = new Car(); myCar.setColor(â&#x20AC;&#x153;Blackâ&#x20AC;?); myCar.setBrand(â&#x20AC;&#x153;Hondaâ&#x20AC;?); myCar.setYear(2009); System.out.println(â&#x20AC;&#x153;Color of my car is â&#x20AC;? + myCar.getColor()); System.out.println(â&#x20AC;&#x153;Brand of my car is â&#x20AC;? + myCar.getBrand()); System.out.println(â&#x20AC;&#x153;Year of my car is â&#x20AC;? + myCar.getYear()); } }