- public class F_PersonDemo4 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Person3 p = new Person3("zhangshan",30);
- Person3 p1 = new Person3("lisi",36);
-
- }
- }
- class Person3
- {
- private String name ;
- private int age;
- Person3 ()
- {
- System.out.println("person3 run");
- }
- Person3(String name, int age )
- {
- // 这里并没有用this.name= name. this.age = age进行给本类的成员变量赋值;
- // 为什么打印的结果不是null和0呢?而是带有传递值的结果呢?
- //在前面加上this.即可,如不加,则代表打印的是 Person3(String name, int age )中的name和age;
-
- System.out.println("name="+this.name+" age="+this.age);
- }
- }
复制代码 |