package guoke; public class Bus extends Vehicle { //Bus类继承 Vehicle类。在Bus.java我还建了其他Vehicle的子类,car,bicycle类 /*问:为什么通过反复调用不同子类的date方法, name,number,way属性只赋值一次? 我是这样调用的 Bus bus=new Bus(); bus.date(); Car car=new Car(); car.date(); 结果显示全是第一次调用的结果,比如上面这样写的话, 就是显示两次bus的属性。 */ /* 父类这样写的 public class Vehicle { protected String name; protected int number; protected String way; public void date(){ System.out.println("交通工具可以载客"); } } */ protected String name="bus";//属性 protected int number=40; protected String way="land"; protected String name="bus"; protected int number=40; protected String way="land"; public void date() {//显示属性的值 // TODO Auto-generated constructor stub System.out.println("公交汽车可以载客:"); System.out.println(name); System.out.println("载客人数number:"+number); System.out.println(way); } } class Car extends Vehicle { //car子类 protected String name="car"; protected int number=4; protected String way="land"; public void date() { // TODO Auto-generated constructor stub System.out.println("轿车可以载客:"); System.out.println(name); System.out.println("载客人数number:"+number); System.out.println(way); } class Bicycle extends Vehicle { //Bicycle子类 protected String name="bicycle"; protected int number=2; protected String way="land"; public void date() { // TODO Auto-generated constructor stub System.out.println("自行车可以载客:"); System.out.println(name); System.out.println("载客人数number:"+number); System.out.println(way); }
qq___571
风中过客
相关分类