Java中的构造方法总结
Java中的构造方法总结
|
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(); }} |
SubClass constructor
|
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"); }} |
Sub with a string. 第二种方法即使父类中有显示的默认构造方法也不会被调用。
|
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"); }} |
one-1
one-2
one-3
|
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"); }} |
one-3
one-1
one-2
two-1
------------
one-1
one-2
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"); }} |
one-1
one-2
two-3
Test main() start
one-1
one-2
two-1
------------
one-1
one-2
two-2
2.主类的父类的构造方法被调用。
3.主类的非静态对象(变量)初始化。
4.调用主类的构造方法。
Java中的构造方法总结的更多相关文章
- Java学习笔记十六:Java中的构造方法
Java中的构造方法 1.使用new+构造方法 创建一个新的对象: 2.构造方法是定义在Java类中的一个用来初始化对象的方法: 3.构造方法与类同名且没有返回值: 4.语法格式: public 构造 ...
- Java中的构造方法
什么是构造方法:每一个类中至少有一个构造方法,它用于创建该类对象,这个和OC中的init有异曲同工之妙. 构造方法的格式:A:方法名与类名相同 B:没有返回值类型,没有void C:没有具体的返回值 ...
- Java面试 - 在Java中, 既然构造方法是一个方法,那么为什么不使用void 定义呢?
Java程序编译器是根据代码结构来进行编译处理的,执行的时候也是根据代码结构来处理的. 如果在构造方法上使用void,那么此结构就会与普通方法的结构相同,这样编译器会认为此方法是一个 普通方法,而普通 ...
- java中的构造方法与其作用
什么是构造方法呢? 方法名和类名相同 没有返回值类型,连void都不能写 没有具体的返回值 构造方法分为无参构造方法与有参构造方法. 先来看一下最简单的无参构造方法: Student.java pac ...
- java中的构造方法(2013-05-05-bd 写的日志迁移
特点: 1.方法名和类名相同 2.没有返回值 3.在创建一个类的新对象时,系统会自动的调用该类的构造方法完成对新对象的初始化 一个类中可以定义多个不同构造方法: 如果程序员没有定义构造方法,系统能够会 ...
- 134、Java中的构造方法和构造块
01.代码如下: package TIANPAN; class Book { public Book() { // 构造方法 System.out.println("[A]Book类的构造方 ...
- Java 中的构造方法
首先创建一个Transport类,定义好类的属性和方法,并且写好构造方法,先看下无参数的构造方法: public class Transport { //名字 public String name; ...
- Java中的构造方法「注意事项」
构造方法是专门用来创建对象的方法,当我们通过关键字new来创建对象时,其实就是调用构造方法. 语法: public 类名称(参数类型 参数名称){ 方法体 } 注意事项: 构造方法的名称必须和所在的类 ...
- 关于Java中的构造方法
关于构造方法: 1.构造方法又叫构造函数/构造器. 2.构造方法语法结构中"返回值类型"不需要指定,也不能写void,如若写void,则变成普通方法. 3.构造方法有返回值,和当前 ...
随机推荐
- .net MVC3 架构搭建描述
用visual studio创建.net MVC3 web项目时,默认情况下是将Views,Controllers,Models创建在一个项目中. 需要把Controllers和Models做为两个独 ...
- mac svn: E210004: Number is larger than maximum
SVN服务器IP地址发现改变,在Eclipse中的SVN资源库中执行Relocate重定位时发生错误: mac svn: E210004: Number is larger than maximum ...
- 如何查看PHP的配置信息
1.问题描述 如何利用PHP函数查看PHP的配置信息 2.问题函数 <?php echo phpinfo(); ?> 3.输出结果 phpinfo() PHP Version => ...
- sqlserver获取代理服务作业job的执行情况
以下脚本为获取sqlserver的执行job with testtemp as( SELECT sch.job_id,--his.[server] as InstanceName CONVERT(nv ...
- 编程技巧:使用整数同时进行多个true|false判断
情景 : 假设需要判断某银行用户的其中一个账号(profileA),币种(Currency)为人民币(CNY),余额是否大于1,0000,然后进行某业务逻辑处理. 概述: 为了进行这种判断,需要判断/ ...
- HihoCoder 1063 : 缩地 树形DP第二题(对象 边)
时间限制:12000ms 单点时限:1000ms 内存限制:256MB 描述 编织者是 Dota 系列中的一个伪核,拥有很强的生存能力和线上消耗能力.编织者的代表性技能是缩地.缩地带来的隐身.极限移动 ...
- 一图说明offsetTop、top、clientTop、scrollTop等
offsetParent:该属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),已进行过CSS定位的容器元素. 如果这个容器元素未进行CSS定位, ...
- 从 “x is null 和 x == null” 的区别看 C# 7 模式匹配中常量和 null 的匹配
尝试过写 if (x is null)?它与 if (x == null) 相比,孰优孰劣呢? x is null 还有 x is constant 是 C# 7.0 中引入的模式匹配(Pattern ...
- .NET中的那些受特别对待的类型(CriticalFinalizerObject)
转自:http://www.cnblogs.com/yuyijq/archive/2009/08/09/1542435.html 股票里面有个ST股,就是Special Treatment的意思.就是 ...
- bean:write
bean:write相当于<%=request.getAttribute("something")%> 例子一: 某处设置了request.setAttribute(& ...