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.构造方法有返回值,和当前 ...
随机推荐
- Django项目的ORM操作之--数据模型类创建
在django项目中,其自带了ORM(Object Relation Mapping)对象关系映射框架,我们在django项目下app的models模块下对类进行操作,通过ORM会将我们对类的操作转化 ...
- 【SQL查询】正则表达式匹配字符串
1. 元字符说明 元字符 含义 ^ 匹配输入字符串的开始位置. $ 匹配输入字符串的结尾位置. * 匹配前面的字符零次或多次. + 匹配前面的字符一次或多次. ? 匹配前面的字符零次或一次. . 匹配 ...
- C# OPC UA服务器 OPC UA网关 三菱 西门子 欧姆龙 Modbus转OPC UA 服务器 可配置的OPC UA服务器网关 HslSharp软件文档
前言 本文将使用一个基于开源项目HslCommunication创建的OPC UA网关,方便通过配置创建一个OPC UA的网关中心.具体的操作及支持的设备信息项目参照下面: 开源项目HslCommun ...
- HDU 2268
http://acm.hdu.edu.cn/showproblem.php?pid=2268 小学四年级应用题,让我找回了儿时的快乐... #include <iostream> #inc ...
- C++语言的url encode 和decode
std::string UrlEncode(const std::string& szToEncode) { std::string src = szToEncode; char hex[] ...
- swift 3新特性总结
swift新特性之String 参考自[Swift 3.0 变化汇总系列总结-String] 使用方法:直接CMD+F搜索相应的函数或关键字符串,比较修改代码. 重要: /// 使用String的方法 ...
- [转]MongoDB c++驱动安装与使用
安装 获取源码:git clone https://github.com/mongodb/mongo-cxx-driver.git,解压 安装编译工具scons:yum install -y scon ...
- erlang游戏开发tcp
之前在开发游戏的时候我们采用smartfoxserver这个java开发的游戏引擎,这个引擎在开发回合制游戏方面速度还是不错的.但是面对客户日益增长的需求还是有些力不从心.比如集群,比如灾备,热切换, ...
- CF的Architecture,把它搞透!
Architecture Cloud Controller - Maintains a database with tables for orgs, spaces, apps, services, s ...
- LOJ2613 NOIP2013 华容道 【最短路】*
LOJ2613 NOIP2013 华容道 LINK 这是个好题,具体题意比较麻烦可以直接看LINK中的链接 然后考虑我们可能的移动方式 首先我们需要把白块移动到需要移动块S的附近(附近四格) 然后我们 ...