Watcharin Puangplia 29/04/2011
Object attribute
method
Object ! " Object #$ % Method
Class
Package
Subclass
public
Y
Y
Y
Y
protected
Y
Y
Y
N
no modifier(default)
Y
Y
N
N
private
Y
N
N
N
Modifier
class Rectangle{ private double length; private double width; public void setLength(double len){ length = len; } public void setWidth(double w){ width = w; } public double getLength(){ return length; } public double getWidth(){ return width; } public double getArea(){ return length * width; } }
// # CD Student
public class Student{ private String id;
//!E$ F GH id
private String name;
//!E$ F GH CD
private double gpa;
//!E$ F GH GPA
public void setDetails(String ID,String n,double GPA){
// EH E
id = ID;
// GH E D% I ! J ID
name = n;
// GH CD D% I ! J n
gpa = GPA;
// GH D% I ! J GPA
} public void showDetails(){
// E
System.out.println("ID : "+id); System.out.println("Name :"+name); System.out.println("GPA : "+gpa); } public static void main(String args[]){
// E E
Student sc1 = new Student();
// H G !J EH E #
Student sc2 = new Student();
// H G !J EH E # D
sc1.setDetails("12123","TEERAWAT",3.81);
// E #
sc1.showDetails();
// E #
System.out.println();
} }
sc2.setDetails("34123","TANS",2.5);
// E # D
sc2.showDetails();
// E # D
Overloading Method #C
Class Method D CD C E ! D ! ! E K EH $ I ! J D EH public void setData(){ id = null; name = null; gpa = 0; } public void setData(String id_in,String name_in){ id = id_in; name = name_in; gpa = 0; }
Overloading #C method !EK ! 2 method K LF class $ E M D CD method (method name) E K C E ! parameter list ! ! E (signature ! E )
Overloading method #C D CD $ E ! !E$ F ! E C $ J $ !JL E I H CD $ E ! ! $ # !E$ F # !E$ F FN Integer G D FN String G D
parameter list signature Compiler argument ! "# ! signature $
! power % " & Error x = power(2); x = power(2,3); x = power(); x = power(1,2,3) x = power("2",3)
// // // // //
x "' 4 x "' 5 I Love You Error Error
public class Student { private String id; private String name; private double gpa; public void setData(){ id = null; name = null; gpa = 0; } public void setData(String id_in,String name_in){ id = id_in; name = name_in; gpa = 0; } public void setData(String id_in,String name_in,double gpa_in){ id = id_in; name = name_in; gpa = gpa_in; } public String toString(){ return " : " +id +"\n : "+name+"\nGPA : "+gpa; } }
public class StudentDemo { public static void main(String[] args) { Student somsri,pranee,winai ; somsri = new Student(); pranee = new Student(); winai = new Student(); somsri.setData(); pranee.setData("001","Pranee"); winai.setData("002", "Winai", 3.4); System.out.println(somsri.toString()+"\n"); System.out.println(pranee.toString()+"\n"); System.out.println(winai.toString()+"\n"); } }
Constructors #C Method D CD Object M I # EK $ E K M FN Method D CD $ EH Class ! ! L return type # D K Java default Constructors #C Constructors D CD $ EH Class ! L EH# J $ !J L " Class Student{ Student(){}; }
public class Rectangle { private double length; private double width; public Rectangle(double len, double w){ length = len; width = w; } // }
Class Rectangle
public class ConstructorDemo { public static void main(String[] args) { Rectangle box = new Rectangle(8.0, 12.0); System.out.println("The box's length is " +box.getLength()); System.out.println("The box's width is " +box.getWidth()); System.out.println("The box's area is " +box.getArea()); } }
Class Constructor $ 1 !E$ M ! !E$ D ! ! E K EH $ I ! J D EH public Rectangle(){ length = 0.0 width = 0.0 } public Rectangle(double len,double w){ length = len; width = w; }
public class Rectangle { private double length; private double width; public Rectangle(){ length = 0.0; width = 0.0; } public Rectangle(double len, double w) length = len; width = w; } // Class Rectangle }
{
public class ConstructorDemo2 { public static void main(String[] args) { Rectangle boxA = new Rectangle(); Rectangle boxB = new Rectangle(3.0,2.0); System.out.println("The box A area is " + boxA.getArea()); System.out.println("The box B area is " + boxB.getArea()); } }
ICK P (primitive data type) int , long , double Q Q # M #E # D GH !E$ F
(Copy Value) int a , int b; a = 1; b = a;
a
b
a
1
b
a
1
b
1
# a #E LF D b
HH (reference data type) FN D GH Address !E$ F Object !E$ F Array Q Q # M #E D (Copy reference) C #E Address ED Rectangle recA , recB;
recA
recB
Address
Address
recA = new Rectangle(); recB = new Rectangle(); recB = recA
Rectangle Object
Rectangle Object
MF HH # Object # D LF FN people $ !J # D L Class People FN Class EH GH # Class Car EH GH !JM Class Car GH M Object Car LF FN Car
People name address People() setName(String) setAddress(String) getName() getAddress()
carID owner Car() setCarID(String) setOwner(People) getCarID() getOwnerName() toString()
public class People { private String name; private String address; public People(String n,String a){ name = n; address = a; } public void setName(String n){name = n;} public void setAddress(String a){address = a;} public String getName(){return name;} public String getAddress(){return address;} }
public class Car { private String carID; private People owner; public Car(String carIDIn,People ownerIn){ carID = carIDIn; owner = ownerIn; } public void setCarID(String carIDIn){carID = carIDIn;} public void setOwner(People ownerIn){owner = ownerIn;} public String getOwner(){return owner.getName();} public String toString(){return "CarID : "+carID + "\nOwner : "+owner.getName() + "\nAddress: "+owner.getAddress();} }
public class public People People
OwnerCar { static void main(String []args){ winai = new People("Winai","Nakhonpathom"); manee = new People("Manee","Bangkok");
Car car1 = new Car(" 1539",winai); Car car2 = new Car(" 5678",manee); System.out.println(car1.toString()); System.out.println(car2.toString()); } }
4. 4.1 class Employee / !" class String empNm : #$ % & double salary : ' ! ' $ & Setter-Getter Method '%$ # & () * " ( &" & Encapsulation field
// ( // ) ' Setter-Getter Method
4.2 class TestEmployee (main class) ()' + class Object " Employee ", 2 Objects ()& * & object - Setter Method Object 1 & #$ " Object ' . emp1 - & " empNm ' . bSomchaic - & " salary ' & 3,000 Object 2 & #$ " Object ' . emp2 - & " empNm ' . bManeec - & " salary ' & 5,000 4.3 + class TestEmployee (main class) " 0(" Object () Object " 0(" / !
5. 5.1 class Animal / !" class String name : #$ 1 int leg : " String eat : & Setter-Getter Method '%$ # & () * " ( &" & Encapsulation field
5.2 class ShowAnimal (main class) ()' + class Object " Employee ", 5 Objects ()& * & object - Setter Method Object 1 & #$ " Object ' . ani1 - & " name ' . bhh..c - & " leg ' & h. - & " eat ' & bhhc h Object 1 & #$ " Object ' . ani5 - & " name ' . bhh..c - & " leg ' & h. - & " eat ' & bhhc 5.3 + class ShowAnimal (main class) " 0(" Object () Object " 0(" / !