
import java.util.*;
class car{
private int passengers;
private double weight;
private String color;
public void input(){
Scanner sin=new Scanner(System.in);
System.out.println("请输入汽车的载客人数:");
passengers=sin.nextInt();
System.out.println("请输入汽车的重量(吨):");
weight=sin.nextDouble();
System.out.println("请输入汽车的颜色:");
color=sin.next();
}
public int getPassengers() {
return passengers;
}
public void setPassengers(int passengers) {
this.passengers = passengers;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class lx2 {
public static void main(String[] args){
car a1=new car();
System.out.println("请输入汽车对象的属性值:");
a1.input();
System.out.println("请输入第一辆车对象的信息如下:");
System.out.println("汽车的载客人数为:"+a1.getPassengers()+";重量为:"+a1.getWeight()+"(吨);颜色为:"+a1.getColor());
car a2=new car();
a2.input();
System.out.println("请输入汽车对象的属性值:");
System.out.println("请输入第一辆车对象的信息如下:");
System.out.println("汽车的载客人数为:"+a2.getPassengers()+";重量为:"+a2.getWeight()+"(吨);颜色为:"+a2.getColor());
}
}
这个代码对吗?谢谢!