How can I write code below in java version?
I have read similar questions, but they are confusing, they answered that java didn't have delegate feature like c# had, in other hand they answered with their delegate implementation in java, but nothing is similar with my condition. I really hope it's clear on this question. I have been getting stuck since a week
class Action
{
public delegate void ActionDelegate();
public static ActionDelegate OnAction;
public void DoAction()
{
Console.WriteLine("Action A");
if (!ReferenceEquals(OnAction, null))
OnAction();
}
}
class TaskA
{
public TaskA()
{
Action.OnAction += DoTaskA;
}
private void DoTaskA()
{
Console.WriteLine("Do Task A");
}
}
class TaskB
{
public TaskB()
{
Action.OnAction += DoTaskB;
}
private void DoTaskB()
{
Console.WriteLine("Do Task B");
}
}
class Program
{
static void Main(string[] args)
{
TaskA taskA = new TaskA();
TaskB task = new TaskB();
Action action = new Action();
action.DoAction();
}
}
Output:
Action A
Do Task A
Do Task B
Press any keys to continue...
@FunctionalInterface
to such an interface. – Intension