重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java代码的写法,Java写代码

JAVA 请问这个代码的标准写法 应该怎么写

来分析下问题:

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都网站制作、灵台网络推广、成都微信小程序、灵台网络营销、灵台企业策划、灵台品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供灵台建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

    要输出的是一个矩阵,对于矩阵来说有两个重要的参数,行和列,如果更灵活一点的话就是矩阵每个点的表示符号,以及列之间的分隔符:

    用面向对象的方式来分析问题,这个对象有四个属性,行数,列数,和矩阵中每个点的符号表示及列与列之间的分隔符表示,并且这个类有个行为,就是打印矩阵,因此这个需求可以用这样一个类来表示,四个属性,一个行为(方法):

public class Matrix {

private int rows;

private int cols;

private char symbol;

private String separator;

public Matrix(int rows, int cols, char symbol, String separator) {

this.cols = cols;

this.rows = rows;

this.symbol = symbol;

this.separator = separator;

}

public void print() {

StringBuilder builder = new StringBuilder();

for (int i = 0; i  rows; i++) {

for (int j = 0; j  cols; j++) {

builder.append(symbol);

if (j  cols - 1) {

builder.append(separator);

}

}

if (i  rows - 1) {

builder.append("\r\n");

}

}

System.out.print(builder.toString());

}

public static void main(String[] args){

Matrix matrix = new Matrix(10,10,'*'," ");

matrix.print();

}

}

java中的单例模式的代码怎么写

我从我的博客里把我的文章粘贴过来吧,对于单例模式模式应该有比较清楚的解释:

单例模式在我们日常的项目中十分常见,当我们在项目中需要一个这样的一个对象,这个对象在内存中只能有一个实例,这时我们就需要用到单例。

一般说来,单例模式通常有以下几种:

1.饥汉式单例

public class Singleton {

private Singleton(){};

private static Singleton instance = new Singleton();

public static Singleton getInstance(){

return instance;

}

}

这是最简单的单例,这种单例最常见,也很可靠!它有个唯一的缺点就是无法完成延迟加载——即当系统还没有用到此单例时,单例就会被加载到内存中。

在这里我们可以做个这样的测试:

将上述代码修改为:

public class Singleton {

private Singleton(){

System.out.println("createSingleton");

};

private static Singleton instance = new Singleton();

public static Singleton getInstance(){

return instance;

}

public static void testSingleton(){

System.out.println("CreateString");

}

}

而我们在另外一个测试类中对它进行测试(本例所有测试都通过Junit进行测试)

public class TestSingleton {

@Test

public void test(){

Singleton.testSingleton();

}

}

输出结果:

createSingleton

CreateString

我们可以注意到,在这个单例中,即使我们没有使用单例类,它还是被创建出来了,这当然是我们所不愿意看到的,所以也就有了以下一种单例。

2.懒汉式单例

public class Singleton1 {

private Singleton1(){

System.out.println("createSingleton");

}

private static Singleton1 instance = null;

public static synchronized Singleton1 getInstance(){

return instance==null?new Singleton1():instance;

}

public static void testSingleton(){

System.out.println("CreateString");

}

}

上面的单例获取实例时,是需要加上同步的,如果不加上同步,在多线程的环境中,当线程1完成新建单例操作,而在完成赋值操作之前,线程2就可能判

断instance为空,此时,线程2也将启动新建单例的操作,那么多个就出现了多个实例被新建,也就违反了我们使用单例模式的初衷了。

我们在这里也通过一个测试类,对它进行测试,最后面输出是

CreateString

可以看出,在未使用到单例类时,单例类并不会加载到内存中,只有我们需要使用到他的时候,才会进行实例化。

这种单例解决了单例的延迟加载,但是由于引入了同步的关键字,因此在多线程的环境下,所需的消耗的时间要远远大于第一种单例。我们可以通过一段测试代码来说明这个问题。

public class TestSingleton {

@Test

public void test(){

long beginTime1 = System.currentTimeMillis();

for(int i=0;i100000;i++){

Singleton.getInstance();

}

System.out.println("单例1花费时间:"+(System.currentTimeMillis()-beginTime1));

long beginTime2 = System.currentTimeMillis();

for(int i=0;i100000;i++){

Singleton1.getInstance();

}

System.out.println("单例2花费时间:"+(System.currentTimeMillis()-beginTime2));

}

}

最后输出的是:

单例1花费时间:0

单例2花费时间:10

可以看到,使用第一种单例耗时0ms,第二种单例耗时10ms,性能上存在明显的差异。为了使用延迟加载的功能,而导致单例的性能上存在明显差异,

是不是会得不偿失呢?是否可以找到一种更好的解决的办法呢?既可以解决延迟加载,又不至于性能损耗过多,所以,也就有了第三种单例:

3.内部类托管单例

public class Singleton2 {

private Singleton2(){}

private static class SingletonHolder{

private static Singleton2 instance=new Singleton2();

}

private static Singleton2 getInstance(){

return SingletonHolder.instance;

}

}

在这个单例中,我们通过静态内部类来托管单例,当这个单例被加载时,不会初始化单例类,只有当getInstance方法被调用的时候,才会去加载

SingletonHolder,从而才会去初始化instance。并且,单例的加载是在内部类的加载的时候完成的,所以天生对线程友好,而且也不需要

synchnoized关键字,可以说是兼具了以上的两个优点。

4.总结

一般来说,上述的单例已经基本可以保证在一个系统中只会存在一个实例了,但是,仍然可能会有其他的情况,导致系统生成多个单例,请看以下情况:

public class Singleton3 implements Serializable{

private Singleton3(){}

private static class SingletonHolder{

private static Singleton3 instance = new Singleton3();

}

public static Singleton3 getInstance(){

return SingletonHolder.instance;

}

}

通过一段代码来测试:

@Test

public void test() throws Exception{

Singleton3 s1 = null;

Singleton3 s2 = Singleton3.getInstance();

//1.将实例串行话到文件

FileOutputStream fos = new FileOutputStream("singleton.txt");

ObjectOutputStream oos =new ObjectOutputStream(fos);

oos.writeObject(s2);

oos.flush();

oos.close();

//2.从文件中读取出单例

FileInputStream fis = new FileInputStream("singleton.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

s1 = (Singleton3) ois.readObject();

if(s1==s2){

System.out.println("同一个实例");

}else{

System.out.println("不是同一个实例");

}

}

输出:

不是同一个实例

可以看到当我们把单例反序列化后,生成了多个不同的单例类,此时,我们必须在原来的代码中加入readResolve()函数,来阻止它生成新的单例

public class Singleton3 implements Serializable{

private Singleton3(){}

private static class SingletonHolder{

private static Singleton3 instance = new Singleton3();

}

public static Singleton3 getInstance(){

return SingletonHolder.instance;

}

//阻止生成新的实例

public Object readResolve(){

return SingletonHolder.instance;

}

}

再次测试时,就可以发现他们生成的是同一个实例了。

java的写法

由已知,程序需要有四个变量来存放四种数据,根据数据的性质,变量会设定为不同类型,如下

public class Test {

public static void main(String[] args) throws Exception{

String shouJiPinPai = "小米";

String caoZuoXiTong = "android";

int cpuShuLiang = 2;

Double jiaGe = 1999.0;

System.out.println("手机品牌:"+shouJiPinPai);

System.out.println("操作系统:"+caoZuoXiTong);

System.out.println("cpu数量:"+cpuShuLiang);

System.out.println("价格:"+jiaGe+"元");

}

}

其中 shouJiPinPai,caoZuoXiTong,cpuShuLiang 和 jiaGe 分别是中文拼音的字母(初学者可这样命名变量)。

附:想自学Java,建议先从java基础的书籍开始学习,如《java基础程序设计》等介绍java基础知识如基本数据类型、各类基本的运算符、变量、分支结构语句和对象与类等的书籍,再慢慢深入!

运行时可调用浏览器打开一个网页,网页地址在代码中的java代码怎么写?

网页地址在代码中的java代码写法如下:

package com.test;

import java.lang.reflect.Method;

//实现打开浏览器并跳到指定网址的类

public class BareBonesBrowserLaunch {

public static void openURL(String url) {

try {

browse(url);

} catch (Exception e) {

}

}

private static void browse(String url) throws Exception {

//获取操作系统的名字

String osName = System.getProperty("os.name", "");

if (osName.startsWith("Mac OS")) {

//苹果的打开方式

Class fileMgr = Class.forName("com.apple.eio.FileManager");

Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });

openURL.invoke(null, new Object[] { url });

} else if (osName.startsWith("Windows")) {

//windows的打开方式。

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);

} else {

// Unix or Linux的打开方式

String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };

String browser = null;

for (int count = 0; count browsers.length browser == null; count++)

//执行代码,在brower有值后跳出,

//这里是如果进程创建成功了,==0是表示正常结束。

if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)

browser = browsers[count];

if (browser == null)

throw new Exception("Could not find web browser");

else

//这个值在上面已经成功的得到了一个进程。

Runtime.getRuntime().exec(new String[] { browser, url });

}

}

}

//主方法 测试类

public static void main(String[] args) {

String url = "";      

BareBonesBrowserLaunch.openURL(url);

}


当前名称:java代码的写法,Java写代码
文章地址:http://cqcxhl.cn/article/hshgih.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP