您好我是编程新手,我正在尝试在Android中创建简单的闹钟应用程序可以帮助我理解这段代码
if len(s) > 0 and s[0] == '*': ...
if s[:1] == '*': ...
更详细的代码是here
答案 0 :(得分:0)
在Android编程中保存指向变量this
的指针是常见的:
它将在以后被其他无法访问this
的类使用(因为他们有一个"不同的" this
),而他们可能仍然需要访问上下文(活性)。
答案 1 :(得分:0)
The code you posted is an example of the Singleton pattern. And here goes a quote from wikipedia
...the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
Normally this pattern requires you to make the object's constructor private but that's kinda impossible with Android activities.
By doing that
inst = this;
the activity "stores itself" in a variable that will be later returned by the getInstance() method. Using this patter will allow other classes (other activities, I guess) use this activity by simply calling AlarmActivity.getInstance()...which can be beneficial in some cases.