java中;在构造函数中定义的变量,为什么一般函数可以用???
publicclassDemo{publicstaticvoidmain(String[]args){Personp=newPerson();p.speak();}}cl...
public class Demo {
public static void main(String[] args) {
Person p=new Person();
p.speak();
}
}
class Person {
private String name;
private int age;
//String name;
//int age;
Person() {
name="baby";
age=1;
System.out.println("Person run");
}
public void speak() {
System.out.println(name+"...."+age);
}
}
控制台打印结果如下:
/*Person run
baby....1*/ 展开
public static void main(String[] args) {
Person p=new Person();
p.speak();
}
}
class Person {
private String name;
private int age;
//String name;
//int age;
Person() {
name="baby";
age=1;
System.out.println("Person run");
}
public void speak() {
System.out.println(name+"...."+age);
}
}
控制台打印结果如下:
/*Person run
baby....1*/ 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询