简体   繁体   English

Java语法混乱,调用变量方法?

[英]Java Syntax confusion, calling a variables method?

I have a class Ball , within it a variable velocity which is a Vector , declared as: 我有一个Ball类,其中的一个可变velocity是一个Vector ,声明为:

private Vector velocity;

Now somewhere else in the class, there is a function called bounce , declared as: 现在,在该类的其他地方,有一个名为bounce的函数,声明为:

public void bounce(float surfaceTangent) {
    velocity = velocity.bounce(surfaceTangent);
}

I don't understand what that line assigning the velocity is doing, its unfamiliar syntax to me. 我不知道那条分配velocity线在做什么,它对我来说并不熟悉。 It looks like its calling velocity's bounce function, but velocity is a variable, not a class. 它看起来像是调用速度的bounce功能,但velocity是变量,而不是类。 It doesn't have a function at all... What exactly is this doing? 它根本没有功能……这到底在做什么?

Velocity is an instance of class Vector (confusing name here because most people would associate that type name with java.util.Vector), which has instance methods and instance variables that belong to every instance of Vector. Velocity是Vector类的一个实例(此处名称令人困惑,因为大多数人会将该类型名称与java.util.Vector关联),该类具有属于Vector的每个实例的实例方法和实例变量。 Bounce looks like an instance method here. 跳动看起来像是这里的实例方法。 In object oriented programming you usually interact with objects (instances) through their methods. 在面向对象的编程中,您通常通过对象(实例)的方法进行交互。

but velocity is a variable, not a class. 但是速度是一个变量,而不是一个类。 It doesn't have a function at all... 它根本没有功能...

In Java, classes do have their own class methods (and class variables), these are denoted by the method modifier of static. 在Java中,类确实具有自己的类方法(和类变量),它们由static的method修饰符表示。

您有一个声明为velocity的变量,您要为其分配由对Velocity实例调用的方法bounce(float)返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM