JAVA 面向对象( 类和对象)



基本概念:

     面向对象是一种新兴的程序设计方法,或者是一种新的程序设计规范(paradigm),其基本思想是使用对象、类、继承、封装、消息等基本概念来进行程序设计



例题:


<pre>
  1个班有4个小组,每个小组有3学生
  要求:
     1)输入班级名,能查询到其下的所有小组名及每个小组的学生信息,展示如下:
        你查询的班级名为"LOL":
            |—— 第一组,输出组
               |——剑圣
               |—— 剑姬

    |—— 剑豪      

       |—— 第二组,辅助组
               |—— 狗头
               |—— 牛头

    |—— 石头   

     |—— 第三组,中单组
              |—— 卡特
               |—— 卡牌

    |—— 安妮


   |—— 第四组,ADC组
                  |—— 小炮
              |—— 女警

       |—— 薇恩

 

     2)输入指定小组名,能查询到其下的所有学生信息
        你查询的小组名为"ADC组":
            |—— 小炮
            |—— 女警

     |—— 薇恩
    3)输入指定学生名,能查询到器所属的小组信息机班级信息!
        你查询的小组名为"薇恩":所在小组为“ADC组”,所在班级为“LOL”!
  </pre>


 思路:首先找出三个类模型:班级、小组、学生

    其次 写一个主调方法类


班级类


public class Class {
    
    public String name ;
    public Group[] groups;
    
    
    public void queryTeamAndStudentInfo() {
         for (Group group : groups) {
                System.out.println("|—— " + group.name);
                for (Student student : group.students) {
                    System.out.println("\t|—— " + student.name);
                }
            }
    }
    
    public void queryStudentInfo(String studentName) {
         boolean flag = false;
            Group belongTeam = null;
            for (Group team : groups) {
                for (Student student : team.students) {
                    if (student.name.equals(studentName)) {
                        flag = true;
                        belongTeam = team;
                        break;

                    }
                }
            }

            if (flag) {
                System.out.println("你查询的学生名为“" + studentName + "”:所在小组为“" + belongTeam.name + "”,所在班级为“" + name + "”!");
            } else {
                System.out.println("你查询的学生不存在!");
            }
        }




}


小组类

public class Group {
    
    public String name ;
    public Student[] students;
    
     public void queryStudentInfo() {
            for (Student student : students) {
                System.out.println("|—— " + student.name);
            }
        }
    
}


学生类


public class Student {
    
    public String name ;

    
}



主调用方法类:


public class Main {
            
    public Class initData(){
         Student student1 = new Student();
            student1.name = "剑圣";
            Student student2 = new Student();
            student2.name = "剑姬";
            Student student3 = new Student();
            student3.name = "剑豪";
            Student student4 = new Student();
            student4.name = "狗头";
            Student student5 = new Student();
            student5.name = "石头";
            Student student6 = new Student();
            student6.name = "牛头";
            Student student7 = new Student();
            student7.name = "卡牌";
            Student student8 = new Student();
            student8.name = "安妮";
            Student student9 = new Student();
            student9.name = "卡特";
            Student student10 = new Student();
            student10.name = "小炮";
            Student student11 = new Student();
            student11.name = "女警";
            Student student12 = new Student();
            student12.name = "薇恩";
            
            
            Group group1 = new Group();
            group1.name ="输出组";
            group1.students = new Student[] { student1, student2,student3 };
            Group group2 = new Group();
            group2.name = "辅助组";
            group2.students = new Student[] { student4, student5,student6  };
            Group group3 = new Group();
            group3.name = "中单组";
            group3.students = new Student[] { student7, student8,student9  };
            Group group4 = new Group();
            group4.name = "ADC组";
            group4.students = new Student[] { student10, student11,student12  };
            
            Class myClass = new Class();
            myClass.name = "LOL";
            myClass.groups = new Group[] { group1, group2, group3, group4 };

            return myClass;
    }
    
     public static void main(String[] args) {
         Class myClass = new Main().initData();
            // myClass.queryTeamAndStudentInfo();

            // Group group = myClass.teams[0];
            // group.queryStudentInfo();

            myClass.queryStudentInfo("薇恩");
        
        
    }
}





你可能感兴趣的:(类,面向对象,对象)