How to reference an object in another function to its class?

Let’s say that we have a class and a function (sorry for the peudo-code):

class Person
{
    int age;
}

Person squid;

void IncreaseAge(Human object)
{

}

How would I make it so that I could do something like this:

void IncreaseAge(Human dataType)
{
    squid.age += 1;
}

Thanks!

1 Like

1 way to do it: :angel:

Person squid;

void setup() {
  frameRate(1);
  squid = new Person();
}

void draw() {
  squid.increaseAge(); // alt.: ++squid.age;
  print(squid);
}

class Person {
  int age;

  Person increaseAge() {
    ++age;
    return this;
  }

  String toString() {
    return "Age: " + age + TAB;
  }
}
2 Likes

Another more “functional” way, w/o a method: :smile_cat:

Person squid;

void setup() {
  frameRate(1);
  squid = new Person();
}

void draw() {
  increaseAge(squid); // alt.: ++squid.age;
  print(squid);
}

Person increaseAge(final Person human) {
  ++human.age;
  return human;
}

class Person {
  int age;

  String toString() {
    return "Age: " + age + TAB;
  }
}
2 Likes

Hello,

Processing:
https://processing.org/reference/Object.html
https://processing.org/tutorials/objects/
https://processing.org/reference/class.html

Coding Train 7.1: Introduction to Functions and Objects - Processing Tutorial

Java:
[https://docs.oracle.com/javase/tutorial/java/javaOO/index.html](http://The Java Tutorials)

:)

1 Like