对象数组和静态/非静态方法。
class Foititis //Dimiourgeia twn antikeimenwn typou foititis
{
private String onoma, epitheto;
private short AriMit, EtosEis;
public Foititis (String on, String ep, short AM, short EE)
{
onoma = on;
epitheto = ep;
AriMit = AM;
EtosEis = EE;
}
public String getEpwnymo() //Prosbash sto epitheto apo tis alles klaseis
{
return epitheto;
}
public String toString()
{
String emf;
emf = "--------------------" + "\n";
emf = "Onoma" + onoma + "\n";
emf = "Epwnymo" + epitheto + "\n";
emf = "Arithmos Mitrwoy" + AriMit + "\n";
emf = "Etos Eisagwnis" + EtosEis + "\n";
emf = "--------------------";
return emf;
}
}
class MyUtils01 //Anazitisi Me Epwnymo, Seiriaki Anazitisi
{
public static int AnazitisiMeEpwnymo(Foititis[] a, String key)
{
boolean flag = false;
int j = 0;
return -1;
while ( !flag && j < a.length)
{
if (a[j].getEpwnymo.equals(key))
{
flag = true;
return j;
}
j += 1;
}
}
}
在这个程序中,我有一个数组(名为pinakas)并发送给方法&#34; AnazitisiMeEpwnymo&#34;该课程&#34; MyUtils01&#34;,并重命名为&#34; a&#34; (为了使用)。该数组是&#34; Foititis&#34;类型的对象数组。所以我希望能够从班级&#34; MyUtils01&#34;在属性&#34;上标&#34;,这是私有的,这就是为什么我尝试使用get方法,但我收到error消息。
先谢谢你,我知道我的帖子可能会以某种方式覆盖在另一个帖子上,但没有(从我发现的那些)不使用对象数组,并且没有尝试调用上面的方法。
答案 0 :(得分:2)
你错过了一些括号
package edu.byu.cs.superasteroids.main_menu;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import edu.byu.cs.superasteroids.R;
import edu.byu.cs.superasteroids.base.ActionBarActivityView;
import edu.byu.cs.superasteroids.content.ContentManager;
import edu.byu.cs.superasteroids.database.DBhelper;
import edu.byu.cs.superasteroids.game.GameActivity;
import edu.byu.cs.superasteroids.importer.ImportActivity;
import edu.byu.cs.superasteroids.ship_builder.ShipBuildingActivity;
public class MainActivity extends ActionBarActivityView implements IMainMenuView {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//TODO: Set this activity's controller to an instance of your MainMenuController
//TODO: Pass the MainMenuController's constructor a reference to its IMainMenuView (this)
//IMainMenuController controller = new MainMenuController(this);
//setController(controller);
//TODO: Initialize your database
DBhelper.init(getBaseContext());
ContentManager.getInstance().setResources(getResources());
ContentManager.getInstance().setAssets(getAssets());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public void startGame(View v) {
Intent intent = new Intent(this, ShipBuildingActivity.class);
startActivity(intent);
}
public void quickPlay(View v) {
if (getController() != null) {
((IMainMenuController) getController()).onQuickPlayPressed();
}
}
public void startGame() {
Intent intent = new Intent(this, GameActivity.class);
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP);
this.startActivity(intent);
}
public void importData(View v) {
Intent intent = new Intent(this, ImportActivity.class);
startActivity(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
不是
package edu.byu.cs.superasteroids.importer;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import edu.byu.cs.superasteroids.R;
public class ImportActivity extends ActionBarActivity {
private ListView listView;
private Resources res;
private AssetManager am;
private List<String> fileList;
private IGameDataImporter dataImporter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//TODO: Set the dataimporter to an instance of your GameDataImporter
dataImporter = new GameDataImporter(this);
}
@Override
public void onResume() {
super.onResume();
res = getResources();
am = res.getAssets();
String[] files = null;
try {
files = am.list("");
} catch (IOException e) {
e.printStackTrace();
}
fileList = null;
if(files != null) {
fileList = new ArrayList<String>();
for(String file : files) {
if(file.endsWith(".json"))
fileList.add(file);
}
if(fileList.size() > 0) {
ImportFileAdapter fileAdapter = new ImportFileAdapter(this,
android.R.layout.simple_list_item_1, fileList);
ListView listView = (ListView) findViewById(R.id.import_list);
listView.setAdapter(fileAdapter);
listView.setOnItemClickListener(fileClickListener);
fileAdapter.notifyDataSetChanged();
}
}
}
private AdapterView.OnItemClickListener fileClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast toast = Toast.makeText(ImportActivity.this, "", Toast.LENGTH_LONG);
if(dataImporter == null) {
toast.setText("The importer has not been implemented or created yet...\nThe file was not imported.");
}
else {
try {
boolean success = dataImporter.importData(new InputStreamReader(
new BufferedInputStream(am.open(fileList.get(i)))));
if (success)
toast.setText("The file was imported.");
else
toast.setText("An error occurred as the importer tried to import the file.");
} catch (IOException e) {
e.printStackTrace();
toast.setText("The file could not be imported, because it does not exist.");
}
}
toast.show();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_import, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_import, container, false);
return rootView;
}
}