开发者

How do I share a method between two classes?

I have an iPhone app t开发者_JAVA技巧hat uses a Tab Bar Controller with 3 tabs. Each tab is a separate class. There are several methods that are identical in each class. Instead of having three copies of the same method, I'd like to share the method between the classes. However, I have not figured out how to do this.

Thanks.


This is a classical case of inheritance. Create a base class, and put all things that are common across the classes you are trying to build, into it, both functions and data members. Then, derive your three classes for your tabs from this class (inherit from it, or make it the parent class, lots of overlapping terms here that people generally throw around). Make sure your methods in the parent class are NOT defined as private methods. That would make them inaccessible to your child classes. Hope that helps!


Here you can make a new class and define that method inside that class which you want to share between more than one class.

Now whenever you want to access that method, just import the class and you can use the same method in multiple classes.

Let me know if you need more help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜