如何在Java中更好地随机化方法调用(不重复类似的行)?
我将此文件作为Android宠物应用程序的一个示例,该应用程序展示猫或狗的个人资料(由我的课程提供)。这是基本文件:如何在Java中更好地随机化方法调用(不重复类似的行)?,java,android,android-studio,Java,Android,Android Studio,我将此文件作为Android宠物应用程序的一个示例,该应用程序展示猫或狗的个人资料(由我的课程提供)。这是基本文件: 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.servi
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);
}
}
但我觉得这里的“重复台词”太多了。如果是在Python中,我可能会利用getAttribute()
方法根据字符串匹配确定在对象中使用哪个方法。然而,当我尝试使用假定的Java等价物getMethod()
时,当我尝试使用它时,我不断得到一个Java.lang.NoSuchMethodException
错误
在Java中有没有更好的方法来实现这一点。。。?或者,对于这个特定的例子,它是否需要一个完整的重组?一旦您选择了下面两个备选方案中的任何一个,只需更新代码的其余部分,您可能会看到更少的重复,因为动物在某处会有一个共同点。:-) 创建一个
Animal
界面,让您可以访问给定案例中动物的常见内容,然后在您的Dog
和Cat
类中实现该界面。你所讨论的代码可能主要是关于动物的,除非你明确需要以某种方式区分狗或猫
这种方法的一个优点是,虽然接口将强制执行契约,但如果您希望Dog
类能够包含Cat
不应该包含的内容,它也为您提供了回旋空间。也许已经有了
枚举方式
将您的
Dog
和Cat
类合并为一个Animal
类,该类包含一个名为type
或类似内容的enum
字段。更少的类(万岁?),但需要注意的是,狗、猫和任何你未来添加的动物都必须适应相同的数据结构。根据动物的实际体重和意义,这可能是一个很坏的主意,也可能不是一个很坏的主意。这个操作有一个很有用的名称:用多态性替换条件。我不知道,@chrylis cautiouslyoptimistic-,谢谢!我们已经学到了一些新东西,现在才过午夜6分钟。:-)@谢谢你的建议!为了进一步扩展,请参考我在github中上传的这个项目:尽管我的猫/狗类已经实现了一个接口,但我主要关心的是猫和狗之间列表对象名称的差异。你会如何重新构造它,使应用程序可以选择查看猫、狗或两者。。。?
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;
}
}