是一个特殊的方法
和类名相同
没有返回值
可以重载
1.2 作用
初始化
给成员变量赋值
1.3 实例
public class Test6
{
public static void main(String[] args)
{
Person per = new Person();
/*
per.pid = 1;
per.name = "tom";
per.age = 20;
*/
per.display();
Person per2 = new Person(2,"geek99",20);
per2.display();
}
}
class Person
{
public Person()
{
System.out.println("Person");
pid = 0;
name = "无名氏";
age = 0;
}
public Person(int pid,String name,int age)
{
this.pid = pid;
this.name = name;
this.age = age;
}
int pid;
String name;
int age;
void display()
{
String msg = pid+","+name+","+age;
System.out.println(msg);
}
}
原文出处:http://geek99.com/node/412#
该博客教程视频地址:http://geek99.com/node/1608