package forken;
/**
- 构造器(构造方法): 它是一种特殊的方法(没有返回值类型)
*person p=new person(); person()就是构造器
*每个类都会有一个默认的构造方法 如果类中定义一个构造器 那么默认的构造器就失效了
*自定义构造器语法: public 类名(){
}
*构造器怎么调用: new 构造器()
*注意: 构造器的作用是负责初始化工作的(变量。对象) int a=1; person p=new person();
- 初始化变量:
-
}public constrctordemo(String name,int age){ this.name=name; this.age=age;
- 初始化对象:
p=new p();
- @author Administrator
*/
public class constrctordemo {
private String name;
private int age;
private person p;
public constrctordemo(String name,int age){
this.name=name;
this.age=age;
p=new person();
}
/* public constrctordemo(String name){
System.out.println(name+" 这是自定义构造器................");
}
public static void main(String[] args){
constrctordemo cd= new constrctordemo("tom");
}
/*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
constrctordemo cd= new constrctordemo("tom",20);
System.out.println(cd.getName()+"........"+cd.getAge());
}
}