Summary: B Receives call from A, and automatically starts the start method
Let's say we have class A: A has a function named Start, that start function is initially called from another class (lets refer that class as C) and once it's called, it should call every method from all classes using Start (and using it's base class), with the same method.
And we have B: a class using A as it's base class, it's job is to receive the Start method from A, (initially called by C). Now i could simply do this by calling B's method directly from A, but in this case, B could be named anything, with multiple classes inheriting the same method.
This is useful because i don't want to assign all my variables to one start function. Instead, be able to create a function that allows the same function name to be called.
For example: here's what i was thinking it would look like
class A
{
public void Start()
{
// Call B's Start here...
}
}
class B : A
{
public void Start()
{
// Receive A's call ... and do stuff here
}
}
// This time, we also need to access this start method.
// But this class could be named anything
class anotherClass : A
{
public void Start()
{
// Receive A's call
}
}
class C
{
static void Main(string[] args)
{
A a = new A();
// Maybe somehow call all start methods here
a.Start();
}
}
But as you can see, Start in class A will be called, but it will never call start in class B.
In better context, I need a way for all Start methods from every class to be called.
You have never created a B
or anotherClass
object. Therefore their Start
method cannot be called.
Inheritance works the other way round. A derived class can call its base class members, because it knows its ancestor and inherits all its members (fields, properties, methods). The base class (A
) on the other side does not know its descendants.
You must use virtual
methods that you can override
in derived classes. Example:
class A
{
public virtual void Start()
{
Console.WriteLine("Starting A");
}
}
class B : A
{
public override void Start()
{
base.Start();
Console.WriteLine("Starting B");
}
}
Now you can create a B
object and call its Start
method
var b = new B();
b.Start();
This will output:
Starting A
Starting B
Since derived types are assignment compatible to their base types, you can also do something like this
var list = new List<A> { new B(), new A() };
foreach (A a in list) {
a.Start();
}
Starting A
Starting B
Starting A
where the two first lines are from B.Start()
and the last one from A.Start()
.
But this works only for classes in the direct lineage. You cannot call methods from siblings. Why? Let's make an example:
class C : A
{
private string s = "hello";
public override void Start()
{
base.Start();
Console.WriteLine("Starting C: " + s);
}
}
Assuming that you could do something like this in B
:
sibling(C).Start();
Where should the value "hello"
of s
come from? Neither A
, nor B
has such a field and a C
object was never created. So your requirement to call every method from all classes cannot be fulfilled. But if a field of A
was involved this would work, as B
inherits this field.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments