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.构造方法有返回值,和当前 ...
随机推荐
- [置顶]
不再迷惑,也许之前你从未真正懂得 Scroller 及滑动机制
学习本来就是从困惑中摸索问题答案的过程,能够描述出来问题就已经成功了一半.只要发现了困扰你的东西是什么,那么你就离解答出来不远了.----肯尼斯 R. 莱伯德 一直以来,Android 开发中绕不过去 ...
- matplotlib 数据可视化
图的基本结构 通常,使用 numpy 组织数据, 使用 matplotlib API 进行数据图像绘制. 一幅数据图基本上包括如下结构: Data: 数据区,包括数据点.描绘形状 Axis: 坐标轴, ...
- .pyc和.pyo文件有何用
百度知道:http://zhidao.baidu.com/link?url=_tFP1xglFnoEBObWtIArI3b3Ft0PQowx5m5ruIaX3mFIAFVr7vX45Lfb0geCjA ...
- 【剑指offer】09-2跳台阶,C++实现
原创博文,转载请注明出处! # 本文是牛客网<剑指offer>刷题笔记 1.题目 # 一只青蛙一次可以跳1级台阶,也可以跳2级.求该青蛙跳n级的台阶总共有多少种跳法. 2.思路 # 跳0级 ...
- HDU2161
解题思路:判断素数模板题. #include<cstdio> #include<cstring> #include<algorithm> using namespa ...
- Mysql 分组查询最高分
今天告诉我要写一个服务,目的是按照每个班中各分组中竞赛最高分组平分小组得分给各个成员的服务,于是就有两个技术需求 1 查询每个班的冠军团队 2 增加一组人的分数 从“1”中,查出每个班N个分组中的得分 ...
- Page View Controllers
Page View Controllers You use a page view controller to present content in a page-by-page manner. A ...
- PHP5.3、PHP5.4、PHP5.5、PHP5.6的新特性
1. PHP5.3中的新特性 1.1 支持命名空间(namespace) 毫无疑问,命名空间是PHP5.3所带来的最重要的新特性. 在PHP5.3中,可以用命名空间防止代码的冲突,命名空间的分隔符为 ...
- PHP的网站主要攻击方式有哪些
1.命令注入(Command Injection) 2.eval注入(Eval Injection) 3.客户端脚本攻击(Script Insertion) 4.跨网站脚本攻击(Cross Site ...
- input type = number 去除上下箭头,禁用滚轮事件(默认的自带滚轮加减数字)
<style type="text/css"> /*盒子大小从边框开始计算*/ html * { box-sizing: border-box; } /*解决模态框抖动 ...