Java访问权限原理与用法详解

2020-02-12 12:02:21于丽

输出结果

day7.Window@7852e922
day7.Window@7852e922

懒汉模式

类加载时,没有指定对象,只有在应用的时候才去创建对象,多线程的环境时,推荐使用饿汉式,因为是线
程安全的。

package day7;
class Window{
  private static Window win = null;
  private Window() {
}
public static Window getInstance() {
  if(win == null) {
    win = new Window();
  }
  return win;
  }
}
public class TestWindow {
public static void main(String[] args) {
Window win = Window.getInstance();
Window win1 = Window.getInstance();
System.out.println(win);
System.out.println(win1);
}
}

返回结果

day7.Window@7852e922
day7.Window@7852e922

API之Math类

常用方法

package day7;
public class TestMath {
public static void main(String[] args) {
// Math
System.out.println(Math.abs(‐33.4));//33.4
//大于等于44.6的最小整数‐》double
System.out.println(Math.ceil(44.6));//45.0
//小于等于44.6的最大整数‐》double
System.out.println(Math.floor(44.6));//44.0
//四舍五入为一个long数字
System.out.println(Math.round(44.6));//45
//求几次方的值
System.out.println(Math.pow(3,2));//9.0
//double [0.0,1.0)
double n = Math.random();
System.out.println(n);
//1‐10
//[最小值,最大值]
//(int)(Math.random()*(最大值‐最小值+1)+最小值)
int num = (int)(Math.random()*(10‐1+1)+1);
System.out.println(num);
}
}

Random类

Random rand1 = new Random(11);//11为随机种子
System.out.println(rand1.nextDouble());
Random rand2 = new Random(11);//
System.out.println(rand2.nextDouble());

随机种子相同时,相同随机次数输出结果相同。

Random rand3 = new Random(11);
//int范围内的整数
System.out.println(rand3.nextInt());
//[0,5)
System.out.println(rand3.nextInt(5));

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。