死了好多脑细胞
package com.bj.array; /**the steps of creating a new "Class" * Step 1: to creat a new Class * Using the "private" to encapsulate the "Object" *Step 2:All+Insert调用Getter and Setter *Step 3:All+Insert调用Constructor *Step 4:to set a null of this "Class" *Step 5:to set a entrance of method, then print it out *There are 4 subjects in this exercise:Java, Python, Bigdata, AI * entrance: public static void main(String[] args){ * to use 3 initialization method to print it * 1.Default initialization * 2.Static initialization * 3.Dynamic initialization * } * to use a for(){} * Step 6:to add a override:keyboard "toString" between the "Step 3:Constructor"and "Step 2:Getter and setter" */ public class exercise { // There are 4 subjects in this exercise:Java, Python, Bigdata, AI public static void main(String[] args) { /* 1.Static initialization */ int[] i = {60, 70, 80, 90};//静态初始化基本类型数组 Exam[] exams = {//给subject数组分配空间和赋值 new Exam(60, "java"), new Exam(70, "python"), new Exam(80, "bigData"), new Exam(90, "AI") }; //做一个强化for循环的打印,需要在步骤3和步骤2之间加上一个步骤6:Step 6 for (Exam exam : exams) { System.out.println(exam); } //2.Dynamic initialization int[] e1 = new int[4];//动态初始化数组,先分配空间,并说明一共有4门科目 Exam[] exams1 = {//给subject数组分配空间和赋值 new Exam(60, "java"), new Exam(70, "python"), new Exam(80, "bigData"), new Exam(90, "AI")}; for (int j = 0; j < 4; j++) { System.out.println(exams1[j]);//尝试了getScore() and getSubject()反而只能打印出一半,所以就直接写 //exams1[j]全部打印,就出分数和科目名称了 } //3.Default initialization int score[] = new int[4];//默认值0,0 String[] subject = new String[4];//默认值null,null Exam[] exams2 = {//给subject数组分配空间和赋值 new Exam(60, "java"), new Exam(70, "python"), new Exam(80, "bigData"), new Exam(90, "AI")}; for(Exam exam3:exams2){ System.out.println(exam3); } } //Step 1:to creat a class and to private package static class Exam { private int score;//score private String subject;//my subject //Step 4: to set a null of this "Class" public Exam() { } //Step 3: to use the keyboard :all+insert to add "Constructor" public Exam(int score, String subject) { this.score = score; this.subject = subject; } //step 6:to override:toString() @Override public String toString() { return "Exam{" + "score=" + score + ", subject='" + subject + '\'' + '}'; } //Step 2:to use the keyboard:all+insert to add "Getter and Setter" public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } } }