使用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,必须使用强制转换
例如:一个码头,含有很多船台,希望将一个新船台加入码头中,此类: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,必须使用强制转换