import java.text.DecimalFormat;
public class Baoliuxiaoshu {
public static void main(String[] args) {
// TODO Auto-generated method stub
double i=21.23456;
DecimalFormat test=new DecimalFormat(".000");
String tt=test.format(i);
System.out.println(i);
System.out.println(tt);
System.out.println(String.format("%.2f", i));
}
}
21.23456
21.235
21.23
操作环境:
Mac
Eclipse neon(4.6)
Tomcat 9
遇到的坑:
1. 安装TomcatPlugin报错 无法安装其插件
2. preferences中没有Server选项
3. Server中Runtime Environments没有tomcat9版本 无法配置
(导致jsp程序报错 not found on the Java Build Path)
解决方法:
- 安装TomcatPlugin报错 无法安装其插件
网上的方法是通过http://tomcatplugin.sf.net/update 来安装
可是事实上也可能是这个Eclipse的版本问题 我们在线安装等半天最后说不行 所以这里采取的最好的方法是通过离线安装
net.sf.eclipse.tomcat.updatesite-2016-09-21
(链接:http://pan.baidu.com/s/1pLLWefh 密码:20q8)
下载好压缩包后解压为文件夹,之后help-install new software-add-local-选择文件夹-然后安装就成功了
之后在preferences中可以找到tomcat 选择版本为9 选则tomcat的路径 配置完成
这时候工具栏就会出现tomcat的标志了
懒得上图了
- preferences中没有Server选项
还是像刚才一样help-install new software-地址栏输入 http://download.eclipse.org/releases/neon
然后下面那栏搜索JST(J2EE标准工具)下载
打开preferences就能看到Server选项了
如果不是这样安装或者用其他网上给的网址安装有可能出现下面这个问题
-
Server中Runtime Environments没有tomcat9版本 没法配置环境 结果导致jsp程序报错 not found on the Java Build Path
解决方法其实就是上面 一定要下载新版的插件才能出现tomcat9的选项

然后记得在创建web程序时选上配置好的这个runtime environments 就不报错啦

java中this常用有三种功能分别是:引用成员变量,调用构造方法,返回类实例。第一种功能比较容易理解,实际上也就是防止对象属性与方法中形参名相同。
哦对 顺便说下this的概念:this代表调用这个函数的对象。 其实和python中的self类似。
java中this调用构造方法
使用格式如下:
this([参数列表])
系统将根据参数列表来决定调用哪一个构造方法。使用this时还需注意下面几点:
- 用this调用构造方法时,该语句只能用在构造方法中。
- this语句必须是构造方法中的第一条语句。
- 和new不同,this虽然可以调用构造方法,但它只是执行构造方法中的语句,并不会创建对象。
(百度网友淡忘WHY66的回答)
举个例子
package cn.rui0;
public class Testhis {
public Testhis() {
this(911);//调用有参的构造方法 必须放在第一行
System.out.println("无参");
}
public Testhis(int i) {
System.out.println(i+"\n"+"有参");
}
public static void main(String[] args) {
Testhis a=new Testhis();//创建对象的同时会调用无参的构造方法
}
}
output:
911
有参
无参
好的,接下来说
java中this返回类实例
可以理解为返回类的对象,然后…要说的都在下面了..
package cn.rui0;
public class Testhis {
int i;
int num;
public Testhis() {
this(911);//必须放在第一行
num=100;
System.out.println("无参");
}
public Testhis(int i) {
this.i=i+1;
System.out.println(this.i+"\n"+"有参");
}
public Testhis aaa(){ //这里定义的返回值类型为Testhis这个类
num++;
return this;
}
public static void main(String[] args) {
Testhis a=new Testhis();//创建对象的同时会调用无参的构造方法
Testhis b=new Testhis();//创建另一个对比
System.out.println("*********************");
System.out.println(a);//输出实例化的类的对象a的地址
System.out.println(a.aaa());
/*
* 输出时运行了这个实例化的类的对象a中的方法aaa()
* 而方法aaa()中返回了当下调用这个方法的对象a(因为用的是this所以是当下调用这个方法的对象a,而不是b)
* 并且类型为Testhis,可以理解为又成为属于Testhis的一个对象,就是又有了Testhis类下的一波属性什么的
* 所以输出的地址和上面a的一样,并且可以进行像下面这样的输出
*/
System.out.println(b);//先对比一下输出地址 是不一样的对象 好了没它事了
System.out.println(a.aaa().getClass().getName ());//获取当前类名
System.out.println(a.num);//获取当前类实例化对象的属性
System.out.println(a.aaa().num);
// 这部分可能有点抽象,本辣鸡打字可能也没说太清,大家不懂可以再多去对比几次的输出的语句和output去理解
}
}
output:
912
有参
无参
912
有参
无参
*********************
cn.rui0.Testhis@7852e922
cn.rui0.Testhis@7852e922
cn.rui0.Testhis@4e25154f
cn.rui0.Testhis
102
103
注意:
被static修饰的方法没有this指针。因为被static修饰的方法是公共的,不能说属于哪个具体的对象的
感谢阅读,如果有错误非常欢迎指出~thx 🙂
如果在同一个包内可以不用import 直接创建对象 比如同包内有一个class内容为
package cn.rui0;
public class Telphone {
public Telphone() {
// TODO Auto-generated constructor stub
System.out.println("cn.rui0.Telphone");
}
}
直接这样 或者注释掉第二行都可以调用
package cn.rui0;
import cn.rui0.Telphone;
public class pagbao {
public static void main(String[] args) {
Telphone phone = new Telphone();
}
}
但如果还有个包或者子包,他们和上面那个包中有相同的类名时
package cn.rui0.cs;
public class Telphone {
public Telphone() {
// TODO Auto-generated constructor stub
System.out.println("cn.rui0.cs.Telphone");
}
}
调用时就需要
import cn.rui0.cs.Telphone;
才能输出 cn.rui0.cs.Telphone
如果你像下面这样调用 没有明确指明具体是哪个class
package cn.rui0;
import cn.rui0.cs.*;//或者调用其他包时没有指明文件
public class pagbao {
public static void main(String[] args) {
Telphone phone = new Telphone();
}
}
则他会首先寻找自己包内有没有这个类,如果有就先调用自己包里的最后输出cn.rui0.Telphone 执行过程就如第一个一样。(如果没有就再调用import了的.cs里的这个类 最后输出的就是cn.rui0.cs.Telphone)像上面这个咱们2已经假设了两个包内有相同的类名的情况,它当然就会输出cn.rui0.Telphone了
so,
如果要调用的类在另一个包或者子包并与这个包中的类名不同,则可以用通配符调用其所有文件或者还是指定文件调用 如下
另一个包中的tclass类
package cn.ruilin;
public class tclass {
public tclass() {
// TODO Auto-generated constructor stub
System.out.println("cn.ruilin.tclass");
}
}
调用该包内其全部文件
package cn.rui0;
import cn.ruilin.*;
public class pagbao {
public static void main(String[] args) {
tclass phone = new tclass();
}
}
这样是可以运行的 并且输出cn.ruilin.tclass
也就是说,你不导入包时程序会自动把本包下的文件导入提供调用,当然你也可以指明调用哪个。
而当导入其他包的类与本包类重名时,不能采用import 包名.*;此时java程序只承认本包下的文件;而应该采用import 包名.文件名,此时java程序只承认由其他包导入的文件。