- Android Programming for Beginners
- John Horton
- 459字
- 2021-07-23 14:47:06
The structure of Java code – revisited
We have already seen that each time we create a new Android project we also create a new Java package, as a kind of container for the code we will write.
We have also learned about and played around with classes. We have imported and taken direct advantage of classes from the Android API such as Log
and Toast
. We have also used the AppCompatActivity
class, but in a different manner to that of Log
and Toast
. You might remember the first line of code in all our projects so far, after the import
statements, used the extends
keyword:
public class MyActivity extends AppCompatActivity {
When we extend a class as opposed to just importing it, we are kind of making it our own. In fact, if you take another look at the line of code you can see that we are making a new class, with a new name, MyActivity
, but basing it on the AppCompatActivity
class from the Android API.
Note
AppCompatActivity
is a slightly modified version of Activity
. It basically provides extra features for older versions of Android that would otherwise not be present. Everything we have discussed about Activity
is equally true for AppCompatActivity
. We will see some more variations on the Activity
class as we progress. It is entirely possible that you have a different class in place of AppCompatActivity
, dependent upon changes that have taken place since this was written. Updates of Android Studio will often change the default Activity
class that it uses when it creates a new project. As long as the name ends in …Activity
it doesn't matter because everything we have discussed and will discuss is equally true
. I will usually just refer to this class simply as Activity
.
In summary, we can import classes to use them, we can extend classes to use them and, very importantly, we will eventually make our own classes.
Tip
The important point
Classes, in their various forms, are the foundation of every single line of code in Java. Everything in Java is, or is part of, a class.
Our own classes and those written by others are the building blocks of our code, and the methods within the classes wrap the functional code, the code that actually does the work.
We can write methods within the classes that we extend, as we did with topClick
and bottomClick
in Chapter 2, Java – First Contact. Furthermore, we overrode methods that are already part of classes written by others, such as onCreate
, onPause
, and so on.
However, the only code that we put in these methods was a few calls using Toast
and Log
. We aren't going to code the next killer app with just that.
- 基于粒計算模型的圖像處理
- Visual C++串口通信技術詳解(第2版)
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- 軟件項目管理實用教程
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- Building an RPG with Unity 2018
- 青少年Python編程入門
- Visual C#.NET程序設計
- Clojure Reactive Programming
- 數據結構與算法分析(C++語言版)
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- 數字媒體技術概論
- HTML5/CSS3/JavaScript技術大全
- Java EE 程序設計