java学习(类与对象) 第二更 创建汽车对象 并录入汽车信息

最近学习了java的类与对象。
java面向对象编程的有三大特征:
①封装性
所谓封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者
对象操作,对不可信的进行信息隐藏。简而言之就是,内部操作对外部而言不可见(保护性)
②继承性
继承是指它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展
③多态性
所谓多态就是指一个类实例的相同方法在不同情形有不同表现形式。多态机制使具有不同内部结构
的对象可以共享相同的外部接口。(利用多态可以得到良好的设计)

public class Car{
	private String brand;//车牌属性
	private String color;//颜色属性
	private String performance;//性能属性  S A B 级
	
	
	public Car(String brand){//此方法录入只有车牌信息的车辆
		this.brand = brand;
	}
	public Car(String brand,String color){
		this(brand);//构造方法重载  使代码简洁  避免重复代码
		this.color = color;//只有车牌和颜色的车辆
	}
	
	public Car(String brand,String color,String performance){
		this(brand,color);
		this.performance = performance;//车牌 颜色 性能 全有的车辆
	}
	
	String CarInfo(){
		return "这辆车的品牌是"+brand+",颜色是"+color+",性能等级是"+performance;
	}
	
	String getBrand(){
		return brand;
	}
	
	String getColor(){
		return color;
	}
	
	String getPerformance(){
		return performance;
	}
	
	public void setBrand(String brand){
		this.brand = brand;
	}
	public void setColor(String color){//用set方法 可以修改颜色  性能信息
		this.color = color;
	}
	public void setPerformance(String performance){
		this.performance = performance;
	}
	
	public static void main(String[] args){//录入车辆信息
		Car c = new Car("陕A666","黑色","A等级");
		Car c1 = new Car("陕A667","红色");
		c1.setPerformance("B等级");//修改车辆性能信息
		Car c2 = new Car("陕A668");
		c2.setColor("白色");
		c2.setPerformance("S等级");//修改车辆颜色  性能等级信息
		
		System.out.println(c.CarInfo());
		System.out.println(c1.CarInfo());
		System.out.println(c2.CarInfo());
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43223415/article/details/83989289