java新增车辆的代码,java 机动车
Java编写汽车类car
public class Car {
创新互联-专业网站定制、快速模板网站建设、高性价比峡江网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式峡江网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖峡江地区。费用合理售后完善,十余年实体公司更值得信赖。
private int num;//编号
private String name;//型号
private double price;//单价
/**
* 无参构造
*/
public Car(){
super();
}
/**
* 有参构造
* @param num
* @param name
* @param price
*/
public Car(int num, String name, double price) {
super();
this.num = num;
this.name = name;
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String inforShow() {
return "Car [num=" + num + ", name=" + name + ", price=" + price + "]";
}
}
public class PriCar extends Car{
private int PersonNum;//最大载客量
public PriCar(int personNum) {
super();
PersonNum = personNum;
}
public PriCar() {
super();
}
public int getPersonNum() {
return PersonNum;
}
public void setPersonNum(int personNum) {
PersonNum = personNum;
}
@Override
public String inforShow() {
return "PriCar [PersonNum=" + PersonNum + "]";
}
}
public class VanCar extends Car {
private double weight;//最大载重
public VanCar(double weight) {
super();
this.weight = weight;
}
public VanCar() {
super();
}
@Override
public String inforShow() {
return "PriCar [num=" + super.getNum() + ", name=" + super.getName() + ", price=" + super.getPrice() +",weight=" + weight + "]";
}
}
测试类不想写了 应该可以自己写出来了吧
用java创建一个汽车类(Car),为其定义两个属性:颜色和型号,为该类创建两个构造方法
如下:
public class Car {
private String brandName ; // 汽车牌子
private int color; // 颜色 0:红 1:黄 2:兰 ...
public Car( String brandName, int color ){
this.brandName = brandName;
this.color = color;
}
public void move( String direction, int meters ){
System.out.println("牌子为"+ brandName + "的汽车向"+ direction + "移动了"+meters+"米.");
}
public static void main(String[] args){
Car car = new Car( "BMW", 1 );
car.move("东北", 100 );
}
介绍
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
Java建立一个汽车(Car)类,包括品牌(String car_brand)、速度(double car_speed)两个成员变量具体看图
这个代码还是比较简单,下面的代码可以参考
class Car{
private String car_brand;
private double car_speed;
public Car(){ }
public Car(String brand, double speed){
this.car_brand = brand;
this.car_speed = speed;
System.out.println(this.car_brand + "汽车正以时速"
+ this.car_speed + "公里的速度行驶");
}
public void speedUp(String brand){
System.out.println(brand + "汽车正在加速行驶");
}
public void speedUp(String brand, double speed){
System.out.println(brand + "汽车正以时速"
+ speed + "公里的速度加速行驶");
}
public void speedDown(String brand){
System.out.println(brand + "汽车正在减速行驶");
}
public void speedDown(String brand, double speed){
System.out.println(brand + "汽车正以时速"
+ speed + "公里的速度减速行驶");
}
}
class Test{
public static void main(String[] args){
Car car = new Car("宝马", 100.5);
car.speedUp("法拉利");
car.speedUp("法拉利", 280.5);
car.speedDown("法拉利");
car.speedDown("法拉利", 120.9);
}
}
请问如何用Java编写一个汽车类Car
public class Car {
private String color;//颜色
private int door;//车门数量
private float speed;//车速
public Car(){
this.color = "红色";
this.door = 3;
this.speed = 110;
}
public Car(String color, int door, float speed) {
this.color = color;
this.door = door;
this.speed = speed;
}
public void start(){
//汽车启动。输出汽车已启动,并输出汽车的各个属性
System.out.println("汽车已启动,汽车颜色为"+color+",车门数为"+door+",车速为"+speed);
}
public void speedUp(float speed){
//加速
System.out.println("汽车加速到"+speed+"km/h");
}
public void shutDown(float speed){
//减速
System.out.println("汽车减速到"+speed+"km/h");
}
public void brake(){
//刹车
System.out.println("已刹车");
}
}
public class Test {
public static void main(String[] args){
Car car = new Car();
car.start();
car.speedUp(100);
car.shutDown(60);
car.brake();
Car car1 = new Car("白色",4,20.2F);
car1.start();
car1.speedUp(100);
car1.shutDown(60);
car1.brake();
}
}
运行结果
本文名称:java新增车辆的代码,java 机动车
文章地址:http://azwzsj.com/article/dsscico.html