class

Page 1

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

+ďœ‹&$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, "# 24!"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$H1+&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็"H1&!IG่ <!,&!IG่ class GJ6^ Object G่ 6)<516 class <!,62'5OZ> static variable ,$62' -16$61 =่!'. =4H1 static variable [<! object Z< object -'8่4 H1G่ =่!'. =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ďœ‹+ďœ‹%VW6 #&0% /'X*VE<87+%1 Object I ."" ref_nm.dataName; L2,&!ďœŠ14 // main class Car myCar = new Car(); String carColor = myCar.color; int carMfgYear = myCar.mfgYear; * 61 !6Z>ďœ‹+ďœ‹&$I=+&4 Object Lďœ‹&4L ,5#&" 4%ŕšˆ&';++&4 modifier Z-ďœ‹:I6Lďœ‹&4


Object S,&.&! STUďœ‹ method 4<";Y2+%1 Object I ."" ref_nm.methodName([arguments]); L2,&!ďœŠ14 // main class Car myCar = new Car(); String carColor = myCar.getColor();// return String value myCar.doBreak();// void method * 61 !6Z>ďœ‹ method +&4 Object Lďœ‹&4L ,5#&" 4%ŕšˆ&';++&4 modifier Z-ďœ‹:I6Lďœ‹&4


Static S,&.&! STUďœ‹+ďœ‹%VW64!".2ďœ’) static variable I ."" ref_nm.dataName; ClassName.dataName; L2,&!ďœŠ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ďœ‹ method 4!".2ďœ’) static variable I ."" ref_nm.methodName([arguments]); ClassName.methodName([arguments]); L2,&!ďœŠ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 G1'2้' • speed 5O:I6Z>;< q(1OZ' method break G1'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_&!Ir1!Z' method ( >' color, speed) 5O:I6Z>;< q(1OZ' method G่ O61_L2,. ;, G1'2้'


The this Reference : this

61 Z> this 5O:I6Z> $%่& object L&461 &14&)4:84L2, &4 [<!#,'Z-s5O '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-'<H1 )่$L'Z-62"+&$I=L14^ 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 = “Black�; } public Car(String color) { this.color = color; }

Car() { color = “Black�; } Car(String color) { this.color = color; }

}

}

Car myCar = new Car();

Car myCar = new Car(“Blue�);


Encapsulation

object -'8่4^ &15$;<-=1! method &!Ir1!Z' Z'$J$$&4+&4 client - %&TIG่ !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'L14^ ;,r1!Z';$Z- client I [<! client 5O !6Z>" )61 L14^ +&4 object ;<T1' interface G1'2' ้


Encapsulation : Black box Encapsulate field & method Object encapsulated

Client

+ďœ‹&$I= / HJK#$"2L) data / property

(PL)6 $ / 61 6 OG01 behavior / method


Encapsulation : Visibility Modifiers

61 encapsulation Z' Java 5OZ>61 601-'<61 +1:84T1' modifier (visibility modifiers) - public : method .=O variable :I6&14&)4;<516 class &%่'^ - private : method .=O variable :I6&14&)4;<516r1!Z' class G1'2' ้ - protected : method .=O variable :I6&14&)4;<516 class G่&!IZ' package <!,62'- %& class =I6 / -=1' - default : method .=O variable :I6&14&)4;<516 class G่ &!IZ' 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$ .LZ'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 OG01T1' method F8่4$ 2 O rG - mutators / setter ' method G่Z>#01- 2"601-'<- %& =่!'. =4H1L14^ r1!Z' object - accessors / getter ' method G่&1'H1L14^ 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(“Black�); myCar.setBrand(“Honda�); myCar.setYear(2009); System.out.println(“Color of my car is � + myCar.getColor()); System.out.println(“Brand of my car is � + myCar.getBrand()); System.out.println(“Year of my car is � + myCar.getYear()); } }


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.