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:

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

Java:
[Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)](http://The Java Tutorials)

:)

1 Like