public class test {
test() {
System.out.println("我是构造函数");
}
public static void main(String[] args) {
test t1 = new test();
System.out.print(t1);
}
}
test() {
System.out.println("我是构造函数");
}
这个就是构造函数,也相当于初始化函数,与OC、Swift中的init()方法是等价的
自定义构造函数
public class test {
int ages;
String names;
test(int age,String name) {
System.out.println("我是构造函数");
ages = age;
names = name;
}
public static void main(String[] args) {
// test t1 = new test();
// System.out.print(t1);
test t2 = new test(30, "zhangfei");
System.out.print(t2);
}
}
网友评论