大学java习题 :定义一个汽车类car
急求!!!!属性包括:汽车品牌颜色和速度(double)并且所有属性为私有。至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。为私有...
急求!!!!属性包括:汽车品牌 颜色 和 速度(double)并且所有属性为私有。 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。 为私有属性提供访问器方法。注意: 汽车品牌一旦初始化之后不能修改。 定义测试类CarTest,在其main方法中创建一个品牌为“benz” 颜色为“black”的汽车
展开
5个回答
展开全部
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
+ "]";
}
}
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
+ "]";
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package Help3;
public class Car {
private String pingpai;
private String color;
private Double sudu;
public Car(String pingpai, String color, Double sudu) {
this.pingpai = pingpai;
this.color = color;
this.sudu = 0.0;
}
public void setPingpai(String pingpai) {
this.pingpai = pingpai;
}
public void setColor(String color) {
this.color = color;
}
public void setSudu(Double sudu) {
this.sudu = sudu;
}
public String getPingpai() {
return pingpai;
}
public String getColor() {
return color;
}
public Double getSudu() {
return sudu;
}
@Override
public String toString() {
return "Car [pingpai=" + pingpai + ", color=" + color + ", sudu="
+ sudu + "]";
}
}
package Help3;
public class CarTest {
public static void main(String[] args) {
Car car = new Car("benz", "black", 0.0);
System.out.println(car.toString());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//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");
}
}
纯手打,可能有编译错误
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");
}
}
纯手打,可能有编译错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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");
}
}
按你的要求写的两个类
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");
}
}
按你的要求写的两个类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询