Java对象可以自己传递自己的实例

时间:2022-03-12 16:59:08

I have a class called SomeClass which has a method called methodToCall(SomeClass o)

我有一个名为SomeClass的类,它有一个名为methodToCall的方法(SomeClass o)

If I instantiate SomeClass like this:

如果我像这样实例化SomeClass:

SomeClass itsObject = new SomeClass();

Can I then do this:

我可以这样做:

itsObject.methodToCall(itsObject);

5 个解决方案

#1


11  

Absolutely. How that will behave will depend on the implementation, of course.

绝对。当然,这将取决于实施方式。

Just as an example, equals is defined to be reflexive, such that:

举个例子,equals被定义为反身,这样:

x.equals(x)

should always return true (assuming x is non-null).

应始终返回true(假设x为非null)。

#2


9  

Yes, you can.

是的你可以。

One (contrived) example:

一个(人为的)例子:

BigInteger one = BigInteger.ONE;
BigInteger two = one.add(one);

(You should try these things in your IDE - it takes less time than writing a question)

(你应该在你的IDE中尝试这些东西 - 它比写一个问题花费的时间更少)

#3


2  

Yes. There is nothing that prevents you doing this.

是。没有什么可以阻止你这样做。

#4


2  

It would be by default,
you don't need to specify it. in method body you can refer it as this

默认情况下,您不需要指定它。在方法体中,您可以将其称为此

Note: method should not be static

注意:方法不应该是静态的

and if you want to externally specify you can do it simple.

如果你想在外部指定你可以做到这一点。

#5


1  

Yes, you can do this, as long as methodToCall accepts an object of type SomeClass(or a class deriving from SomeClass) as a parameter:

是的,只要methodToCall接受SomeClass类型的对象(或派生自SomeClass的类)作为参数,就可以这样做:

public void methodToCall(SomeClass parameter){.....}

You can call it from outside your class:

你可以从课外打电话:

yourObject.methodToCall(yourObject)

or from within the class, using this :

或者从课堂内,使用这个:

public class SomeClass{
    ...
    public void AnotherMethod(SomeClass parameter)
    {
       this.methodToCall(this);
    }
    ...
}

#1


11  

Absolutely. How that will behave will depend on the implementation, of course.

绝对。当然,这将取决于实施方式。

Just as an example, equals is defined to be reflexive, such that:

举个例子,equals被定义为反身,这样:

x.equals(x)

should always return true (assuming x is non-null).

应始终返回true(假设x为非null)。

#2


9  

Yes, you can.

是的你可以。

One (contrived) example:

一个(人为的)例子:

BigInteger one = BigInteger.ONE;
BigInteger two = one.add(one);

(You should try these things in your IDE - it takes less time than writing a question)

(你应该在你的IDE中尝试这些东西 - 它比写一个问题花费的时间更少)

#3


2  

Yes. There is nothing that prevents you doing this.

是。没有什么可以阻止你这样做。

#4


2  

It would be by default,
you don't need to specify it. in method body you can refer it as this

默认情况下,您不需要指定它。在方法体中,您可以将其称为此

Note: method should not be static

注意:方法不应该是静态的

and if you want to externally specify you can do it simple.

如果你想在外部指定你可以做到这一点。

#5


1  

Yes, you can do this, as long as methodToCall accepts an object of type SomeClass(or a class deriving from SomeClass) as a parameter:

是的,只要methodToCall接受SomeClass类型的对象(或派生自SomeClass的类)作为参数,就可以这样做:

public void methodToCall(SomeClass parameter){.....}

You can call it from outside your class:

你可以从课外打电话:

yourObject.methodToCall(yourObject)

or from within the class, using this :

或者从课堂内,使用这个:

public class SomeClass{
    ...
    public void AnotherMethod(SomeClass parameter)
    {
       this.methodToCall(this);
    }
    ...
}