如何在Java中创建正确的实例?

时间:2022-07-14 18:09:01

I am learning Java and found this article on *.

我正在学习Java,并在*上发现了这篇文章。

So there are two classes:

所以有两个类:

public class Image {

...

    public Image clone() {
        Image clone = new Image(getMagicNumber(), getHeight(), getWidth(), getMax());
        for (int i = 0; i < getHeight(); i++){

            for (int j = 0; j < getWidth(); j++){
                clone.setPixel(getPixel(i, j), i, j);
             }
        }
        return clone;
    }
}

And than there is this class:

而且还有这个课程:

public class Filter {

    public Filter() {

    }

    public Image linearFilter(Image image, float[][] kernel) {
        Image filtered = image.clone();

        ...

         return filtered;
    }
}

I am used to do X instancename = new X(); for creating an instance, where X is the name of the class. Are there different ways for creating an instance? For example in the Filter class: How is Image filtered = image.clone(); creating an instance? In order to create an instance I thought on both sides of the "equation" X has to be equal. What I mean by this: Image filtered = new Image();. I don't understand how Image filtered = image.clone(); is creating a new instance. Can someone explain?

我习惯做X instancename = new X();用于创建实例,其中X是类的名称。是否有不同的方法来创建实例?例如,在Filter类中:Image filtered如何= image.clone();创建一个实例?为了创建一个实例,我认为“方程式”X的两边必须相等。我的意思是:Image filtered = new Image();.我不明白Image filtered = image.clone();正在创建一个新实例。谁能解释一下?

2 个解决方案

#1


1  

Image filtered = image.clone();

Is same as

是一样的

Image filtered = new Image();

you can see thet clone() is a method of your class which return the instance of the class Image

你可以看到这个clone()是你的类的一个方法,它返回类Image的实例

But making instance using methods like clone() are supportive when you want to create only one instance of your class, you can make the instance of your class public and final, and return it using a public method.

但是当你想只创建一个类的实例时,使用clone()等方法制作实例是可以支持的,你可以将你的类的实例设为public和final,并使用public方法返回它。

#2


1  

class: How is Image filtered = image.clone(); creating an instance?

class:Image filtered如何= image.clone();创建一个实例?

It is creating an instance as you can see that the method clone() is returning the type Image, the first line of the method is showing this :

它正在创建一个实例,因为您可以看到方法clone()返回的是Image类型,该方法的第一行显示如下:

Image clone = new Image(getMagicNumber(), getHeight(), getWidth(), getMax());

Image clone = new Image(getMagicNumber(),getHeight(),getWidth(),getMax());

and at last of this method clone is returned,this was the logic

并且在这个方法的最后,返回了clone,这是逻辑

I hope it helps

我希望它有所帮助

#1


1  

Image filtered = image.clone();

Is same as

是一样的

Image filtered = new Image();

you can see thet clone() is a method of your class which return the instance of the class Image

你可以看到这个clone()是你的类的一个方法,它返回类Image的实例

But making instance using methods like clone() are supportive when you want to create only one instance of your class, you can make the instance of your class public and final, and return it using a public method.

但是当你想只创建一个类的实例时,使用clone()等方法制作实例是可以支持的,你可以将你的类的实例设为public和final,并使用public方法返回它。

#2


1  

class: How is Image filtered = image.clone(); creating an instance?

class:Image filtered如何= image.clone();创建一个实例?

It is creating an instance as you can see that the method clone() is returning the type Image, the first line of the method is showing this :

它正在创建一个实例,因为您可以看到方法clone()返回的是Image类型,该方法的第一行显示如下:

Image clone = new Image(getMagicNumber(), getHeight(), getWidth(), getMax());

Image clone = new Image(getMagicNumber(),getHeight(),getWidth(),getMax());

and at last of this method clone is returned,this was the logic

并且在这个方法的最后,返回了clone,这是逻辑

I hope it helps

我希望它有所帮助