I have two interfaces Interface1, Interface2. I only want to MyClass inherits from one of them.
It should compile error if I define a class like this:
class MyClass : Interface1, Interface2
{
}
Thanks!
First, it is not inheritance , it is implementation.
you should write one of them
class MyClass: Interface1
{
}
Related
I have a class in which I need to use a service but this class needs to be instantiated.
It's like this:
class EntrySubject implements ISubject {
constructor(entry: EntryEntity) {}
}
Since I need a service there, I could do this:
class EntrySubject implements ISubject {
constructor(entry: EntryEntity, entryService: EntryService) {}
}
And when using this class inside EntryService I would just instantiate my class as:
const entrySubject = new EntrySubject(entry, this);
But in this case, as far as I understand, every new subject instance would have its own EntryService, but what should I do if I want a single instance of EntryService?
All you have to do is to decorate your EntitySubject, or any other class that you wish to inject, with #Injectable(), and have that class in the 'providers' array of the module. That way, when the constructor has the class in its params, nest will inject a singletone instance of that class.
Please note that when using #Injectable, the default value that is used is #Injectable({scope: DEFAULT}) which means a singletone instance of the class.
For more info on injection scopes visit :
https://docs.nestjs.com/fundamentals/injection-scopes
I would like to know if I can access the constructor of the base class in its derived classes in C#. If yes please let me know how could we make it. Thanks in advance.
You can call the base class constructor as part of the execution of the derived class constructor
public MyBase
{
public MyBase() { }
}
public Derived
{
public Derived() : base() { }
}
When using this pattern, you are said to be using the base class initializer.
For more background, see the base keyword and instance constructors on MSDN.
is there a way to get the name of a child class inside of a super class?
class ChildClass inherits SuperClass { }
class SuperClass {
notify { "$NAME_OF_CHILD_CLASS is inheriting me": }
}
The expected output would be ChildClass is inheriting me. I'd need something similar to $module_name which contains the name of the class that contains the current resource's definition.
Is there a way to achieve this? I don't mind if I need a custom function for that...
Consider the following code:
// ======== Abstract class ========
public abstract class Creatures {
public abstract void loseEnergy();
public void execute()
{
loseEnergy();
}
}
// ======== Animals ========
public class Animals : Creatures
{
public override void loseEnergy(){}
}
public class Birds : Animals
{
public override void loseEnergy(){}
}
// ======== Human ========
public class Human : Creatures
{
public override void loseEnergy(){}
}
public class Male : Human
{
public override void loseEnergy(){}
}
public class Female : Human
{
public override void loseEnergy(){}
}
[ This code was based on the code by Jayson suggested here: "Base class methods calling derived class methods ?" ]
In the given code example, I would like to have the runtime executing EACH derived class object's certain method, in this case, which is 'loseEnergy()', however, I could not find the solution.
How do I approach this problem?
What can be useful to know or to try.. in order to solve this issue?
Your help is very much appreciated!
Thank you!
Kind regards,
Segara
P.S. Some search I have done so far:
"How to call overriden methods in all derived classes"
"Collection of derived classes that have generic base class"
"How to call derived function using base class object"
"Call method of the derived class through reflection possible or no"
EDIT:
I decided to stick to the idea I had before which is to have some list that would contain the objects of the classes that have 'loseEnergy()' method. Having such list I will be able to call every object's method 'loseEnergy()', which is what I wanted.
Question can be closed.
Thank you.
I didn't really understand your problem but anyway i can try to give you some means to use abstract classes :
If you use a abstract method, you SHOULD override it in a subclasses (like a method declared in an interface)
If you want that all inherited class use a same method, you can implement it in the abstract class ; all subclasses will use the method you implements if you don't override it, you've have to not declare it in the subclasses (extends < ABS_CLASS > is good enough)
If you want use a method of the abstract class which is override in the sub class you can use the keyword super .
I hope it will help you.
if you mean that you want the calls: female.loseEnergy() -> human.loseEnergy() -> creature.loseEnergy(), call the base method in the first line of the overriden one
public class Female : Human
{
public override void loseEnergy()
{
base.loseEnergy();
// do stuff
}
}
In the Greenfoot environment that you mention in the post above, the act() method is called only on actors which have been added into the "world". Internally, this adds them into a list. The simulation process iterates through the list and calls act() on each object in turn. Objects that are not "in the world" are not known to the system and so do not have their act method called. There is no magic here going on here.
If you wanted similar behaviour but without manually adding objects into a list, you could possibly have the base class constructor add new objects into a global list. I don't know C# so I don't know precisely how to do this, but I cannot imagine it would be difficult.
I am creating a class diagram but I was wondering if there would be any association between the 2 classes shown below - as far as I understand it, for association, ClassA must have an instance of ClassB which in this case there is not, however, it does need to know about a variable of ClassB, so is there an association between these 2 classes?
public class ClassA()
{
int val = ClassB.x
}
public class ClassB()
{
public static int x = 5;
}
Sure there is association. You can't use ClassA without existing of ClassB.
Yes there is an association between these two classes. The association is neither an aggregation nor a composition, it is a "uses/usage" dependency.
ClassA ------Uses-----> ClassB
Take a look at this link to know more about different types of dependencies