大学java习题 :定义一个汽车类car
发布网友
发布时间:2022-04-24 19:04
我来回答
共5个回答
热心网友
时间:2022-04-29 23:10
public class CarTest {
public static void main(String[] args) {
Car car =new Car("benz","black");
System.out.println(car);
}
}
public class Car {
public Car() {
}
public Car(String brand, String color) {
this.brand=brand;
this.color=color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
private String brand;
private String color;
private Double speed = 0.0;
@Override
public String toString() {
return "Car [brand=" + brand + ", color=" + color + ", speed=" + speed
+ "]";
}
}
public class Car {
public Car() {
}
public Car(String brand, String color) {
this.brand=brand;
this.color=color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
private String brand;
private String color;
private Double speed = 0.0;
@Override
public String toString() {
return "Car [brand=" + brand + ", color=" + color + ", speed=" + speed
+ "]";
}
}
热心网友
时间:2022-04-30 00:28
package Help3;
public class Car {
private String pingpai;
private String color;
private Double su;
public Car(String pingpai, String color, Double su) {
this.pingpai = pingpai;
this.color = color;
this.su = 0.0;
}
public void setPingpai(String pingpai) {
this.pingpai = pingpai;
}
public void setColor(String color) {
this.color = color;
}
public void setSu(Double su) {
this.su = su;
}
public String getPingpai() {
return pingpai;
}
public String getColor() {
return color;
}
public Double getSu() {
return su;
}
@Override
public String toString() {
return "Car [pingpai=" + pingpai + ", color=" + color + ", su="
+ su + "]";
}
}
package Help3;
public class CarTest {
public static void main(String[] args) {
Car car = new Car("benz", "black", 0.0);
System.out.println(car.toString());
}
}
热心网友
时间:2022-04-30 02:03
public class Car {
private String brand;
private String color;
private double speed;
public Car(String _brand, String _color) {
brand = _brand;
color = _color;
speed = 0;
}
public String getBrand(){
return brand;
}
public String getColor(){
return color;
}
public void setColor(String newcolor){
color = newcolor;
}
public double getSpeed(){
return speed;
}
public void setSpeed(double newspeed){
speed = newspeed;
}
}
public class CarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car myCar = new Car("Benz", "black");
System.out.println(myCar.getBrand());
}
}
注意class Car放到Car.java里,class CarTest放到CarTest.java里
热心网友
时间:2022-04-30 03:54
//car类文件
Class Car{
private String brand;
private String color;
private double speed;
public Car(String brand,String color){
this.brand=brand;
this.color=color;
this.speed=0.0d;
}
public String getBrand(){
return this.brand;
}
public String getColor(){
return this.color;
}
public setColor(String color){
this.color=color;
}
public double getSpeed(){
return this.speed;
}
public setSpeed(double speed){
this.speed=speed;
}
}
//主类文件
Class CarTest{
public static void main(String[] args){
Car car=new Car("benz","black");
}
}
纯手打,可能有编译错误
热心网友
时间:2022-04-30 06:02
public class Car {
private final String brand;
private final String color;
private Double speed;
Car (String brand , String color ) {
this.brand = brand;
this.color = color;
this.speed = 0d;
}
public static void main(String args[]){
}
}
public class TestCar {
public static void main(String args[]){
Car car = new Car("benz", "black");
}
}
按你的要求写的两个类