我相对较新的Android编程(约2个月)是否有必要为几十个不同的变量获取?
例如 -
//Yes I realise that this isn't 'dozens'
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public float getRotation() {
return rotation;
}
Run Code Online (Sandbox Code Playgroud)
虽然有必要为浮点数和字符串设置不同的getter和setter,但这是不好的做法,如果是这样,为什么要使用类似switch语句的东西来返回不同的变量?
public float returnSomething(String theThing) {
switch (theThing) {
case "height":
return height;
case "rotation" :
return rotation;
case "width":
return width;
default:
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
那么,上面的代码被认为是坏的吗?如果是这样,请解释原因.
感谢任何帮助,这不是一个真正的问题,因为无论哪种方式都运行正常,我只是不明白为什么人们会使用几十个getter和setter如果没有充分的理由.
我想同样的问题适用于制定者
我有一个模块来提供Retrofit接口.
@Module
class NetModule(val base: String) {
@Provides
@Singleton
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(object: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
info("Request for ${request.url()}")
return chain.proceed(request)
}
}).build()
}
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder()
.enableComplexMapKeySerialization()
.serializeNulls()
.setPrettyPrinting()
.setLenient()
.create()
}
@Provides
@Singleton
fun provideRetrofit(OkHttpClient: OkHttpClient, Gson: Gson): Retrofit {
return Retrofit.Builder()
.baseUrl(base)
.client(OkHttpClient)
.addConverterFactory(GsonConverterFactory.create(Gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.build()
}
@Provides
@Singleton
fun provideIdService(Retrofit: Retrofit): IdService {
return Retrofit.create(IdService::class.java)
}
}
Run Code Online (Sandbox Code Playgroud)
将NetModule …
我有一个包含计算器的片段(只有三个用于监听输入的 TextInputEditTexts)。
这些输入在 RelativeLayout 中列出,如下所示 -
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/binomial_probability_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/label_probability"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/binomial_probability_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberDecimal"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/binomial_trials_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/label_trials"
android:paddingBottom="16dp"
android:layout_below="@+id/binomial_probability_wrapper">
<android.support.design.widget.TextInputEditText
android:id="@+id/binomial_trials_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberSigned"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/binomial_successes_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/label_successes"
android:paddingBottom="16dp"
android:layout_below="@+id/binomial_trials_wrapper">
<android.support.design.widget.TextInputEditText
android:id="@+id/binomial_successes_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberSigned"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我还尝试使用 LinearLayout 将每个输入放在单独的行上,但结果相同。
底部输入被切断。如果我在屏幕上测量它,它显然是不同的尺寸,但 Android 为每个布局和输入的高度返回相同的值。
欢迎任何有关如何解决此问题的建议。
编辑:一些额外的信息。布局边界显示文本光标实际上高于底部 TextInputEditText 的高度。顶部有正确的高度。
第二次编辑:我在下面添加了一个 TextView(用于输出),现在被切断了,而 TextInputEditText 现在和其他的一样。
我有一个公开可见的Int来保存当前的应用程序主题.
var themeId: Int = R.style.AppTheme
private set
Run Code Online (Sandbox Code Playgroud)
如果我使用注释值 @StyleRes
@StyleRes var themeId: Int = R.style.AppTheme
private set
Run Code Online (Sandbox Code Playgroud)
我收到警告
此注释不适用于类型void; 预期的int或long
如果我将变量更改为val,则不会显示错误.我也可以通过添加@JvmField注释来消除错误,但这会删除拥有私有setter的能力.
我是否错误地使用了注释,或者这是Android Studio上Kotlin的问题?
编辑:
吸气剂可以注释
var themeId: Int = R.style.AppTheme
private set
@StyleRes get
Run Code Online (Sandbox Code Playgroud) 在缩短我的代码的同时,我将一些变量声明减少到一行 -
##For example- going from-
Var1 =15
Var2 = 26
Var3 = 922
##To-
Var1, Var2, Var3 = 15, 26, 922
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试对此代码做同样的事情时 -
User_Input += Master_Key[Input_ref]
Key += Master_Key[Key_ref]
Key2 += Master_Key[Key_2_Ref]
##Which looks like-
User_Input, Key, Key2 += Master_Key[Input_Ref], Master_Key[Key_Ref], Master_Key[Key_2_Ref]
Run Code Online (Sandbox Code Playgroud)
这会引发错误
SyntaxError: illegal expression for augmented assignment
Run Code Online (Sandbox Code Playgroud)
我已经阅读了相关的Python文档,但我仍然找不到缩短这一特定代码的方法.
如果我希望对一个字符串执行两次检查,它不是null,并且它不是0长度,我可以这样做 -
if(string != null) {
if(string.length() > 0) {
//Do something
}
}
Run Code Online (Sandbox Code Playgroud)
或者我可以做到这一点
if(string != null && string.length() > 0) {
//Do something
}
Run Code Online (Sandbox Code Playgroud)
首先执行第一次检查,第二次比较不会发生,并且不会抛出NullPointerException.
第二种方法是否可以保证在所有情况下都有效?如果是这样,使用它会被视为不良做法吗?