编程语言

img yaohao803

vector方法

发表于2004/10/9 10:52:00  833人阅读

分类: J2SE

使用setElement方法象vector中添加新的引用(或者可以理解成成员)。
例如:一个码头,含有很多船台,希望将一个新船台加入码头中,此类:Dock
//Dock.java


package com.tianyu.summer;
import java.util.*;
public class Dock
{
 private int id;
 private String location;
 private boolean electricity;
 private boolean water;
 private Vector slips;
 public Dock(int anId,String aLocation,boolean anElectricity,boolean aWater)
 {
  setId(anId);
  setLocation(aLocation);
  setElectricity(anElectricity);
  setWater(aWater);
 }
 public void addSlipToDock(Slip aSlip)
 {
  slips.addElement(aSlip);//connect dock to slip(1...*) 
 }
 public Vector getSlips()
 {return slips;}
 public void setId(int anId)
 {id=anId;}
 public void setLocation(String aLocation)
 {location=aLocation;}
 public void setElectricity(boolean anElectricity)
 {electricity=anElectricity;}
 public void setWater(boolean aWater)
 {water=aWater;}
 
 public int getId()
 {return id;}
 public String getLocation()
 {return location;}
 public boolean getElectricity()
 {return electricity;}
 public boolean getWater()
 {return water;}
}

Vector.size()用来统计 Vector中含有多少个成员,返回int型
Vector.elementAt( i )将第i个元素返回,返回类型是Object,必须使用强制转换
阅读全文
0 0

相关文章推荐

img
取 消
img