-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathDemoClass.java
103 lines (91 loc) · 2.59 KB
/
DemoClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/******************************************************************/
/* Author: CS307 Course Staff */
/* Date: February 14, 2005 */
/* Description: Demos constructors, static vs instance methods, */
/* and method overloading. */
/******************************************************************/
public class DemoClass
{
private int x;
public DemoClass()
{
// assign default value
x = 0;
}
public DemoClass(int x)
{
// use this.x to refer to the instance variable x
// use x to refer to a local variable x (more specifically,
// method parameter x)
this.x = x;
}
public DemoClass(DemoClass otherDemo)
{
// copy the value from the otherDemo
this.x = otherDemo.x;
}
// static method (aka class method)
public static void s1() {
return;
}
// instance method
public void i1() {
return;
}
// static calling static OK
// static calling instance is a compile-time error
public static void s2() {
// i1(); // compile-time error
s1(); // DemoClass.s1
return;
}
// instance calling static OK
// instance calling instance OK
public void i2() {
s1(); // DemoClass.s1();
i1(); // this.i1();
return;
}
// call various versions of overload() based on their
// list of parameters (aka function signatures)
public void overloadTester() {
System.out.println("overloadTester:\n");
overload((byte)1);
overload((short)1);
overload(1);
overload(1L);
overload(1.0f);
overload(1.0);
overload('1');
overload(true);
}
public void overload(byte b) {
System.out.println("byte");
}
public void overload(short s) {
System.out.println("short");
}
public void overload(int i) {
System.out.println("int");
}
public void overload(long l) {
System.out.println("long");
}
public void overload(float f) {
System.out.println("float");
}
public void overload(double d) {
System.out.println("double");
}
public void overload(char c) {
System.out.println("char");
}
public void overload(boolean b) {
System.out.println("boolean");
}
public static void main(String[] args) {
DemoClass dc = new DemoClass();
dc.overloadTester();
}
}
// end of DemoClass.java