28
Watcharin Puangplia 29/04/2011

class and object2

Embed Size (px)

DESCRIPTION

class and object2

Citation preview

Page 1: class and object2

Watcharin Puangplia 29/04/2011

Page 2: class and object2

attribute

Objectattribute

method

��������������� ��

������������������ ������������� �������������� ��������� Object �������� �����!�� " ���� Object #$�������%�� Method

Page 3: class and object2

Modifier� � �

������ ��Modifier�

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

Page 4: class and object2

class Rectangle{private double length;private double width;public void setLength(double len){

length = len; length = len; } public void setWidth(double w){ width = w;}public double getLength(){

return length; } public double getWidth(){ public double getWidth(){ return width;

}public double getArea(){

return length * width; }}

Page 5: class and object2

public class Student{ //��� #���CD� Student

private String id; //!E$�F���GH id

private String name; //!E$�F���GHCD�

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 IDid = ID; //��GH��E���D%�� ���� I������!��J ID

name = n; //��GHCD���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��

}

}

Page 6: class and object2

Overloading Method #C������������� Class �� Method ��D��CD����C� �E �!����������� � ��D�!�!�� �E ��K �����EH��� $ ��� ���� I������!��J��D���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){public void setData(String id_in,String name_in){id = id_in;name = name_in;gpa = 0;

}

Page 7: class and object2

Overloading #C������� method !EK �!� 2 method ��K LF� class

����$�E M����DCD� method (method name) EK ���C� �E �!� ����$�E M����DCD� method (method name) EK ���C� �E �!�

parameter list �!�!�� �E (signature !�� �E )

����

Overloading method #C� ����������������D��CD�����$�E �!��� ���� !E$�F�!�� ���E ��C���� $ ���J��$�� !JL�������E �I��� � H� � ����CD� � �F�!�� ���E ��C���� $ ���J��$�� !JL�������E �I��� � H� � ����CD� � ����$�E �!�!� �����������$�� ������� #��!E$�F� ������ �� ���� #��!E$�F����FN Integer �G����� � � ������ �D ���� ���FN String �G����� � � ��������� �D

Page 8: class and object2

���������� ����������������������� �� �����

parameter list ���� signature ��� �� ��������� ��� �� ���� Compiler �������� argument !�������"#�!�� � signature $��

������� ����!������ power %�"����� �&�� Error ������� ����!������ %�"����� �&��

���� ����

x = power(2); // x "'� 4 x = power(2,3); // x "'� 5 x = power(); // I Love You x = power(1,2,3) // � ��� � Error x = power("2",3) // � ��� � Error

Page 9: class and object2

public class Student {

private String id;

private String name;

private double gpa;

public void setData(){

id = null;

name = 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){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;

}

}

Page 10: class and object2

public class StudentDemo {

public static void main(String[] args) {

Student somsri,pranee,winai ;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);winai.setData("002", "Winai", 3.4);

System.out.println(somsri.toString()+"\n");

System.out.println(pranee.toString()+"\n");

System.out.println(winai.toString()+"\n");

}

}

Page 11: class and object2

Constructors #C� Method ��D������������� � ��CD��������� Object M������� � �I�� #�EK ����$���� EK M���FN Method ��D��CD�Object M������� � �I�� #�EK ����$���� EK 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{Class Student{

Student(){};

}

Page 12: class and object2

public class Rectangle

{

private double length;

private double width;private double width;

public Rectangle(double len, double w){

length = len;

width = w;

}

// ����� Class Rectangle ������ ���

}

Page 13: class and object2

public class ConstructorDemo{

public static void main(String[] args)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());

}}

Page 14: class and object2

�������� 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){public Rectangle(double len,double w){

length = len;width = w;

}

Page 15: class and object2

public class Rectangle

{

private double length;private double length;

private double width;

public Rectangle(){

length = 0.0;

width = 0.0;

}

public Rectangle(double len, double w) {

length = len; length = len;

width = w;

}

// ����� Class Rectangle ������ ���

}

Page 16: class and object2

public class ConstructorDemo2{

public static void main(String[] args)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 " +System.out.println("The box B area is " +boxB.getArea());

}}

Page 17: class and object2

�������ICK P� (primitive data type) �� int , long ,

double Q�Q ������ �#�� M�����#E����#����D��GH����� !E$�F��� double Q�Q ������ �#�� M�����#E����#����D��GH����� !E$�F��� (Copy Value) ��

a b

1a b

int a , int b; a = 1;b = a;

1 1a bb = a;

#���� a ���#E���� LF��D b

Page 18: class and object2

��������HH�� �� (reference data type) �FN ���������D��GH Address �� !E$�F��� �� Object !E$�F��� �� Array Q�Q ����GH Address �� !E$�F��� �� Object !E$�F��� �� Array Q�Q ������ �#��M�����#E������D������ �� (Copy reference) ��C�#E���� Address ED �� ��

Address Address

recA recB

Rectangle recA , recB;

recA = new Rectangle();

recB = new Rectangle();

RectangleObject

recB = new Rectangle();

RectangleObject

recB = recA

Page 19: class and object2

������� MF������HH����#��� ����������� Object �� #���� �D ���LF�FN people��$�� !J�� ���#���� �D L� �� � �D ���LF�FN people��$�� !J�� ���#���� �D L� ��

People

nameaddress

Class People �FN Class �����EH��GH����� # ��� Class Car

�����EH��GH�������� !JM�� ���� Class Car ���������GH���������� �� M���� �� Object Car ���LF���� ����FN ����� ��

Car

carIDowneraddress

People()setName(String)setAddress(String)getName()getAddress()

owner

Car()setCarID(String)setOwner(People)getCarID()getOwnerName()toString()

Page 20: class and object2

public class People {

private String name;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;}

}

Page 21: class and object2

public class Car {private String carID;private People owner;public Car(String carIDIn,People ownerIn){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()

+ "\nOwner : "+owner.getName()

+ "\nAddress: "+owner.getAddress();}

}

Page 22: class and object2

public class OwnerCar {

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

People winai = new People("Winai","Nakhonpathom");

People 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(car1.toString());

System.out.println(car2.toString());

}

}

Page 23: class and object2

� 4. ������������� ���������4.1 ������� class Employee � �������� / ���� �!"�� class ����4.1 ������� class Employee � �������� / ���� �!"�� class ����

String empNm : #$��%�&���double salary : ����'�!�' $��

��&���������� Setter-Getter Method '%$���#�&���� �()� �������* "������������ ����(&"��&�� Encapsulation field

Page 24: class and object2

// ����(�� ��// ������)��'���

��� Setter-Getter Method

Page 25: class and object2

� 4.2 ������� class TestEmployee (main class) � ������()'��� +���� class ���� ���� Object "�� Employee ",���� 2 Objects �()&���� �������*���& object

-��� Setter Method -��� Setter Method� ���� Object ��� 1 � �&���� #$��"�� Object '�.� emp1 - &���� ���"�� empNm '�.� bSomchaic - &���� ���"�� salary '���& 3,000

� ���� Object ��� 2 � �&���� #$��"�� Object '�.� emp2 - &���� ���"�� empNm '�.� bManeec - &���� ���"�� empNm '�.� bManeec - &���� ���"�� salary '���& 5,000

� 4.3 +���� class TestEmployee (main class) ���� �"���0("�� Object ����� � ����() Object ���� �"���0("������� / ���� �!�����

Page 26: class and object2
Page 27: class and object2

� 5. ������������� ���������5.1 ������� class Animal � �������� / ���� �!"�� class ����5.1 ������� class Animal � �������� / ���� �!"�� class ����

String name : #$����1int leg : ������"�String eat : ������

��&���������� Setter-Getter Method '%$���#�&���� �()� �������* "������������ ����(&"��&�� Encapsulation field����� ����(&"��&�� Encapsulation field

Page 28: class and object2

� 5.2 ������� class ShowAnimal (main class) � ������()'��� +���� class ���� ���� Object "�� Employee ",���� 5 Objects �()&���� �������*���& object

-��� Setter Method� ���� Object ��� 1 � �&���� #$��"�� Object '�.� ani1� ���� Object ��� 1 � �&���� #$��"�� Object '�.� ani1 - &���� ���"�� name '�.� bhh..c - &���� ���"�� leg '���& h.- &���� ���"�� eat '���& bhhc

h� ���� Object ��� 1 � �&���� #$��"�� Object '�.� ani5 - &���� ���"�� name '�.� bhh..c - &���� ���"�� name '�.� bhh..c - &���� ���"�� leg '���& h.- &���� ���"�� eat '���& bhhc

� 5.3 +���� class ShowAnimal (main class) ���� �"���0("�� Object ����� � ����() Object ���� �"���0("������� / ���� �!�����