zoukankan      html  css  js  c++  java
  • Java面向对象_对象数组

    今天学习了对象数组,写点东西总结一下。废话不多说,啥是对象数组呢?

    对象数组的概念是这么讲的,对象数组就是数组里的每个元素都是类的对象,赋值时先定义对象,然后将对象直接赋给数组。

    举个例子,使用对象数组实现多个Hero的管理

      1 import java.util.Arrays;
      2 import java.util.Scanner;
      3 
      4 public class Test4 {
      5 
      6     /**
      7      * @param args
      8      */
      9     public static void main(String[] args) {
     10         // TODO Auto-generated method stub
     11         HeroManager hm=new HeroManager();
     12         System.out.println("------增加英雄------");
     13         Heros.print1();
     14         hm.add(new Heros("关羽","男",30));
     15         hm.add(new Heros("张飞","男",31));
     16         hm.add(new Heros("刘备","男",32));
     17         hm.add(new Heros("曹操","男",33));
     18         hm.add(new Heros("孙权","男",34));
     19         hm.list();
     20         System.out.println("------查找英雄------");
     21         Heros q=hm.find(34);
     22         q.print();
     23         System.out.println("------删除英雄------");
     24         hm.delete(34);
     25         hm.list();
     26         System.out.println("------更新英雄------");
     27         hm.update(new Heros("小郝","女",31));
     28         hm.list();
     29     }
     30 
     31 }
     32 //管理类
     33 class HeroManager{
     34     private Heros[] heros=new Heros[3];
     35     private int count=0;
     36     public void add(Heros h){
     37         if(count>=heros.length){
     38             int newLen=heros.length*3/2+1;//一般扩充原来长度的一半
     39             heros=Arrays.copyOf(heros, newLen);
     40         }
     41         heros[count]=h;
     42         count++;
     43         }
     44     public Heros find(int id){
     45         for(int i=0;i<count;i++){
     46             if(heros[i].getId()==id){
     47                 return heros[i];
     48             }
     49         }
     50         return null;
     51         
     52     }
     53     //关羽  张飞  刘备 曹操 
     54     public void delete(int id){     
     55         for(int i=0;i<count;i++){
     56             if(heros[i].getId()==id){
     57                 for(int j=i;j<count-1;j++){
     58                     heros[j]=heros[j+1];
     59                 }
     60                 heros[count-1]=null;
     61                 count--;
     62                 break;
     63             }
     64         }
     65     }
     66     public void update(Heros h1){
     67         Heros m=find(h1.getId());
     68         if(m!=null){
     69             m.setName(h1.getName());
     70             m.setSex(h1.getSex());
     71         }
     72     }
     73     public void list(){
     74         
     75         for(int i=0;i<count;i++){
     76             heros[i].print();
     77         }
     78     }
     79     
     80     
     81     }
     82 
     83 
     84 //英雄类
     85 class Heros{
     86     private String name;
     87     private String sex;
     88     private int id;
     89     public Heros(){
     90         
     91     }
     92     public Heros(String name,String sex,int id){
     93         this.name=name;
     94         this.sex=sex;
     95         this.id=id;
     96     }
     97     public String getName() {
     98         return name;
     99     }
    100 
    101     public void setName(String name) {
    102         this.name = name;
    103     }
    104 
    105     public String getSex() {
    106         return sex;
    107     }
    108 
    109     public void setSex(String sex) {
    110         this.sex = sex;
    111     }
    112 
    113     public int getId() {
    114         return id;
    115     }
    116 
    117     public void setId(int id) {
    118         this.id = id;
    119     }
    120 
    121     public void print(){
    122         System.out.println(name+"	"+sex+"	"+id);
    123     }
    124     public static void print1(){
    125         System.out.println("姓名"+"	"+"性别"+"	"+"排名"+"	");
    126     }
    127     
    128 }
  • 相关阅读:
    JUnit之持续集成(CI,Continuous Integration)
    Junit初级编码(二)探索JUnit核心
    《opencv学习》 之 特征检测与匹配
    opencv小问题大智慧
    opencv3.1+contrib的配置大总结(配置了两天,遇到问题无数)
    《图像处理实例》 之 透视变换
    《图像处理实例》 之 物体计数
    《图像处理实例》 之 操作规则的圆
    《电路学习第三天》 之 彩扩机项目设计
    《图像处理实例》 之 提取特殊背景的直线
  • 原文地址:https://www.cnblogs.com/shenhainixin/p/5059841.html
Copyright © 2011-2022 走看看