0

given the following:

public class ClassA
{
  private int val;
  public int setter1 { set{ val = value * 100 }}
  public int setter2 { set{ val = value * 200 }}
  
}

public class ClassB
{
  public int setter;
  
  public ClassB(int someSetter)
  {
    b = someSetter; // Obviously impossible, it's merely for demonstration purposes
  }
}

Would it be possible to assign Getter/Setters to instances that belong to a different class?

So that when we create 2 instances:

B b1 = new B(setter1),
  b2 = new B(setter2);

b1.setter = 1; // 'val' is equal to 100
b2.setter = 2; // 'val' is equal to 400

The reason I need this:

I have a bunch of instances that belong to the same class, and a local variable which I assign data to.

I don't know which instance I'm accessing, but I want to assign a Setter so that the value of the local variable changes.

I can easily achieve this differently, but I was wondering if passing Setters was possible.

Thx for reading!

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

0 Answers0