慕粉0956211727
2017-10-15 18:50
父类:
package com.imooc03;
public class Traffictools {
public String way;
public int num;
public void way() {
// TODO Auto-generated method stub
String way1="Bus";
String way2="Ship";
String way3="Plane";
System.out.println("陆地运输方式采用的运输工具是:"+way1);
System.out.println("海洋运输方式采用的运输工具:"+way2);
System.out.println("空中采用的运输工具是:"+way3);
}
public void num() {
// TODO Auto-generated method stub
int num1=40;
int num2=200;
int num3=500;
System.out.println("Bus的承载人数是: " + num1);
System.out.println("Ship的承载人数是: " + num2);
System.out.println("Plane的承载人数是: " + num3);
}
}
子类:
package com.imooc03;
public class Bus extends Traffictools {
public String way1="Car";
@Override
public void way() {
// TODO Auto-generated method stub
super.way();
System.out.println("陆地运输方式采用的运输工具是:"+way1);
}
}
测试类:
package com.imooc03;
public class Test {
public static void main(String[] args) {
Traffictools obj1=new Traffictools();
Bus obj2=new Bus();
obj1.way();
obj1.num();
obj2.way();
}
}
//交通工具类public class Vehicle { String way;//运输方式 int carry;//承载属性 //运输方式 public void transport(){ System.out.println("通过交通工具运输"); }}
//大巴public class Bus extends Vehicle { String way = "陆地运输"; int carry = 40;// 承载人数 // 运输方式 public void transport() { System.out.println("大巴:能搭载"+ carry +"人," + "通过" + way); }}
//飞机public class Plane extends Vehicle { String way = "空中运输"; int carry = 100;// 承载人数 // 运输方式 public void transport() { System.out.println("飞机:能搭载"+ carry +"人," + "通过" + way); }}
//轮船public class Ship extends Vehicle { String way = "海上运输"; int carry = 200;// 承载人数 // 运输方式 public void transport() { System.out.println("轮船:能搭载"+ carry +"人," + "通过" + way); }}
//测试类public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Vehicle v = new Bus(); Vehicle v1 = new Ship(); Vehicle v2 = new Plane(); v.transport(); v1.transport(); v2.transport(); }}
//父类 public class Transport { //num是可载多少人 public int num; public String vehicleName; public String way; public Transport(int num, String vehicleName,String way) { super(); this.num = num; this.way=way; this.vehicleName = vehicleName; } }
没有实现方法的多态
测试类里实现多态要用父类指向子类,Traffictools obj2=new Bus(); 这样写。 你的也没错,只是没有用到多态
//父类
package com.project1;
public class Vehicle {//创建一个交通工具的类;
//里面定义人数和车的类型的两个属性和一个功能的方法;
public int carryNum;
public String models;
public void mode() {
System.out.println("交通工具有运输的功能");
}
public int Num(int newCarryNum) {
carryNum=newCarryNum;
return carryNum;
}
public String Models(String newModels) {
models=newModels;
return models;
}
}
//创建五个子类,里面重写了交通工具的功能
package com.project1;
public class Bike extends Vehicle {
public void mode() {
System.out.println("自行车有陆地运输人的功能!");
}
}
package com.project1;
public class Bus extends Vehicle{
public void mode(){
System.out.println("公共汽车有陆地运输人的功能!");
}
}
package com.project1;
public class Car extends Vehicle {
public void mode(){
System.out.println("小轿车有陆地运输人的功能!");
}
}
package com.project1;
public class Plane extends Vehicle {
public void mode() {
System.out.println("飞机有天上运输人的功能");
}
}
package com.project1;
public class Ship extends Vehicle {
public void mode() {
System.out.println("轮船有水中运输人的功能!");
}
}
//测试了类
package com.project1;
public class Initail {
public static void main(String[] args) {
Vehicle bike=new Bike();
bike.mode();
bike.Num(1);
bike.Models("自行车");
System.out.println("这种交通工具是:"+bike.models+";它能运输:"+bike.carryNum+"人!");
Vehicle bus=new Bus();
bus.mode();
bus.Num(40);
bus.Models("公共汽车");
System.out.println("这种交通工具是:"+bus.models+";它能运输:"+bus.carryNum+"人!");
Vehicle car=new Car();
car.mode();
car.Num(4);
car.Models("小轿车");
System.out.println("这种交通工具是:"+car.models+";它能运输:"+car.carryNum+"人!");
Vehicle ship=new Ship();
ship.mode();
ship.Num(400);
ship.Models("轮船");
System.out.println("这种交通工具是:"+ship.models+";它能运输:"+ship.carryNum+"人!");
Vehicle plane=new Plane();
plane.mode();
plane.Num(200);
plane.Models("飞机");
System.out.println("这种交通工具是:"+plane.models+";它能运输:"+plane.carryNum+"人!");
}
}
我 忘记写那个 交通工具的名字了
父类 public class Transport { String way; int Persons; public void transport(){ System.out.println("运输方式:"+way+",载客量:"+Persons); } } 子类 public class Bus extends Transport { String way="陆地"; int Persons=40; public void transport(){ System.out.println("运输方式:"+way+",载客量:"+Persons); } } public class Ship extends Transport { String way="海洋"; int Persons=200; public void transport(){ System.out.println("运输方式:"+way+",载客量:"+Persons); } } public class Airplane extends Transport { String way="天空"; int Persons=300; public void transport(){ System.out.println("运输方式:"+way+",载客量:"+Persons); } } 测试类 public class Initail { public static void main(String[] args) { // TODO Auto-generated method stub Transport obj1=new Bus(); Transport obj2=new Ship(); Transport obj3=new Airplane(); obj1.transport(); obj2.transport(); obj3.transport(); } }
感觉你这个 是不是 有点麻烦?
没写注释 不知道你哪个变量代表哪个意思
对的,
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题