Java
构造方法:
-
方法名必须与类名相同
-
有多个构造方法时可重载
-
可以选择是否带有参数
-
不能加void修饰
-
权限可以随便
-
用来实例化对象,用new
public class text { public text(String name){ //构造方法 System.out.println("名字是:"+name); } public static void main(String[] args){ text myfrend=new text("amy"); //添加实例化对象 }
-
如果没有构造方法,系统会自动添加
普通方法
- 方法名不可以和类名相同
- 要有void等返回类型