Questions tagged [methods]

A method is a block of code that performs a task and is associated with a class or an object. It is related to the non-object-oriented concepts of functions and procedures.

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments.

In object-oriented programming, a method is a subroutine (or procedure) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time.

Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance.

The association between class and method is called binding. A method associated with a class is said to be bound to the class. Methods can be bound to a class at compile time (static binding) or to an object at runtime (dynamic binding).

Common features

They have few things in common:

  1. They may take some parameters / arguments for their processing.
  2. They may have a return-value, that returns some value to the calling method.
  3. The concept of and is also associated with methods.

References

Also see:

30291 questions
41
votes
6 answers

Implement a pure virtual method in Objective-C

I want to go to there. Seriously though, how does one implement a pure virtual method in an "Apple" way? Do you use a Protocol with your base class and throw exceptions on those methods?
Chris
  • 645
  • 1
  • 5
  • 11
41
votes
1 answer

R hangs when there are too many arguments in setMethod (or setGeneric)

Basically, when there are many arguments in setMethod or (setGeneric) it works very slowly. Here is a basic example: setClassUnion(name = "mNumeric", members = c("missing", "numeric")) setClass(Class = "classA", representation = representation(ID =…
HBat
  • 4,873
  • 4
  • 39
  • 56
41
votes
16 answers

Method Overloading. Can you overuse it?

What's better practice when defining several methods that return the same shape of data with different filters? Explicit method names or overloaded methods? For example. If I have some Products and I'm pulling from a database explicit way: public…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
41
votes
3 answers

Why are hashCode() and getClass() native methods?

I checked the source code of Object class where I found that method declaration of getClass() was public final native Class getClass(); And the declaration of hashCode() was public native int hashCode(); Why are these two methods native…
user1391940
41
votes
4 answers

Scala - infix vs dot notation

Is there a best practice for one over the other? I've been reading the Scala book by Odersky, et al. and it seems like infix is used for a lot of the Collections API functions, whereas dot is reserved for programmer-defined functions.
Kevin Li
  • 1,540
  • 2
  • 17
  • 23
40
votes
6 answers

Bottle framework and OOP, using method instead of function

I've done some coding with Bottle. It's really simple and fits my needs. However, I got stick when I tried to wrap the application into a class : import bottle app = bottle class App(): def __init__(self,param): self.param = param …
user1129665
40
votes
3 answers

How to imitate string.Format() in my own method?

I have an object with a custom WriteLine(string) method. Something like this: public void WriteLine(string text) { this.StringList.Add(text); } What is the easiest way to duplicate the functionality of string.Format() with this method? For…
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
40
votes
3 answers

Double.valueOf(s) vs. Double.parseDouble

Casting an Object to a double and noticed both these methods. I see that parseDouble has been in since 1.2. Why add this method if it essentially does the same functionality as valueOf(s)?
Will
  • 8,246
  • 16
  • 60
  • 92
40
votes
6 answers

Method with multiple input parameters

I understand how to create my own methods that accept input parameters in objective-c but I have never actually created a method with more than one input parameter! From methods I have used with multiple input parameters each has a name along the…
C.Johns
  • 10,185
  • 20
  • 102
  • 156
40
votes
8 answers

PHP class: Global variable as property in class

I have a global variable outside my class = $MyNumber; How do I declare this as a property in myClass? For every method in my class, this is what I do: class myClass() { private function foo() { $privateNumber = $GLOBALS['MyNumber']; …
Zebra
  • 3,858
  • 9
  • 39
  • 52
40
votes
3 answers

Pass method argument to function

I'm curious if this is possible in Go. I have a type with multiple methods. Is it possible to have a function which takes a method argument and then call it for the type? Here is a small example of what I would want: package main import ( …
dangeroushobo
  • 1,291
  • 2
  • 16
  • 28
40
votes
7 answers

Passing null arguments to C# methods

Is there a way to pass null arguments to C# methods (something like null arguments in c++)? For example: Is it possible to translate the following c++ function to C# method: private void Example(int* arg1, int* arg2) { if(arg1 == null) { …
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
40
votes
4 answers

Can method parameters be dynamic in C#

In c# 4.0, are dynamic method parameters possible, like in the following code? public string MakeItQuack(dynamic duck) { string quack = duck.Quack(); return quack; } I've many cool examples of the dynamic keyword in C# 4.0, but not like above.…
40
votes
5 answers

Generic Method Executed with a runtime type

I have the following code: public class ClassExample { void DoSomthing(string name, T value) { SendToDatabase(name, value); } public class ParameterType { public readonly string Name; public readonly…
Sarit
  • 897
  • 3
  • 9
  • 18
40
votes
7 answers

C# cannot convert method to non delegate type

I have a class called Pin. public class Pin { private string title; public Pin() { } public setTitle(string title) { this.title = title; } public String getTitle() { return title; } } From another class…
gts13
  • 1,048
  • 1
  • 16
  • 29