Why to use [super init] in Objective C constructors?

前端 未结 4 1942
面向向阳花
面向向阳花 2021-01-24 14:34

Say I have a class named Item. Which is a superclass of NewsItem and TwitterItem.

If I want to create some NewsItem\'s do I have to use (inside constructor)

<         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 14:46

    In Java and C#, the compiler automatically makes your constructor call the superclass constructor if you don't explicitly call it. For example, the “Java Tutorials” say this:

    If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

    In Objective-C, the compiler doesn't do it automatically, so you have to do it yourself.

提交回复
热议问题