在面向对象编程中,对象是方法调用的一部分吗?

发布于 2024-12-28 21:19:08 字数 477 浏览 2 评论 0原文

例如,假设我有一个像这样定义的对象:

public class Example {
    public void doSomething() {
        // does something
    }
}

如果我想调用 doSomething,我需要一个 Example 的实例:

Example foo = new Example();
foo.doSomething();  // doSomething is executed

我的问题是,该对象的哪一部分foo.doSomething(); 行被正式视为方法调用

它只是 doSomething() 部分还是包含对象 (foo.doSomething()) 的整个语句?

For example, imagine I have an object defined like so:

public class Example {
    public void doSomething() {
        // does something
    }
}

If I wanted to call doSomething, I'd need an instance of Example:

Example foo = new Example();
foo.doSomething();  // doSomething is executed

My question is, what part of the line foo.doSomething(); is officially considered the method call?

Is it just the doSomething() part or is it the entire statement including the object (foo.doSomething())?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

微凉徒眸意 2025-01-04 21:19:08

在 Java 中,整个 target.method() 被视为方法调用的一部分:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448

这意味着如果您有这样的代码:

foo.bar().baz()

...那么整个表达式就是 baz() 的方法调用,并且该方法调用的目标本身就是另一个方法调用。

In Java, the whole target.method() is considered part of the method invocation:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448

This means that if you have code like this:

foo.bar().baz()

...then the whole expression is the method invocation of baz(), and the target of that method invocation is itself another method invocation.

非要怀念 2025-01-04 21:19:08

doSomething 是Example 类的成员。
foo 是Example 类的一个实例。

doSomething is a member of the Example class.
foo is an instance of the Example class.

赏烟花じ飞满天 2025-01-04 21:19:08

我不知道 OOP 术语是否是一个控制机构,但就我而言,OOP 中的方法不能独立于类。您有两种选择,调用实例方法或类(静态)方法。以太方式,您需要给出调用方法的实体(类或对象)的名称和方法名称。所以 foo.doSometghing() 是方法调用。

I don't know if the is a governing body controlling the OOP terminology, but as fat as i'm concerned methods in OOP can't be independent of classes. You have two choices, you call an instance method or a class (static) method. Ether way, you need to give the name of the entity (class or object) you call the method of and the method name. So foo.doSometghing() is the method call.

没企图 2025-01-04 21:19:08

简而言之,就是 doSomething()。对于实例方法(例如您的doSomething()方法),我们会说您调用了对象上的方法。每个实例方法都有一个额外的参数,称为 this,它代表您调用该方法的对象,语言会自动将其传递给您的方法。

doSomething() 是方法,foo 是您调用该方法的实例。 foo 将作为 this 参数隐式传递。

In short, just the doSomething(). For an instance method (such as your doSomething() method), we would say that you called the method on the object. Every instance method has an extra argument, called this, which represents the object that you called the method on, and the language passes it to the method automatically for you.

doSomething() is the method, and foo is the instance that you called the method on. foo will be implicitly passed as the this parameter.

我喜欢麦丽素 2025-01-04 21:19:08

doSomething() 是方法调用,foo 是调用该方法的 Example 实例。

点运算符取消引用对象 foo,然后访问 foo 的基对象。在本例中该对象是示例。然后它访问Example 的方法doSomething(),在这种情况下会完成一些操作。在这种情况下,foo 是对从类Example 实例化的对象的引用。点运算符取消引用 foo,并且 doSomething() 访问取消引用的基类以调用该方法。

doSomething() is the method call, foo is the instance of Example that the method is being called on/from.

The dot operator de-references the object foo, and then accesses foo's base object. That object in this case is Example. It then accesses Example's method doSomething() in which case something is done. In this scenario foo is a reference to the object instantiated from the class Example. The dot operator de-references foo, and doSomething() accesses the de-referenced base class in order to call the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文