构造器
-
分为有参构造和无参构造,有参构造有形参,无参构造无形参;需要注意的是其命名必须符合:public+类名;
-
new对象的时候首先会调用构造器;
-
关于this,作用于当前的类,其本质为指针,可修改类中的属性值;
-
类内部main方法仍然可以new类本身;
-
main方法调用其他方法时,对于非static类型,需要类名.方法名;static类型的方法,可以直接用方法名调用。
public class Constrator { int a; String name; public static void main(String[] args) { Constrator constrator = new Constrator(); constrator.change("panghu");//对于非static类型,需要类名.方法名 imply("yinpp");//static类型的方法,可以直接用方法名调用 } //has parameter public Constrator(int a, String name) { this.a = a; this.name = name; } //no parameter public Constrator() { System.out.println("here,virtual world is for you"); System.out.println("you have been sucessful"); } public void change(String str){ System.out.println("name:"+str); } public static void imply(String str){ System.out.println("his wife:"+str); } }