6 object Class String Encapsulation Object Class String
บทท 6 : ตวแปร object, Class String , Encapsulation
เนอหา ตวแปร Object, Class String n Encapsulation n Access modifier n private, n public, n protected n package (none หรอ default) n
class Test_String{ public static void main(String args []){ int x=18; String s=new String("wathinee"); String y; } }
ชนดขอมลทเปน class : String int x=18; String s=new String(“ wathinee”); String y; main } y s x null OX 3432434 18 stack new String wathinee heap
ขอแตกตาง กรณทใชคำสง new ภาษาจาวาจะสรางขอความใหมและจองเนอทในหนวยความจ String s 1= new String(“wathinee”); ำเสมอ String s 2= new String(“wathinee”); String s 3= new String(“Wathinee”); Wathinee wathinee s 3 s 2 s 1 Oxoaaa Oxooee Oxdefe Stack wathinee Heap
String เปน Object ทมตวดำเนนการในการเชอมขอความ String s 1=“Hi !”+ “There”; System. out. print(s 1); String s 2=“This”; String s 3=s 2+ “is a book”; System. out. print(s 3); String s 4=s 2+4; System. out. print(s 4); ภาษาจะแปลงขอมลดงกลาวใหเปน String โดย
การเชอมตอ String (String Concatenation) ตวอยาง การใชเครองหมาย + เปนการตอ String เชน String name 1=“Java”; String name 2=“Programming”; String name 3= name 1+name 2; String name 4= name 1+ “ ”+name 2;
การเชอมตอ String (String Concatenation) ตวอยาง String + ตวเลข String msg=“”Hello; int num= 123; String message =msg +num;
การเชอมตอ String (String Concatenation) ตวอยาง คำสง System. out. println() กบการเชอมตอ String int num=3; System. out. println(“ My Number is : ”+num);
String s 1=“hello”; String s 2=“world”; String s 3=new String(“hell”); String s 4=“hell”; String s 5=“hello”; String s 6=new String(“hell”); คำถาม s 1 == s 5 s 3 == s 4 == s 6 s 3 == s 6 s 1. equals(s 5) s 3. equals(s 4) s 4. equals(s 6) s 3. equals(s 6)
Encapsulation (การหมหอ (
Modifier ในภาษาจาวา 1. Access modifier : เพอกำหนดระดบการเขาใชงาน ไดแก private, public, protected และ package (none หรอ default) 2. non –access modifier ไดแก final , static, abstract , native transient , volatile , synchronized , strictfp
รจกกบ คำตอไปนทเกยวของกบ Encapsulation n Access modifier n private, n public, n protected n package (none หรอ default)
Access modifier เพอกำหนดระดบการเขาใชงานได แก q private, q package (none หรอ default) q protected q public
private Student - id - Name + get. Id() + get. Name(); + set. Name(); Test_Student
Controlling Access to Members of a Class
การนำ access modifier แตละแบบไปใชงาน n class public protected package private X attributle Method
package (none หรอ default)) Student Grad Id Name - Grade get. Id() get. Name(); set. Name(); + print. Grade() + print. Detail. Student() xxxx X Y Xx()
การนำ access modifier แตละแบบไปใชงาน n class public protected package private X attributle Method
Controlling Access to Members of a Class
การนำ access modifier แตละแบบไปใชงาน n class public protected package private X X attributle Method
protected (#) Student Grad #Id # Name - Grade get. Id() get. Name(); set. Name(); + print. Grade() + print. Detail. Student() Xxxx W yx() Yyyy X Y xx()
Controlling Access to Members of a Class
การนำ access modifier แตละแบบไปใชงาน n class public protected package private X X attributle Method
public (+) Student Grad + Id + Name - Grade get. Id() get. Name(); set. Name(); + print. Grade() + print. Detail. Student() Xxxx W yx() Yyyy X Y xx()
Controlling Access to Members of a Class
1. โจทย ใหสรางไฟลตอไปน แลวบนทกไฟล เกบไวท c: sourcejava 1. 1 สราง class Student โดยใหจดเกบไวใน Student student. information id : String name : String gpa : double set. Details(ID : String, N : String, GPA : double) get. Name( ) : String get. GPA( ) : double show. Details() java package
student informatio n
student informatio n test Test_Student
student informatio n test Test_Student 2
Note Dos เขาไปยง directory ของ java แลวพมพ Javac –d. *. java
- Slides: 80