Overwrite static method

Asked

Viewed 182 times

1

I don’t even know how to ask the question, but I’ll try. I have a static method in a parent class, and I planned to overwrite in the child classes, but from what I’m seeing here, this is not possible.

I remembered a feature that you have in Java, in which I don’t even remember how to apply in the method signature, but I know that when you will call the method, you call more or less like this:

nomeDoMetodo(NomeDaClasse.class);

I think this might be a more correct way to make the method do different behaviors.

I was wondering if you had a way to do that at C#?

  • Your question is not very clear. But I would indicate you search for delegates. It may suit your need.

1 answer

1

I don’t know what the Java engine looks like, I’ll just answer part C#.

What you want to do is not conceptually correct and therefore has no specific mechanism to deal with this issue. Of course you can do with reflection or other mechanisms that somehow use reflection.

Static methods do not belong to the instance, they belong to the class. Only instance method can be overwritten and if it is virtual.

On the other hand, I don’t think that’s necessary. If you want the derived class to have a static method with the same signature then create this method. You don’t need to do anything else. It may have another implementation or the same implementation (although in this case it would be better to call the method that is serving as the basis, or even create this method in the derived class).

Static methods should be called by the class and this by itself already prevents polymorphism. Therefore the need makes no sense.

  • I’m trying to do a method that takes all contacts from a file, but there are two types of contacts, each saving in a different file. public static List<Contato> Todos(Contato contato)&#xA; {&#xA;&#xA; }

  • And instead of this contact contact, I wanted a way to filter the type of contact, without having to instantiate an object, because it wouldn’t make sense

  • I don’t think this has anything to do with what you asked.

  • From what I read of your reply, in Java it’s identical.

  • 1

    @Jeffersonquesado I just don’t know if there is any reflective way that facilitates this that he wants. It must have, in C# has, but it is something very wrong. I think he wants to do something else, but how do we know without him answering?

  • I agree very much that reflection in this case does not seem at all right. For me it would make much more sense to pass a function to be executed.

  • How could I do that in C# &#xA;public class Teste {&#xA;&#xA; public void ver(Class<? extends Teste> tipo) {&#xA; &#xA; }&#xA;}&#xA;

  • You can’t change the question, if you want to know something else ask a new question.

  • Who negative could inform what has wrong in the answer.

  • But it’s the same thing, only this time I went straight to the point, instead of talking like I would call it, I talked like it’s signature method, in which I want to see if it’s possible in C#

  • The question is quite another, it may be that you are using for the same thing, but the subject is another, you were talking about static method and now you are talking about genericity.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.