Docsity

Calling One Method from Another Method in the Same Class | CSIS 110, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 – Lecture 30
Reading – Sections 7.5 and 7.6
Quiz – Monday, March 28
Last time we looked at the rules for calling one class from another. We also looked at
method style
Calling one method from another method in the same class
What are the rules for calling one method from another?
Within any particular class:
An instance method can call another instance method on the same object - don't need to
use an object name, as we already have the this object. (can invoke using this.method() ).
An instance method can call a class method in the same class - no class name is required.
A class method can call another class method in the same class - no class name is
required.
A class method cannot call an instance method - why not?
Class X {
private int value;
public static void method1( int a ) {
method2( a ); // Not valid
}
public void method2( int a ) {
value = a;
}
}
Method Style
A method provides functional abstraction. What’s abstraction?
- hide the details
- can think about the concept without having to know the details
- functional abstraction – we create a method that hides the details of how it does
something. When we read the code, when we see the method call we know that it does an
action for us – we don’t have to know how it does it.
pf2

Partial preview of the text

Download Calling One Method from Another Method in the Same Class | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 30

Reading – Sections 7.5 and 7. Quiz – Monday, March 28 Last time we looked at the rules for calling one class from another. We also looked at method style Calling one method from another method in the same class What are the rules for calling one method from another? Within any particular class: An instance method can call another instance method on the same object - don't need to use an object name, as we already have the this object. (can invoke using this.method() ). An instance method can call a class method in the same class - no class name is required. A class method can call another class method in the same class - no class name is required. A class method cannot call an instance method - why not? Class X { private int value; public static void method1( int a ) { method2( a ); // Not valid } public void method2( int a ) { value = a; } } Method Style A method provides functional abstraction. What’s abstraction?

  • hide the details
  • can think about the concept without having to know the details
  • functional abstraction – we create a method that hides the details of how it does something. When we read the code, when we see the method call we know that it does an action for us – we don’t have to know how it does it.

Let’s look at an example What does the program Example.java do? Compare with Example2.java Good properties for methods:

  • They have a good name that clearly identifies what they do
  • They are relatively short (10-20 lines max, shorter is OK)
  • They do one thing o printRow() o printTable() o GetUserInput() Not GetUserInputAndPrintRow DoStuff If you can’t think of a good name for what the method does, it probably is doing to many different things In your Interest programs, a big part of your style grade will based on your use of methods. Quiz Review: Quiz4Review.ppt