public class A{
//定义属性
private String name;
private int age;
//无参构造函数
public A(){}
//构造函数
public A(String name, int age) {
this.name = name;
this.age = age;
}
}
用的时候,用构造函数赋值:
A a=new A('zhangsan',18);
复制我的代码会有中英文错误哦
public class Teacher
{
String name;
Int age;
//构造函数
public Teacher(String n,Int a)
{
name=n;
age=a;
}
}
下面是在mian中实例化对象时调用构造器的用例
public static void main(String[] args)
{
//下面的Teacher对象通过构造器已经将name和age初始化了
Teacher t=new Teacher("雨",18);
}
private String name;
private int age;
public 类名(String name, int age) {
this.name = name;
this.age = age;
}