简体   繁体   中英

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. 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(); . 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. 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. So I would use a variable or object and do ".method().method2()" and it won't work.

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.

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 .

drawable is of the type GLAutoDrawable , which has a method called getGL() .

For example, the class GLAutoDrawable could be something like this:

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 . Although this isn't specified in the code you provided, I'll just assume it's called GL .

(For more information on return statements, read http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html )

Then, the variable that is returned from getGL() has the method getGL2() called.

For example, the class GL could look something like this:

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.

Basically, 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 ). The GL object has a getGL2() method, so you are just calling getGL2() on the result of getGL() .

This can make code easier to read, but if any of your "chain" returns null you will be in trouble.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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