Java 在不创建对象的情况下调用类的方法 班级人员 { 私有字符串lastName; 私有字符串名; 私人互联网; //-------------------------------------------------------------- 公众人物(最后一个字符串,第一个字符串,int a) {//构造函数 lastName=last; firstName=first; 年龄=a; } //-------------------------------------------------------------- 公众人士() { 系统输出打印(“姓氏:”+lastName); 系统输出打印(,名字:“+firstName”); System.out.println(“,Age:+Age”); } //-------------------------------------------------------------- 公共字符串getLast()//获取姓氏 {返回lastName;} }//末级人员 //////////////////////////////////////////////////////////////// 类数据数组 { 私人人员[]a;//对数组的引用 private int nElems;//数据项的数量 公共类数据数组(int max)//构造函数 { a=新人员[max];//创建数组 nElems=0;//还没有项目 } //-------------------------------------------------------------- 公共人物查找(字符串搜索名称) {//查找指定的值 int j; 对于(j=0;j
不,你错了。Java 在不创建对象的情况下调用类的方法 班级人员 { 私有字符串lastName; 私有字符串名; 私人互联网; //-------------------------------------------------------------- 公众人物(最后一个字符串,第一个字符串,int a) {//构造函数 lastName=last; firstName=first; 年龄=a; } //-------------------------------------------------------------- 公众人士() { 系统输出打印(“姓氏:”+lastName); 系统输出打印(,名字:“+firstName”); System.out.println(“,Age:+Age”); } //-------------------------------------------------------------- 公共字符串getLast()//获取姓氏 {返回lastName;} }//末级人员 //////////////////////////////////////////////////////////////// 类数据数组 { 私人人员[]a;//对数组的引用 private int nElems;//数据项的数量 公共类数据数组(int max)//构造函数 { a=新人员[max];//创建数组 nElems=0;//还没有项目 } //-------------------------------------------------------------- 公共人物查找(字符串搜索名称) {//查找指定的值 int j; 对于(j=0;j,java,Java,不,你错了。Person不仅创建了一个,而且创建了3,因为你调用了insert()方法三次。你只需要看看这个 class Person { private String lastName; private String firstName; private int age; //-------------------------------------------------------------- public Person(String last, Stri
Person
不仅创建了一个,而且创建了3
,因为你调用了insert()
方法三次。你只需要看看这个
class Person
{
private String lastName;
private String firstName;
private int age;
//--------------------------------------------------------------
public Person(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}
//--------------------------------------------------------------
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
//--------------------------------------------------------------
public String getLast() // get last name
{ return lastName; }
} // end class Person
////////////////////////////////////////////////////////////////
class ClassDataArray
{
private Person[] a; // reference to array
private int nElems; // number of data items
public ClassDataArray(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public Person find(String searchName)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if( a[j].getLast().equals(searchName) ) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return null; // yes, can't find it
else
return a[j]; // no, found it
} // end find()
//-------------------------------------------------------------- // put person into array
public void insert(String last, String first, int age)
{
a[nElems] = new Person(last, first, age);
nElems++; // increment size
}
//--------------------------------------------------------------
public boolean delete(String searchName)
{ // delete person from array
int j;
for(j=0; j<nElems; j++) // look for it
if( a[j].getLast().equals(searchName) )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // shift down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//--------------------------------------------------------------
public void displayA() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
}
//--------------------------------------------------------------
} // end class ClassDataArray
////////////////////////////////////////////////////////////////
class ClassDataApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ClassDataArray arr; // reference to array
arr = new ClassDataArray(maxSize); // create the array
// insert 10 items
arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Lorraine", 37);
arr.insert("Yee", "Tom", 43);
arr.displayA(); // display items
String searchKey = "Stimson"; // search for item
Person found;
found=arr.find(searchKey);
if(found != null)
{
System.out.print("Found ");
found.displayPerson();
}
else
System.out.println("Can't find " + searchKey);
System.out.println("Deleting Smith, Yee, and Creswell");
arr.delete("Smith"); // delete 3 items
arr.delete("Yee");
arr.delete("Creswell");
arr.displayA(); // display items again
} // end main()
} // end class ClassDataApp
在
insert
方法中创建了一个Person对象。事实上,创建了三个对象,因为它被调用了三次。将方法声明为static检查find(searchKey)的返回类型它是Person对象,以后使用该对象可以调用它的任何方法。我建议您阅读有关java编程的良好教程
public void insert(String last, String first, int age) {
a[nElems] = new Person(last, first, age);// new Person created here
nElems++;
}