package mypackage.test;
class Person
{
public String name="Unknown";
public int age;
public Person()
{
System.out.println("1)调用了构造器1!");
System.out.println("name is "+name);
}
public Person(String n)
{
name=n;
System.out.println("2)调用了构造器2!");
System.out.println("name is "+name);
}
public Person(String n,int a)
{
name=n;
age=a;
System.out.println("3)调用了构造器3!");
System.out.println("name is "+name+" and age is "+age);
}
public void shout()
{
System.out.println("listen to me!");
}
}
public class TestPerson
{
public static void main(String args[])
{
Person p1=new Person();
p1.shout();
Person p2=new Person("张三");
p2.shout();
Person p3=new Person("张四",25);
p3.shout();
}
}
运行时报如图错误
但是如果把package语句去掉就对的。