理解Java构造器中的"this"
Calling Another Constructor
if the first statement of a constructor has the form this(...)
, then the constructor calls another constructor of the same class. For example,
public Employee(double s)
{
this("Employee #" + nextId, s);
nextId++;
}
When you call `new Employee(60000), the `Employee(double)` constructor calls the `Employee(String, double)` constructor.
import java.util.*;
/*
- This program demonstrates object construction.
*/
public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee();
//print out inofrmation about all Employee objects
for(Employee anEmployee : staff)
{
System.out.println("name =" + anEmployee.getName() + ", id =" + anEmployee.getId() + ", salary="
+ anEmployee.getSalary());
}
}
}
class Employee
{
private static int nextId;
private int id;
private String name = ""; //instance field initialization
private double salary;
//static initialization block
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}
// object initialization block
{
id = nextId;
nextId++;
}
// three overload constructors
public Employee(String n, double s)
{
name = n;
salary = s;
System.out.println("Constructor: Employee(String n, double s) is called.");
}
public Employee(double s)
{
// calls the Employee(String, doulbe)constructor
this("Employee #" + nextId, s);
}
// the default constructor
public Employee()
{
// name initialization to "" --see above
// salary not explicitly set -- initialized to 0
// id initialized in initialization block
System.out.println("The default constructor Employee() is called.");
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
}
输出结果:
Constructor: Employee(String n, double s) is called.
Constructor: Employee(String n, double s) is called.
The default constructor Employee() is called.
Disconnected from the target VM, address: '127.0.0.1:36707', transport: 'socket'
name =Harry, id =257, salary=40000.0
name =Employee #258, id =4283, salary=60000.0
name =, id =2113, salary=0.0
Process finished with exit code 0
理解Java构造器中的"this"的更多相关文章
- 深入理解Java虚拟机--中
深入理解Java虚拟机--中 第6章 类文件结构 6.2 无关性的基石 无关性的基石:有许多可以运行在各种不同平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关的字节码(ByteCode),从而 ...
- 轻松理解 Java开发中的依赖注入(DI)和控制反转(IOC)
前言 关于这个话题, 网上有很多文章,这里, 我希望通过最简单的话语与大家分享. 依赖注入和控制反转两个概念让很多初学这迷惑, 觉得玄之又玄,高深莫测. 这里想先说明两点: 依赖注入和控制反转不是高级 ...
- 垃圾回收相关(深入理解Java虚拟机中的内容)
程序计数器.虚拟机栈和本地方法栈这三个区域属于线程私有的,只存在于线程的生命周期内,线程结束之后也会消失,因此不需要对这三个区域进行垃圾回收.垃圾回收主要是针对 Java 堆和方法区进行. 判断一个对 ...
- 理解Java序列化中的SerialVersionUid
一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...
- 理解Java虚拟机中的栈、堆、堆栈
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 栈区: 每个线程包含一个栈区,栈中只保存方法中(不包括对象的成员变量)的基础数据类型和自定义对象的引用(不 ...
- 深入理解Java中的锁
转载:https://www.jianshu.com/p/2eb5ad8da4dc Java中的锁 常见的锁有synchronized.volatile.偏向锁.轻量级锁.重量级锁 1.synchro ...
- 转:理解Java泛型
JDK 5.0 中增加的泛型类型,是 Java 语言中类型安全的一次重要改进.但是,对于初次使用泛型类型的用户来说,泛型的某些方面看起来可能不容易明白,甚至非常奇怪.在本月的“Java 理论和实践”中 ...
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- 深入理解Java中的不可变对象
深入理解Java中的不可变对象 不可变对象想必大部分朋友都不陌生,大家在平时写代码的过程中100%会使用到不可变对象,比如最常见的String对象.包装器对象等,那么到底为何Java语言要这么设计,真 ...
随机推荐
- C# Label换行解决方法
一.label太短,无法完成显示所要显示信息长度,要换行,解决方法如下: (1) string aa =(长串) ; string cc= aa.Substring(0,10);//取前10个字符 s ...
- drawRect
1) 画笔设置 Paint.Style.STROKE 中空模式 paint = new Paint(); //新建一个画笔对象 paint.setAntiAlias(true);//抗锯齿功能 pai ...
- wangjunkai
<!Doctype html><html lang="en"> <head> <meta http-equiv="Content ...
- windows下源码安装调试postgresql
环境:windows 10 postgresql版本:postgresql-9.6.5 使用工具:vs2017社区版 辅助工具:perl.diff.flex.bison 相关工具下载地址: perl下 ...
- http请求方法,get 对比 post
本文转自:http://www.w3school.com.cn/tags/html_ref_httpmethods.asp 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? ...
- 小程序页面间传值(处理传值为对象,简单传值直接用options.XX的形式获取)
bookgoods:function(){ var Json = JSON.stringify(this.data.goods) wx.navigateTo({ url: '/pages/bookgo ...
- vue中的导航钩子
//钩子 登录拦截 router.beforeEach((to, from, next) => { const sessionToken = window.sessionStorage.getI ...
- StringOfChar 将一个字符重复多次 形成一个 字符串
StringOfChar Returns a string with a specified number of repeating characters. In Delphi code, Strin ...
- android:imeOptions="actionDone"
把EditText的Ime Options属性设置成不同的值,Enter键上可以显示不同的文字或图案actionNone : 回车键,按下后光标到下一行actionSend : SendactionN ...
- 最长连续公共子序列(LCS)与最长递增公共子序列(LIS)
最长公共子序列(不连续) 实际问题中也有比较多的应用,比如,论文查重这种,就是很实际的一个使用方面. 这个应该是最常见的一种了,不再赘述,直接按照转移方程来进行: 按最普通的方式就是,直接构造二维矩阵 ...