Professional Documents
Culture Documents
04-Some Techniques in Class Building
04-Some Techniques in Class Building
Goals
• Understand notions, roles and techniques for
OBJECT-ORIENTED LANGUAGE AND THEORY overloading methods and overloading
4. SOME TECHNIQUES IN CLASS BUILDING constructors
• Object member, class member
• How to pass arguments of functions
1 2
3 4
3 4
1
5 6
5 6
7 8
7 8
2
9 10
Discussion
Discussion void prt(String s) { System.out.println(s); }
void f1(char x) { prt("f1(char)"); }
• Given the following method:
void f1(byte x) { prt("f1(byte)"); }
public double test(String a, int b)
void f1(short x) { prt("f1(short)"); }
• Select overloading methods of the given method from the
void f1(int x) { prt("f1(int)"); }
list below:
void f1(long x) { prt("f1(long)"); }
1. void test(String b, int a)
void f1(float x) { prt("f1(float)"); }
2. public double test(String a)
3. private int test(int b, String a) void f1(double x) { prt("f1(double)"); }
9 10
11 12
Discussion
void prt(String s) { System.out.println(s); }
void f2(short x) { prt("f2(short)"); } => 2 b
int (signed)
void f2(int x) { prt("f2(int)"); } => 4 b -2^31 -> 2^31-1
void f2(long x) { prt("f2(long)"); } => 8 b
void f2(float x) { prt("f2(float)"); }
byte
• What will happen if we do as follows:
• f2(5); char (unsigned int) int short (signed int)
0 -> 2^16 -1 -2^15 -> 2^15-1
• char x= ‘ a ’ ; f2(x); => 2 b
• byte y=0; f2(y);
• float z = 0; f2(z);
• What will happen if we call f2(5.5)?
11 12
3
14
13 14
15 16
15 16
4
17 18
• Example
this keyword public class Ship {
private double x=0.0, y=0.0
In a constructor, the keyword this is used to refer to other private double speed=1.0, direction=0.0;
constructors in the same class public String name;
17 18
19 20
//(cont.)
private double degreeToRadian(double degrees) { Outline
return(degrees * Math.PI / 180.0);
}
public void move() {
move(1); 1. Method overloading
}
public void move(int steps) { 2. Class members and constant
double angle = degreesToRadians(direction);
x = x + (double)steps*speed*Math.cos(angle);
members
}
y = y + (double)steps*speed*Math.sin(angle);
3. Passing arguments to methods
public void printLocation() {
System.out.println(name + " is at ("
+ x + "," + y + ").");
}
} //end of Ship class
19 20
5
21 22
21 22
23
2.2. Class members Static parts: are shared between all objects
• Members may belong to either of the following: • sAttr: static (class scope)
• The whole class (class variables and methods, • iAttr: instance (object/instance scope)
indicated by the keyword static in Java) sAttr
• Individual objects (instance variables and methods) 5
obj1 obj2
• Static attributes and methods belong to the class
• Changing a value in one object of that class changes sAttr iAttr sAttr
the value for all of the objects iAttr
• Static
methods and fields can be accessed
without instantiating the class
• Static methods and fields are declared using the static 1 4
keyword
23 24
6
26
25 26
27 28
Example: Class JOptionPane in javax.swing Example – using static attributes and methods
in class JOptionPane
• Attributes
JOptionPane.showMessageDialog(null,”Ban da thao tac
loi", "Thong bao loi", JOptionPane.ERROR_MESSAGE);
27 28
7
29 30
29 30
31 32
Example 1
Example 2
class TestStatic{
public static int iStatic; public class Demo {
public int iNonStatic;
int i = 0;
}
public class TestS { void increase(){ i++; }
public static void main(String[] args) { public static void main(String[] args) {
TestStatic obj1 = new TestStatic();
increase();
obj1.iStatic = 10; obj1.iNonStatic = 11;
System.out.println("Gia tri cua i la" + i);
System.out.println(obj1.iStatic+”,”+obj1.iNonStatic);
TestStatic obj2 = new TestStatic(); }
System.out.println(obj2.iStatic+”,”+obj2.iNonStatic); }
obj2.iStatic = 12;
System.out.println(obj1.iStatic+”,”+obj1.iNonStatic);
} non-static method increase() cannot be referenced from a static contex
} non-static variable i cannot be referenced from a static context
31 32
8
Java static methods – Example
class MyUtils {
When static?
. . .
33 34
35
35 36
9
38
37 38
40
39 40
10
41
f1 = 999;
f2.setValue(888);
f3 = new Num(777);
41 42
44
43 44
11
45 46
45 46
47 48
47 48
12
49 50
Example Example
public class Test { public class Test {
public static void change(Student std){ public static void change(Student std){
std.setYear(2000); std = new Student("Hung", 1995);
} }
public static void main(String[] args) { public static void main(String[] args) {
Student std = new Student("Nam", 1990); Student std = new Student("Nam", 1990);
System.out.println(std.getYear()); System. out .println(std.getYear());
change(std); change (std);
System.out.println(std.getYear()); System. out .println(std.getYear());
} }
} }
49 50
51
51 52
13
53
Example 3
public class Test {
public static int plus(int... arr) {
int result = 0;
for (int i : arr) {
result += i;
}
return result;
}
53
14