java小车
1. 用java定义一个汽车类Automobile,
classAutomobile
{
privateString发动机型号;
privateint座位数;
publicStringget发动机型号()
{
return发动机型号;
}
publicvoidset发动机型号(String发动机型号)
{
this.发动机型号=发动机型号;
}
publicintget座位数()
{
return座位数;
}
publicvoidset座位数(int座位数)
{
this.座位数=座位数;
}
@Override
publicStringtoString()
{
returnString.format("[发动机型号=%s,座位数=%s]",发动机型号,座位数);
}
}
interface收费
{
publicvoid收费方法();
}
interface调速
{
publicvoid调速方法();
}
class公共汽车extendsAutomobileimplements收费,调速
{
@Override
publicvoid调速方法()
{
System.out.println("速度非常慢");
}
@Override
publicvoid收费方法()
{
System.out.println("天价呢");
}
}
class出租车extendsAutomobileimplements收费,调速
{
@Override
publicvoid调速方法()
{
System.out.println("速度是20公里每小时");
}
@Override
publicvoid收费方法()
{
System.out.println("出租车的收费是便宜的");
}
}
publicclassMain
{
publicstaticvoidmain(String[]args)
{
出租车czc=new出租车();
czc.set发动机型号("abc");
czc.set座位数(4);
System.out.println(czc);
czc.调速方法();
czc.收费方法();
公共汽车ggqc=new公共汽车();
ggqc.set发动机型号("ddf");
ggqc.set座位数(30);
System.out.println(ggqc);
ggqc.调速方法();
ggqc.收费方法();
}
}
2. 请问如何用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();
}
}
运行结果
3. Java编写汽车类car
publicclassCar{
privateintnum;//编号
privateStringname;//型号
privatedoubleprice;//单价
/**
*无参构造
*/
publicCar(){
super();
}
/**
*有参构造
*@paramnum
*@paramname
*@paramprice
*/
publicCar(intnum,Stringname,doubleprice){
super();
this.num=num;
this.name=name;
this.price=price;
}
publicintgetNum(){
returnnum;
}
publicvoidsetNum(intnum){
this.num=num;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
publicStringinforShow(){
return"Car[num="+num+",name="+name+",price="+price+"]";
}
}publicclassPriCarextendsCar{
privateintPersonNum;//最大载客量
publicPriCar(intpersonNum){
super();
PersonNum=personNum;
}
publicPriCar(){
super();
}
publicintgetPersonNum(){
returnPersonNum;
}
publicvoidsetPersonNum(intpersonNum){
PersonNum=personNum;
}
@Override
publicStringinforShow(){
return"PriCar[PersonNum="+PersonNum+"]";
}
}publicclassVanCarextendsCar{
privatedoubleweight;//最大载重
publicVanCar(doubleweight){
super();
this.weight=weight;
}
publicVanCar(){
super();
}
@Override
publicStringinforShow(){
return"PriCar[num="+super.getNum()+",name="+super.getName()+",price="+super.getPrice()+",weight="+weight+"]";
}
}
测试类不想写了应该可以自己写出来了吧
4. 大神们 怎么用java画汽车
1.最简单的方法:
publicstaticStringreverse1(Stringstr)
{
returnnewStringBuffer(str).reverse().toString();
}
2.最常用的方法:
publicstaticStringreverse3(Strings)
{
char[]array=s.toCharArray();
Stringreverse="";//注意这是空串,不是null
for(inti=array.length-1;i>=0;i--)
reverse+=array[i];
returnreverse;
}
3.常用方法的变形:
publicstaticStringreverse2(Strings)
{
intlength=s.length();
Stringreverse="";//注意这是空串,不是null
for(inti=0;i<length;i++)
reverse=s.charAt(i)+reverse;//在字符串前面连接,而非常见的后面
returnreverse;
}
4.C语言中常用的方法:
publicstaticStringreverse5(Stringorig)
{
char[]s=orig.toCharArray();
intn=s.length-1;
inthalfLength=n/2;
for(inti=0;i<=halfLength;i++){
chartemp=s[i];
s[i]=s[n-i];
s[n-i]=temp;
}
returnnewString(s);//知道char数组和String相互转化
}
5. java定义一个汽车类car
你好,很高兴回答你的问题。
题目要求的代码如下:
public class Car{
private String carNumber;
private int speed;
}
如果有帮助到你,请点击点赞。
6. JAVA 建立一个汽车AutoMobile类......
publicclassAutoMobile{
protectedintnumbersOfWheels;
;
protectedintautoMobileWeight;
protectedintspeed;
publicAutoMobile(){
this(4,"黑色",100);
}
publicAutoMobile(intnumbersOfWheels,StringautoMobileColor,intautoMobileWeight){
this.numbersOfWheels=numbersOfWheels;
this.autoMobileColor=autoMobileColor;
this.autoMobileWeight=autoMobileWeight;
this.speed=0;
}
publicvoidspeedUp(){
speed++;
System.out.println("这是AutoMobile的加速方法,当前速度:"+speed);
}
publicvoidspeedDown(){
speed--;
System.out.println("这是AutoMobile的减速方法,当前速度:"+speed);
}
publicvoidstop(){
speed=0;
System.out.println("这是AutoMobile的停车方法,当前速度:"+speed);
}
publicintgetNumbersOfWheels(){
returnnumbersOfWheels;
}
publicvoidsetNumbersOfWheels(intnumbersOfWheels){
this.numbersOfWheels=numbersOfWheels;
}
(){
returnautoMobileColor;
}
publicvoidsetAutoMobileColor(StringautoMobileColor){
this.autoMobileColor=autoMobileColor;
}
publicintgetAutoMobileWeight(){
returnautoMobileWeight;
}
publicvoidsetAutoMobileWeight(intautoMobileWeight){
this.autoMobileWeight=autoMobileWeight;
}
publicintgetSpeed(){
returnspeed;
}
publicvoidsetSpeed(intspeed){
this.speed=speed;
}
}
{
protectedStringairConditioner;
protectedStringcd;
publicCar(){
super();
this.airConditioner="空调A";
this.cd="sony";
}
publicvoidspeedUp(){
speed+=2;
System.out.println("这是Car的加速方法,当前速度:"+speed);
}
publicvoidspeedDown(){
speed-=2;
System.out.println("这是Car的减速方法,当前速度:"+speed);
}
publicStringgetAirConditioner(){
returnairConditioner;
}
publicvoidsetAirConditioner(StringairConditioner){
this.airConditioner=airConditioner;
}
publicStringgetCd(){
returncd;
}
publicvoidsetCd(Stringcd){
this.cd=cd;
}
}
publicclassTest{
publicstaticvoidmain(String[]args){
Carcar=newCar();
car.speedUp();
car.speedUp();
car.speedDown();
car.stop();
}
}
7. JAVA汽车
import java.awt.Color;
class Car {
Color color; //颜色
double price; //价格
String licensePlate; //车牌
public void start() {}
public void stop() {}
}
class BMW extends Car {
Color color = Color.BLACK;
}
不满意的话,随便回改。答
8. 编写java程序:设计一个汽车类
随便给你建了个,代码如下:
package com.ask.test;
public class Car {
//颜色(黑色、白色等)
private String color;
//品牌(奔驰、宝马、东风等)
private String brand;
//类型(越野、普通汽车等)
private String type;
//构造方法
public Car(String color, String brand, String type) {
super();
this.color = color;
this.brand = brand;
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}