开发者

Encapsulating functions in a second .java file?

开发者 https://www.devze.com 2023-01-16 03:03 出处:网络
I\'ve been messing with Android for a couple of weeks, i found many tutorials to follow, but i didnt find anywhere some \"Style rules\" to make the code looks better.

I've been messing with Android for a couple of weeks, i found many tutorials to follow, but i didnt find anywhere some "Style rules" to make the code looks better.

I would like to know if it开发者_如何学Cs possible (im sure that it is, but dont know how to make it xD) to use more .java files to organize the functions. I mean, right now, i have myApp.java where i coded all my application, but is starting to grow so much, so i would like to separate some functions into another .java file.

As i told before, im almost sure that this is possible, but i dont know how to link that second file so, can anybody help me?

Thank you in advance :)


If I understand you correctly you haven't really learned how to use classes in your application? My suggestion is to do a Google search 'Java for beginners' and look for references to Classes and objects.

You normally don't "link" a file in Java as opposed to some other programming languages. In Java you have java files that compile to class files and use them by creating instances of them like so.

MyClass instance = new MyClass();

Where MyClass is defined in a file called MyClass.java (and located in the same package/folder as your main application). If you are unsure about package another Google search can illustrate how to use them.

If you are using Eclipse, it can help you with this. You can create a class and use it by creating a new instance of it in your main application.


You're talking about separation of concerns - you should examine your application design and have classes where the functionality is broken down into logical units per class.

If you're talking about static methods, where you wish to call some functionality which doesn't rely on the state of an object, then perhaps a utility class could be appropriate.

The java.lang.Math is an example, where all the methods on the (final) class are static. Ideally you would just import the methods you'd want to use in your code using the import static keywords.

0

精彩评论

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