简体   繁体   English

在Java中的方法,变量或对象之间使用两个句号

[英]Using two full stops in java between methods, variables, or objects

I'm sure there are explanations on the internet of basic feature of java as I would imagine, but because I'm completely clueless on what this action is trying to achieve I'm not describing the search properly and no results are coming up that explain what I'm confused with. 我敢肯定,互联网上已经有Java基本功能的解释,就像我想像的那样,但是由于我完全不知道该操作试图达到什么目的,因此我没有正确地描述搜索,并且没有结果解释我的困惑。 So please guide me in the right direction as I can't find the way to search for what this means. 因此,请引导我朝正确的方向前进,因为我找不到搜索这意味着什么的方法。

So below I have an example of what I'm confused with, from the line GL2 gl = drawable.getGL().getGL2(); 因此,下面有一个我很困惑的示例,从GL2 gl = drawable.getGL().getGL2();行开始: GL2 gl = drawable.getGL().getGL2(); . I know how to use methods with an object (eg object.method()) and such or with a variable, but this quoted line is different with two full stops and methods and not one. 我知道如何将方法用于对象(例如object.method()),此类对象或变量,但这引号不同,有两个句号和方法,而没有一个。 What is it trying to do exactly? 它到底想做什么? It seems to be creating a "gl" object to me, and then its using a getGL2() method but but also a getGL(), and when I try test using the same form with some methods I've made I can't chain them at the same time like that. 这似乎是它的使用getGL2()方法,但有getGL(),当我尝试测试使用相同的形式与一些方法我做了我不能创建一个“GL”对象给我,然后像这样同时将它们链接起来。 So I would use a variable or object and do ".method().method2()" and it won't work. 因此,我将使用变量或对象并执行“ .method()。method2()”,它将无法正常工作。

Also I don't understand how its creating an object " GL2 gl " with GL2 being the type and gl being the name of the object but after the "=" there is no "new" like I usually see. 另外,我不明白它是如何创建对象“ GL2 gl ”的,其中GL2是对象的类型,而gl是对象的名称,但是在“ =“之后,没有像我通常看到的那样“新”了。

Code example: 代码示例:

  public void init (GLAutoDrawable drawable) { 
    GL2 gl = drawable.getGL().getGL2();
    //GL2 gl2 = drawable.getGL().getGL2(); 
    gl.glClearColor(0,0,0,1); // black
    //gl2.glClearColor(0,0,0,1); 
  }

First, the method init takes an argument drawable . 首先,方法init接受一个drawable的参数。

drawable is of the type GLAutoDrawable , which has a method called getGL() . drawable的类型为GLAutoDrawable ,其类型为getGL()

For example, the class GLAutoDrawable could be something like this: 例如,类GLAutoDrawable可能是这样的:

public class GLAutoDrawable {

  // other stuff

  public GL getGL(){
    // other stuff
    return gl;
  }

  // other stuff

}

Here, getGL() returns a variable called gl , which is of the type GL . 在这里, getGL()返回名为gl的变量,其类型为GL Although this isn't specified in the code you provided, I'll just assume it's called GL . 尽管您提供的代码中未指定此名称,但我仅假设它称为GL

(For more information on return statements, read http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html ) (有关返回语句的更多信息,请阅读http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

Then, the variable that is returned from getGL() has the method getGL2() called. 然后,从getGL()返回的变量将调用方法getGL2()

For example, the class GL could look something like this: 例如, GL类可能看起来像这样:

public class GL {

  // other stuff

  public GL2 getGL2(){
    // other stuff
    return gl2;
  }

  // other stuff

}

The method getGL2() in the class GL returns an object called gl2 , which is what the final variable in your code is. 该方法getGL2()在类GL返回调用的对象gl2 ,这是在你的代码最终的变量是什么。

Basically, GL2 gl = drawable.getGL().getGL2(); 基本上, GL2 gl = drawable.getGL().getGL2(); is the same thing as: 与以下内容相同:

GL g = drawable.getGL();
GL2 gl = g.getGL2();

except it saves space without having to name a new variable. 除了可以节省空间而不必命名新变量。

GL2 gl = drawable.getGL().getGL2();

drawable has a getGL() method with returns an object (assume it's a GL ). drawable具有一个getGL()方法,该方法带有一个返回对象(假设它是GL )。 The GL object has a getGL2() method, so you are just calling getGL2() on the result of getGL() . GL对象具有getGL2()方法,因此您只是在getGL2()的结果上getGL()

This can make code easier to read, but if any of your "chain" returns null you will be in trouble. 这可以使代码更易于阅读,但是如果您的“链”中的任何一个返回null,那么您将遇到麻烦。

Calling a method on an object consists of four parts: object reference, name of method, parameters, return value. 在对象上调用方法包括四个部分:对象引用,方法名称,参数,返回值。

varToStoreReturnValue = objectReference.methodName(parameters);

Since a method can return an object, you can call a method to retrieve an object reference on which to call another method: 由于方法可以返回对象,因此可以调用方法以检索对象引用,并在该对象引用上调用另一个方法:

objectReference1 = objectReference.methodName(parameters);
varToStoreReturnValue = objectReference2.anotherMethodName(newParameters);

This example above could be written as: 上面的示例可以写成:

varToStoreReturnValue = objectReference.methodName(parameters).anotherMethodName(newParameters);

If no parameters are required, this would look like: 如果不需要任何参数,则如下所示:

ReturnType varToStoreReturnValue = objectReference.methodName().anotherMethodName();

... which in your case would look like: ...在您的情况下如下所示:

GL2 gl = drawable.getGL().getGL2();

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

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