Advantage of singly rooted class hierarchy

后端 未结 4 599
抹茶落季
抹茶落季 2020-12-17 04:49

Excuse me if this is a silly question, but I want to understand all the advantages of singly rooted class (object) hierarchy in language like .Net, Java.

I can thin

4条回答
  •  北海茫月
    2020-12-17 05:47

    It provides a base for everything. For example in C# the Object class it the root which has methods such as ToString() and GetType() which are very useful if you're not sure what specific objects you will be dealing with.

    Also - not sure if it would be a good idea but you could create Extension Methods on the Object class and then every instance of every class would be able to use the method.

    For example, you could create an Extension Method called WriteToLogFile(this Object o) and then have it use reflection on the object to write details of it's instance members to your log. There are of-course better ways to log things but it is just an example.

提交回复
热议问题