Java学习笔记——private和继承用法

//子类1
class truck extends Vehicle
{
		private float load;
//private数据的输入
		public float getload()
		{
			return load;
		} 
		public void setload(float load)
		{
			this.load=load;
		}
//定义类的构造函数
		public void showtruck()
		{
			System.out.println("load:"+getload());
            System.out.println("brand:"+getbrand());
            System.out.println("color:"+getcolor());
			System.out.println("RUNNING!\n");
		}
}
//子类2
class car extends Vehicle
{
		private int seats;
		public int getseats()
		{
			return seats;
		} 
		public void setseats(int seats)
		{
			this.seats=seats;
		}
		public void showcar()
		{
			System.out.println("brand:"+getbrand());
            System.out.println("color:"+getcolor());
            System.out.println("seats:"+getseats());
			System.out.println("RUNNING!\n");
		}
}
//父类
class Vehicle
{
	
		private String brand;
		private String color;
		public String getbrand()
		{
			return brand;
		} 
		public void setbrand(String brand)
		{
			this.brand=brand;
		}
		public String getcolor()
		{
			return color;
		} 
		public void setcolor(String color)
		{
			this.color=color;
		}
		public void show()
		{
			System.out.println("brand:"+getbrand());
                                                System.out.println("color:"+getcolor());
			System.out.println("RUNNING!\n");
		}
}
//测试文件
public class testVehicle
{
    public static void main(String[] args)
    {
        Vehicle x1=new Vehicle();
        x1.setbrand("HongQi");
		x1.setcolor("blue");
		x1.show();
		car x2=new car();
		x2.setbrand("BaoShiJie");
		x2.setcolor("pink"); 
		x2.setseats(6);
		x2.showcar();
		truck x3=new truck();
		x3.setbrand("MingJue");
		x3.setcolor("red"); 
		x3.setload(500);
		x3.showtruck();
    }
}

你可能感兴趣的:(java,算法,开发语言)