Java中的约定-"new"构造函数/方法之外? [英] Convention in java - "new" outside of constructor / method?

查看:75
本文介绍了Java中的约定-"new"构造函数/方法之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题.一位内心的朋友编写了与此代码相似的代码(这只是为了向您解释我的问题,根本没有用....)

Simple question. A friend of mind wrote code similar to this one (which is just to explain you my question, it's not useful at all....)

class Example{
    private int[] tab = new int[10];
    public Example() {
        for(int i = 0 ; i < 10 ; i++)
            tab[i] = (int)(Math.random()*100);
        for(int i = 0 ; i < 10 ; i++)
            System.out.println(tab[i]);
    }
    public static void main(String[] arg) {
        Example ex = new Example();
    }
}

我告诉他他应该将new放在构造函数中

I told him he should put the new inside the constructor

class Example{
    private int[] tab;
    public Example() {
        tab = new int[10];
    ...
}

当他问我为什么时,我不知道该怎么回答:除了这样更好"之外,我没有明确的论点.我的学习方式是,您可以使用基本类型(int,double ...)来初始化变量,但对于数组,则应在构造函数中进行.

When he ask me why, I din't know what to answer : I didn't have a definite argument other than "it's better this way". The way I learn it, you can initialize variables with basic types (int, double...) but for arrays you should do it in the constructor.

所以:

  • 真的更好吗?
  • 有没有很好的理由:约定,风格?
  • 它会更改使用更少/更多内存的东西吗?

我不考虑元素数量可以变化的情况.永远是10

I'm not considering the case where the number of element can vary. It will ALWAYS be 10

推荐答案

  • 真的更好吗?
  • is it really better ?

不是真的,IMO.

  • 是否有一些很好的理由:约定?风格?
  • is there some good reasons : convention ? style ?

选择多个方法可能是有正当理由的,原因是您有多个构造函数,或者初始值取决于构造函数参数.例如

There may be valid reasons for choosing one way over the other is when you have multiple constructors, or when the initial values depend on constructor arguments; e.g.

private int[] tab;

public Example(int size) {
    tab = new int[size];
    for (int i = 0; i < size; i++)
        tab[i] = (int) (Math.random() * 100);
}

private int[] tab = new int[10];

public Example(int initial) {
    for (int i = 0; i < 10; i++)
        tab[i] = initial;
}

public Example() {
    for (int i = 0; i < 10; i++)
        tab[i] = (int) (Math.random() * 100);
}

除此之外(在您的示例中)没有关于此的一般规则.这是个人喜好.

Apart from that (and in your example) there are no general rules about this. It's a matter of personal taste.

  • 它会改变使用更少/更多的内存吗?
  • does it change anything like less/more memory used ?

在您的示例中,这没有什么区别.通常,代码大小或性能可能会有微小的差异,但这并不值得担心.

In your example it will make no difference. In general, there might be a tiny difference in the code size or performance, but it is not worth worrying about.

简而言之,我认为您对朋友的建议没有合理的依据.

In short, I don't think your suggestion to your friend has a rational basis.

以我的学习方式,您可以使用基本类型(int,double ...)来初始化变量,但对于数组,则应在构造函数中进行.

The way I learn it, you can initialize variables with basic types (int, double...) but for arrays you should do it in the constructor.

您应该不了解...或至少认识到这只是个人喜好.

You should unlearn that ... or at least recognize that it is just a personal preference.

这篇关于Java中的约定-"new"构造函数/方法之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆