java中构造方法主要用于帮助新建的对象赋初值
class Person { public Person() { System.out.println("public Person"); } } public class way { public static void main(String args[]) { Person P=new Person(); }}
public Person
构造方法的重载
class Person { private int age; private String name; public String talk() { return("我是:" +name+",今年:"+age+"岁"); } public Person(String n,int a) { name=n; age=a; System.out.println("public Person(String n,int a)"); } } public class way { public static void main(String args[]) { Person p=new Person("张三",25); System.out.println(p.talk()); }}
public Person(String n,int a)我是:张三,今年:25岁