这个概念我比较糊涂,欢迎哪位来给我上上课。(100)(100分)

  • 主题发起人 Puma Wang
  • 开始时间
P

Puma Wang

Unregistered / Unconfirmed
GUEST, unregistred user!
public class personTest {
class person {
public String Name ;
public int Age ;
public void setName(String sName)
{ Name =sName ;
}
public void setAge(int iAge)
{ Age =iAge ;
}
public void showage()
{
System.out.println("This man's age is : " + Age ) ;
}
public void showName()
{ System.out.println("His(her) name is :" + Name) ;
}
public void person(String sName,int iAge) {
Name =sName ;
Age =iAge ;
}
}
public static void main (String[] args) {
person[] friend =new person[3] ;
friend[0]= new person("puma",22) ;
friend[1]= new person("Fish",24) ;
friend[2]= new person("Robot",32) ;
for( int i=0 ;i<friend.
length();i++)
{ friend.showName() ;
friend.showage() ;
}
}
}
在 JDK1.4 里编译的时候说 这个 personTest.java:27 non-static variable this can not be referenced from a static context ..
谁能告诉我怎么回事? 详细一点更好。
这个概念我比较糊涂,欢迎哪位来给我上上课。
 
main 是静态方法,不能这样访问非静态内部类,你必须通过personTest 实例来访问,
public static void main (String[] args) {
personTest pt = new personTest ();
person[] friend =new person[3] ;
friend[0]= pt.new person("puma",22) ;
friend[1]= pt.new person("Fish",24) ;
friend[2]= pt.new person("Robot",32) ;
for( int i=0 ;i<friend.
length();i++)
{ friend.showName() ;
friend.showage() ;
}
}
或者直接把person声明为静态类,
static class person {
 
在《JAVA 编程思想》这本书中,对这个static有详细的说明,你可以仔细看看~!
 
多人接受答案了。
 
顶部