标签:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Sample { private int x; public Sample() { // 不带参数的构造方法 this(1); } public Sample(int x) { //带参数的构造方法 this.x=x; } public int Sample(int x) { //不是构造方法 return x++; } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Mystery { private String s; public void Mystery() { //不是构造方法 s = "constructor"; } void go() { System.out.println(s); } public static void main(String[] args) { Mystery m = new Mystery(); m.go(); }} |
|
1
2
3
4
5
6
7
8
9
|
public class Sample1{} public class Sample2{ public Sample2(int a){System.out.println("My Constructor");}} public class Sample3{ public Sample3(){System.out.println("My Default Constructor");}} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Platypus { String name; Platypus(String input) { name = input; } Platypus() { this("John/Mary Doe"); } public static void main(String args[]) { Platypus p1 = new Platypus("digger"); Platypus p2 = new Platypus(); System.out.println(p1.name + "----" + p2.name); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class getBirthInfo { void getBirthInfo() { System.out.println("born alive."); }} class Platypus1 extends getBirthInfo{ void getBirthInfo() { System.out.println("hatch from eggs"); System.out.println("a mammal normally is "); super.getBirthInfo(); }} public class test1 { public static void main(String[] args) { Platypus1 p1=new Platypus1(); p1.getBirthInfo(); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class getBirthInfo { getBirthInfo(){ System.out.println("auto"); } void aa() { System.out.println("born alive."); }} class Platypus1 extends getBirthInfo{ Platypus1() { super(); System.out.println("hatch from eggs"); System.out.println("a mammal normally is "); }} public class test1 { public static void main(String[] args) { Platypus1 p1=new Platypus1(); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class SuperClass{ SuperClass() { System.out.println("SuperClass constructor"); }}public class SubClass extends SuperClass { SubClass() { System.out.println("SubClass constructor"); } public static void main(String[] args) { SubClass sub = new SubClass(); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class SuperClass{ SuperClass(String str) { System.out.println("Super with a string."); }}public class SubClass extends SuperClass{ SubClass(String str) { System.out.println("Sub with a string."); } public static void main(String[] args) { SubClass sub = new SubClass("sub"); }} |
|
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
|
class One{ One(String str) { System.out.println(str); }}class Two{ One one_1 = new One("one-1"); One one_2 = new One("one-2"); One one_3 = new One("one-3"); Two(String str) { System.out.println(str); }}public class Test{ public static void main(String[] args) { System.out.println("Test main() start"); Two two = new Two("two"); }} |
|
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
|
class One { One(String str) { System.out.println(str); }}class Two{ One one_1 = new One("one-1"); One one_2 = new One("one-2"); static One one_3 = new One("one-3"); Two(String str) { System.out.println(str); }}public class Test{ public static void main(String[] args) { System.out.println("Test main() start"); Two two_1 = new Two("two-1"); System.out.println("------------"); Two two_2 = new Two("two-2"); }} |
|
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
|
class One{ One(String str) { System.out.println(str); }}class Two{ One one_1 = new One("one-1"); One one_2 = new One("one-2"); static One one_3 = new One("one-3"); Two(String str) { System.out.println(str); }}public class Test{ static Two two_3 = new Two("two-3"); public static void main(String[] args) { System.out.println("Test main() start"); Two two_1 = new Two("two-1"); System.out.println("------------"); Two two_2 = new Two("two-2"); }} |
标签:
原文地址:http://www.cnblogs.com/lubocsu/p/5096772.html