public void Start(String s);
public void stop(String s);
}
class Bike implements Vehicle {
@Override
public void Start(String s) {
Systemoutprintln(s + " bike start");
}
@Override
public void stop(String s) {
Systemoutprintln(s + " bike stop");
}
}
class Bus implements Vehicle {
@Override
public void Start(String s) {
Systemoutprintln(s + " bus start");
}
@Override
public void stop(String s) {
Systemoutprintln(s + " bus stop");
}
}
class interfaceDemo {
public static void main(String[] args) {
Bike bike = new Bike();
bikeStart("My");
bikestop("My");
Bus bus = new Bus();
busStart("My");
busstop("My");
}
}private ArrayList cars;
private ArrayList buses;
public VehicleData()
{
cars=new ArrayList();
buses=new ArrayList();
}
这个是在构造函数中初始化cars和buses集合对象
至于下面的for其实就是通过迭代器遍历集合,调用集合中对象的printCarInfo方法回调的话,服务器是不用能用线程处理的,原因是服务器根本不知道客户端有没有关闭浏览器等等,但是可以通过>
public class Transport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public void manned(String name){
Systemoutprintln(name + "可以载人");
}
}
public class Bus extends Transport {
public void manned(String name){
Systemoutprintln((name + "可以载50人"));
}
}
public class Ship extends Transport {
public void manned(String name){
Systemoutprintln((name + "可以载200人"));
}
}
public class Plane extends Transport {
public void manned(String name){
Systemoutprintln((name + "可以载500人"));
}
}
public class Test {
//测试
public static void main(String[] arg){
Transport b = new Transport();
bsetName("公共汽车");
bmanned(bgetName());
Transport s = new Transport();
ssetName("游轮");
smanned(sgetName());
Transport t = new Transport();
tsetName("飞机");
tmanned(sgetName());
}
}
class Auto {
double speed;
public void start() {
speed++;
Systemoutprintln("汽车启动了,当前速度:" + speed);
}
public void speedUp() {
speed += 2;
Systemoutprintln("汽车加速了,当前速度:" + speed);
}
public void stop() {
speed = 0;
Systemoutprintln("汽车停下了,当前速度:" + speed);
}
}
class Bus extends Auto {
int passanger;
public Bus() {
passanger = 0;
Systemoutprintln("这是一个汽车,当前速度:" + speed + ",当前乘客数:" + passanger);
}
public void gotOn() {
passanger++;
Systemoutprintln("上来一个乘客,当前乘客数:" + passanger);
}
public void gotOff() {
if (passanger > 0) {
passanger--;
Systemoutprintln("下来一个乘客,当前乘客数:" + passanger);
} else {
Systemoutprintln("车里没有乘客,当前乘客数:" + passanger);
}
}
}
public class test {
public static void main(String[] args) {
Bus bus = new Bus();
busstart();
busspeedUp();
busstop();
busgotOn();
busstart();
busspeedUp();
busstop();
busgotOff();
}
}
运行结果:
这是一个汽车,当前速度:00,当前乘客数:0
汽车启动了,当前速度:10
汽车加速了,当前速度:30
汽车停下了,当前速度:00
上来一个乘客,当前乘客数:1
汽车启动了,当前速度:10
汽车加速了,当前速度:30
汽车停下了,当前速度:00
下来一个乘客,当前乘客数:0
参考程序:
public class Student {private String name;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public void goHome(Traffic t){
Systemoutprintln("学生:"+thisname);
trun();
}
public Student(String name) {
thisname = name;
}
}
public abstract class Traffic {
protected double speed;
public void run(){
}
}
public class Bus extends Traffic{
@Override
public void run() {
Systemoutprintln("时速为"+superspeed+"公里每小时的汽车正在嘀嗒嘀嗒的开着");
}
public Bus(double busSpeed) {
superspeed = busSpeed;
}
}
public class Train extends Traffic {
@Override
public void run() {
Systemoutprintln("时速为"+superspeed+"公里每小时的火车正在况且况且的开着");
}
public Train(double trainSpeed) {
superspeed = trainSpeed;
}
}
public class AirPlan extends Traffic {
@Override
public void run() {
Systemoutprintln("时速为"+superspeed+"公里每小时的飞机正在吴屋吴屋的开着");
}
public AirPlan(double airPlanSpeed) {
superspeed = airPlanSpeed;
}
}
public class Test {
public static void main(String[] args) {
Traffic traffic1 = new Bus(40);
Student student = new Student("小明");
studentgoHome(traffic1);
Traffic traffic2 = new Train(120);
Student student2 = new Student("小花");
student2goHome(traffic2);
Traffic traffic3 = new AirPlan(300);
Student student3 = new Student("小红");
student3goHome(traffic3);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)