java object array basics

 topic:

  
    Topic:
   Defining an array to store 3 Student object
   arrays has a disadvantage: once created, the length of the program cannot be changed during the run

		//数组对象
public class Student {
	
	private String name;
	 private int age;
	public Student() {System.out.println();
	}
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}
public class Test01 {

	public static void main(String[] args) {
		//s首先创建一个长度为3的数组,;面用来存放person类型对象
		Student array[]=new Student[3];
		Student one=new Student();//创建一个无参数
		System.out.println();//地址值
		
		
		Student two=new Student("小明",16);
 		Student three=new Student("小红",17);
		//将one当中的地址值赋值到数组的0号元素
 		array[0]=one;
		array[1]=two;
		array[2]=three;
		
		System.out.println(array[0]);//地址值
		System.out.println(array[1]);//地址值
		System.out.println(array[2]);//地址值
		
		
		System.out.println(array[0].getName());//null
		System.out.println(two.getName());
		
		System.out.println(array[2].getAge());//17
		}

}

Study notes

Published 10 original articles · praised 0 · visits 89

Guess you like

Origin blog.csdn.net/Q791425037/article/details/105637305