关于java问题
来源:百度知道 编辑:UC知道 时间:2024/06/27 08:49:43
class Person
{
public Person()
{
}
String name;
int age;
class birthday
{
int year;
int month;
int day;
}
}
class TestPerson
{
public static void main(String [] args)
{
Person p1 = new Person();
}
}
怎么才能访问birthday中的变量
{
public Person()
{
}
String name;
int age;
class birthday
{
int year;
int month;
int day;
}
}
class TestPerson
{
public static void main(String [] args)
{
Person p1 = new Person();
}
}
怎么才能访问birthday中的变量
只要加入一条语句就可以了
birthday bd=new birthday();
代码如下:
class Person
{
public Person()
{
}
String name;
int age;
birthday bd=new birthday();
class birthday
{
int year;
int month;
int day;
}
}
class TestPerson
{
public static void main(String [] args)
{
Person p1 = new Person();
System.out.println(p1.bd.year);
}
}
你可以在person中定义公共接口来访问,因为你在建立person对象的时候,birthday类并没有实例对象建立,所以需要单独建立对象,不过不推荐你这样写。