neroverse.blogg.se

Duke simpleimage class
Duke simpleimage class















That code would figure out what the width of fgImage is, that it's 480 pixels, and decide that 480 is its answer. The computer will then go into that code and start executing it. We don't actually know what the code is, but that is fine as long as we know what it does. The code for getWidth() is in the Duke Learn to Program Library. The first line we have seen before, it makes an image and initializes fgImage to refer to it. Then you continue executing code after the method call. The method call, which is actually an expression, evaluates to whatever value the body returned. At some point, the method will figure out what answer it wants to give back, which is called returning its answer. It has the same semantics, whether it is inside a method or not. You do that code by following all the normal rules for executing code. First, execution goes into the method, you stop executing statements where you were, go inside the code for the method, and then do whatever code is there. Before we step through the behavior, we'll describe the semantics. Now, that you have seen the syntax of a method call, let us see the semantics. If the method took any parameters, you would specify them inside the parentheses. Parentheses after a name indicate that it is a method, or a function, which is similar.

duke simpleimage class

Next is the name of the method that we want to call. We want the getHeight method inside of fgImage. Next is a dot, the dot operator means inside of. Which SimpleImage's height do we want to get? Whatever SimpleImage we name here. First is the name of the object we want to invoke the method on.

duke simpleimage class

Here is an example of calling two methods, getWidth and getHeight, to get the width and height of an image respectively. In general, methods let you perform some operation, possibly one that is quite complex, on an object. How could you do these things? The answer is that you can call some methods that are built into SimpleImage, which already have the code to perform these operations.

#DUKE SIMPLEIMAGE CLASS HOW TO#

But you also need to know how to do things like look at particular pixels and change their colors.















Duke simpleimage class