Java基础003

版权声明: https://blog.csdn.net/qq_40905403/article/details/83821683


到此为止,我们知道怎么去创建对象和给成员变量赋初值了

创建对象并给变量赋初值—构造器

public class Student {
	String name;
	int age;
	String sex;
	Student(String name,int age,String sex){
		this.name = name;
		this.age = age;
	}
	/**
	* 更新属性
	*/
	void update(String name,int age) {
		this.name = name;
		this.age = age;
	}
}
public class test {
	public static void main(String[] args){
		Student s = new Student("张君瑞",23,"阳泉平定");
		s.update("成小丽",23);
	}
}

引用数组

将多个对象放到一个数组里,方便操作

public class test {
	public static void main(String[] args){
		Student[] stus = {
			new Student("张君瑞",23,"阳泉平定"),
			new Student("成小丽",23,"阳泉平定")
		};
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40905403/article/details/83821683
003