java构造方法的事情

java构造方法的事情

今天在实验时使用到了构造方法,构造方法没有返回类型,可以有形参,方法名要和类名一致,在对类创建一个对象时可直接对构造方法进行定义

public class Student { 	public String name; 	public int age; 	public Student(String name,int age) { 		// TODO 自动生成的构造函数存根 		this.name=name; 		this.age=age; 	} } //------------------------------------------------ public class Test { 	public static void main(String[] args) { 		Student stu=new Student(asd,20); 		//在创建对象时就对构造方法进行定义了,定义内容就是括号里的内容 		System.out.println(stu.toString()); 	} }