Membina Kelas

Download Report

Transcript Membina Kelas

Method Definition
 Example
public void MethodName()
{
}
// Method Header
// Start of method body
// End of method body
 The Method Header
modifieropt ResultType MethodName
public static
public
public
void
void
double
(Formal ParameterList )
main
(String argv[ ] )
deposit
(double amount)
calculateArea
()
1
Method Header
 A method declaration begins with a method header
int add (int num1, int num2)
method
name
return
type
Formal parameter list
The parameter list specifies the type
and name of each parameter
The name of a parameter in the method
declaration is called a formal parameter
2
Method Body
 The method header is followed by the method body
int add (int num1, int num2)
{
int sum = num1 + num2;
return sum;
sum is local data
The return expression
must be consistent with
the return type
Local data are
created each time
the method is called,
and are destroyed
when it finishes
executing
}
3
User-Defined Methods
 Methods can return zero or one value

Value-returning methods


Methods that have a return type
Void methods

Methods that do not have a return type
4
calculateArea Method.
public double calculateArea()
{
double area;
area = length * width;
return area;
}
5
Return statement
 Value-returning method uses a return
statement to return its value; it passes a value
outside the method.
 Syntax:return statement
return expr;
 Where expr can be:

Variable, constant value or expression
6
User-Defined Methods
 Methods can have zero or >= 1 parameters

No parameters


Nothing inside bracket in method header
1 or more parameters

List the paramater/s inside bracket
7
Method Parameters
- as input/s to a method
public class Rectangle
{
. . .
public void setWidth(double w)
{
width = w;
}
public void setLength(double l)
{
length = l;
}
. . .
}
8
Syntax: Formal Parameter List
(dataType identifier, dataType identifier....)
Note: it can be one or more dataType
Eg.
setWidth( double w )
int add (int num1, int num2)
9
Using Rectangle Instances
 We use a method call to ask each object to
tell us its area:
System.out.println("rectangle1 area " + rectangle1.calculateArea());
System.out.println("rectangle2 area " + rectangle2.calculateArea());
References to
objects
Printed output:
Method calls
rectangle1 area 300
rectangle2 area 500
10
The RectangleUser Class
Definition
Class
Definition
public class RectangleUser
{
public static void main(String argv[])
{
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25,20);
System.out.println("rectangle1 area " +
rectangle1.calculateArea());
System.out.println("rectangle2 area " +
rectangle2.calculateArea());
} // main()
} // RectangleUser
An application must
have a main() method
Object
Creation
Object
Use
11
Method Call
 Syntax to call a method
methodName(actual parameter list);
Eg.
segi4.setWidth(20.5);
obj.add (25, count);
12
Formal vs Actual Parameters
 When a method is called, the actual parameters in the
invocation are copied into the formal parameters in the
method header
total = obj.add(25, count);
int add (int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
13
Method Overloading
 In Java, within a class, several methods can
have the same name. We called method
overloading
 Two methods are said to have different
formal parameter lists:


If both methods have a different
number of formal parameters
If the number of formal parameters
is the same in both methods, the
data type of the formal parameters
in the order we list must differ
in at least one position
14
Method Overloading
 Example:
public
public
public
public
public
public
void
void
void
void
void
void
methodABC()
methodABC(int x)
methodABC(int x, double y)
methodABC(double x, int y)
methodABC(char x, double y)
methodABC(String x,int y)
15
Java code for overloading





















public class Exam
{
public static void main (String [] args)
{
int test1=75, test2=68, total_test1, total_test2;
Exam midsem=new Exam();
total_test1 = midsem.result(test1);
System.out.println("Total test 1 : "+ total_test1);
total_test2 = midsem.result(test1,test2);
System.out.println("Total test 2 : "+ total_test2);
}
int result (int i)
{
return i++;
}
int result (int i, int j)
{
return ++i + j;
}
}
16
Java code (constructor overloading)
public class Student
{ String name;
int age;
Student(String n, int a)
{
name = n; age = a;
System.out.println ("Name1 :" + name);
System.out.println ("Age1 :" + age);
}
Student(String n)
{
name = n; age = 18;
System.out.println ("Name2 :" + name);
System.out.println ("Age2 :" + age);
}
public static void main (String args[])
{
Student myStudent1=new Student("Adam",22);
Student myStudent2=new Student("Adlin");
}
}
17
Object Methods & Class Methods
 Object/Instance methods belong to objects
and can only be applied after the objects are
created.
 They called by the following :
objectName.methodName();
 Class can have its own methods known as
class methods or static methods
18
Static Methods
 Java supports static methods as well as static variables.
 Static Method:-
Belongs to class (NOT to objects created from the class)
 Can be called without creating an object/instance of the
class
 To define a static method, put the modifier static in the
method declaration:
 Static methods are called by :

ClassName.methodName();
19