暖风align
2016-01-14 13:19
父类Vehicle代码:
package com.imooc;
public class Vehicle {
public String way;
public int num;
public String name;
public static void getWay(){
System.out.println("各交通工具的运输路径分别是:");
}
public static void getNum(){
System.out.println("各交通工具的承载人数分别是:");
}
}
子类car代码:
package com.imooc;
public class Car extends Vehicle {
public int num=20;
}
测试类Initailv代码:
package com.imooc;
public class Initailv {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle car=new Vehicle();
car.name="汽车";
car.way="陆地";
car.num=20;
Vehicle ship=new Vehicle();
ship.name="轮船";
ship.way="海洋";
ship.num=200;
Vehicle plane=new Vehicle();
plane.name="飞机";
plane.way="空中";
plane.num=500;
Vehicle.getWay();
System.out.println(car.name+":"+car.way);
System.out.println(ship.name+":"+ship.way);
System.out.println(plane.name+":"+plane.way);
Vehicle.getNum();
System.out.println(car.name+":"+car.num);
System.out.println(ship.name+":"+ship.num);
System.out.println(plane.name+":"+plane.num);
}
}
可以了,但是不够简洁,后面学习到封装,继承和多态会更加简洁的,代码的维护度更强
没有用到封装
倒数第六行和倒数第十行,调用方法要用对象。你用的是类。
写的太复杂了,但是是正确的
Java入门第二季 升级版
530654 学习 · 6091 问题
相似问题