Greetings to all!
There are several classes in the project in java files. There are a number of functions that need to be used everywhere. It's kind of like checking if there is an internet connection, if there is an SD card. How best to arrange and use them, so as not to describe in each class?
thank
- omg ... ie oop ... =) - Gorets
2 answers
There are no functions in java. Simply no. But there is their equivalent - static methods. Some "gurus" may not understand if you talk about functions in java.
And the solution to your problem is known and it is very simple. Make yourself a separate class, which is usually called Utils, and add all the necessary "functions" there as public static methods. Well, somewhere like that
public class Utils { public static boolean isSDCadrdExists() { return false; } } and then just call as needed
if (Utils.isSDCardExists()) { // do } When there are a lot of methods in this class, divide into two (or three) and make the classes UtilsNetwork, UtilsMath, CommonUtils (or as the functionality will be adequate).
This method is very popular in java. As an example implementation, you can look at the class Math.
- Thanks, I just finished writing such a class, I forgot to specify static, I thought, why it doesn’t work) - imholynx
One of the scenarios for the development of events is this: make out the basic activation of an abstract and place protected-methods for determining the presence of a network connection, sd card, etc. in it. Then you inherit each activation from this abstract and simply call these methods in the right places. In general, this question is more about OOP and OOD, android does not impose any architectural restrictions on your code. Use normal OOP practices, generics, design patterns, and feel free to
- oneYes, but functions are used not only in activities. About multiple inheritance, as I understood in java, and there is no question. And if for convenience, create a separate class and describe all the functions in it, then create an instance of it in each class and call the necessary methods? - imholynx
- so it should be done - Gorets
- and please forget the word "function" in java, there are methods. - andreich