Professional Documents
Culture Documents
Techniques in Class Building
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
Nguyen Thi Thu Trang • How to pass arguments of functions
trangntt@soict.hust.edu.vn
3 4
1
2/25/19
5 6
7 8
2
2/25/19
9 10
Discussion
Discussion void prt(String s) { System.out.println(s); }
void f1(char x) { prt("f1(char)"); }
• Given a following method:
void f1(byte x) { prt("f1(byte)"); }
public double test(String a, int b)
void f1(short x) { prt("f1(short)"); }
• Let select overloading functions of the given method from
void f1(int x) { prt("f1(int)"); }
the 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)
void f1(double x) { prt("f1(double)"); }
3. private int test(int b, String a)
• What will happens if we do as follows:
4. private int test(String a, int b)
• f1(5);
5. double test(double a, int b)
• char x=‘a’; f1(x);
6. double test(int b)
• byte y=0; f1(y);
7. public double test(String a, long b)
• float z = 0; f1(z);
11 12
3
2/25/19
13 14
15 16
• Example
public class Ship { //(cont.)
private double x=0.0, y=0.0 private double degreeToRadian(double degrees) {
private double speed=1.0, direction=0.0; return(degrees * Math.PI / 180.0);
public String name; }
public void move() {
public Ship(String name) { move(1);
this.name = name; }
} public void move(int steps) {
public Ship(String name, double x, double y) { double angle = degreesToRadians(direction);
this(name); this.x = x; this.y = y; x = x + (double)steps*speed*Math.cos(angle);
}
y = y + (double)steps*speed*Math.sin(angle);
public Ship(String name, double x, double y,
double speed, double direction) { }
this(name, x, y); public void printLocation() {
this.speed = speed; System.out.println(name + " is at ("
this.direction = direction; + x + "," + y + ").");
} }
//to be continued… } //end of Ship class
4
2/25/19
17 18
19 20
5
2/25/19
22
valCount
2
val1 val2
value value
23 24
• Methods:
6
2/25/19
25 26
Example – using static attributes and methods in Example – using static attributes and methods
class JOptionPane in class JOptionPane (2)
JOptionPane.showMessageDialog(null,”Ban da thao tac Object[] options = { "OK", "CANCEL" };
loi", "Thong bao loi", JOptionPane.ERROR_MESSAGE);
JOptionPane.showOptionDialog(null,"Nhan OK de tiep tuc",
"Canh bao", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,null,options,options[0]);
27 28
Example 1
Static member (2) class TestStatic{
• Modifying value of a static member in an object will public static int iStatic;
modify the value of this member in all other objects of the public int iNonStatic;
}
class.
public class TestS {
• Static methods can access only static attributes and public static void main(String[] args) {
can call static methods in the same class. TestStatic obj1 = new TestStatic();
obj1.iStatic = 10; obj1.iNonStatic = 11;
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);
}
}
7
2/25/19
29
32
8
2/25/19
34
• Example
public Polygon polygonFrom(Point... corners) {
int numberOfSides = corners.length;
3.2. Passing by values
double squareOfSide1, lengthOfSide1; • C++
squareOfSide1 = (corners[1].x - corners[0].x)
• Passing values, pointers
*(corners[1].x - corners[0].x)
+(corners[1].y - corners[0].y) • Java
*(corners[1].y - corners[0].y) ; • Passing values
lengthOfSide1 = Math.sqrt(squareOfSide1);
//create & return a polygon connecting the Points
}
• corners is considered as an array
• You can pass and array or a sequence of arguments
9
2/25/19
37
39 40
10
2/25/19
42
43 44
11