![](/img/trans.png)
[英]How to conditionally execute many similar repeating lines of code in Java?
[英]How to better randomize method calls in Java (without repeating similar lines)?
我將此文件作為 Android 的寵物應用程序示例,該應用程序展示了貓或狗的個人資料(由 Udemy 課程提供)。 這是基本文件:
package com.delta.generics;
import android.app.Activity;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.service.dreams.DreamService;
import android.util.StringBuilderPrinter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class GenericsActivity extends Activity {
public TextView nameTextView = null;
public TextView descriptionTextView = null;
public RatingBar ratingView = null;
public ImageView portraitView = null;
public Button nextButton = null;
private int currentSelection = 0;
// CatAdapter catAdapter;
AdoptAdapter<Cat> catAdoptAdapter;
// AdoptAdapter<Dog> dogAdoptAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generics);
nameTextView = (TextView) findViewById(R.id.nameTextView);
descriptionTextView = (TextView) findViewById(R.id.descriptionTextView);
ratingView = (RatingBar) findViewById(R.id.ratingView);
portraitView = (ImageView) findViewById(R.id.portraitView);
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNext();
}
});
// commenting this out in favour of AdoptAdapter objects
// catAdapter = new CatAdapter(this,nameTextView,descriptionTextView,ratingView,portraitView);
// catAdapter.set(AdoptData.mCatList.get(0));
catAdoptAdapter = new AdoptAdapter<Cat>(this, nameTextView, descriptionTextView, ratingView, portraitView);
// dogAdoptAdapter = new AdoptAdapter<Dog>(this, nameTextView, descriptionTextView, ratingView, portraitView);
catAdoptAdapter.set(AdoptData.mCatList.get(0));
// dogAdoptAdapter.set(AdoptData.mDogList.get(0));
// mCatList and mDogList is an object already exists in AdoptData.java
}
public void showNext(){
int random = currentSelection;
int animal_random = 0;
animal_random = (int )(Math.random() * 2;
while(random == currentSelection){
//avoid same selection twice.
random = (int )(Math.random() * AdoptData.mCatList.size());
random = (int )(Math.random() * AdoptData.mDogList.size());
}
currentSelection = random;
Cat c = AdoptData.mCatList.get(random);
// Dog d = AdoptData.mDogList.get(random);
// commenting in favour of AdoptAdapter object
// catAdapter.set(c);
catAdoptAdapter.set(c);
// dogAdoptAdapter.set(d);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.generics, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我看到的問題是它的格式化方式,它只能展示 Cat 或 Dog 的個人資料,但不能一起展示,我想弄清楚如何“混合”它們,以便 Cats 和 Dogs 隨機出現按“下一步”按鈕。 所以我想在 Java 和這個文件的上下文中,我想我可以修改showNext()
方法:
public void showNext(){
int random = currentSelection;
int animal_random = 0;
animal_random = (int )(Math.random() * 2);
Log.e("animal_random", String.valueOf(animal_random));
switch(animal_random){
case 0:
while(random == currentSelection){
//avoid same selection twice.
random = (int )(Math.random() * AdoptData.mCatList.size());
}
currentSelection = random;
Cat c = AdoptData.mCatList.get(random);
catAdoptAdapter.set(c);
break;
case 1:
while(random == currentSelection){
//avoid same selection twice.
random = (int )(Math.random() * AdoptData.mDogList.size());
}
currentSelection = random;
Dog d = AdoptData.mDogList.get(random);
dogAdoptAdapter.set(d);
break;
}
}
但我覺得這里的“重復行”太多了。 如果這是在 Python 中,我可能會利用getAttribute()
方法根據字符串匹配確定在 object 中使用哪種方法。 但是,當我嘗試使用假定的 Java 等效getMethod()
時,當我嘗試使用它時,我不斷收到java.lang.NoSuchMethodException
錯誤。
在 Java ... 或者是否需要對這個特定示例進行全面重組?
一旦您選擇了以下兩個替代方案中的任何一個,只需更新您的代碼的 rest,您可能會看到全面的重復減少,因為這些動物將在某個地方有共同點。 :-)
創建一個Animal
接口,讓您可以在給定的情況下訪問動物的常見事物,然后在您的Dog
和Cat
class 中實現該接口。 除非您明確需要以某種方式區分Dog
或Cat
,否則您有問題的代碼可能主要是在談論Animal
。
這種方法的好處之一是,雖然界面將強制執行合同,但它也為您提供了回旋余地,例如,如果您希望Dog
class 能夠包含Cat
不應該包含的內容。 也許它已經做到了?
將您的Dog
和Cat
類合並到一個Animal
class 中,該類帶有一個名為type
或類似名稱的enum
字段。 更少的類(歡呼?),但需要注意的是,狗、貓和您添加的任何未來動物都必須適應相同的數據結構。 根據動物的實際類型實際具有多少重量和意義,這可能是也可能不是一個非常糟糕的主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.