java--面向对象之继承1(公有方法和私有方法)

来源:互联网 发布:4g网络哪家快 编辑:程序博客网 时间:2024/06/06 01:06

Vehicles:

package JC;



public class Vehicles {
/*public String brand;
public String color;

public Vehicles(String brand, String color) {
this.brand = brand;
this.color = color;
}*/

private String brand;
private String color;


public Vehicles(String brand, String color) {
this.brand = brand;
this.color = color;
}
public void run(){
System.out.println(this.color+"的"+this.brand+"已经开动了");

public void showInfo(){
System.out.println("商标:"+this.brand+",颜色 :"+this.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;
}



}

Car :

package JC;


public class Car extends Vehicles {
/*public int seats;

public Car(String brand, String color, int seats) {
super(brand, color);
this.seats = seats;
}*/

private int seats;

public Car(String brand, String color, int seats) {
super(brand, color);
this.seats = seats;
}




public void showCar(){
System.out.println(super.getColor()+"的"+super.getBrand()+"已经开动了"+this.seats);
}



}

Trunk :

package JC;


public class Trunk extends Vehicles{
/*public float load;

public Trunk(String brand, String color, float load) {
super(brand, color);
this.load = load;
}*/

private float load;


public Trunk(String brand, String color, float load) {
super(brand, color);
this.load = load;
}




public void showTrunk(){
System.out.println(super.getColor()+"的"+super.getBrand()+"已经开动了"+this.load);
}


}


Test:

package JC;


import org.junit.Test;




public class jt {
@Test
public void test1(){
/**
* 公有方法
*/
/*Vehicles veh=new Vehicles("雪佛兰", "白色");
Car car=new Car("雪佛兰", "黑色", 7);
Trunk truck=new Trunk("一汽", "蓝色", 70000);

veh.run();
veh.showInfo();
System.out.println(veh.brand);
System.out.println(veh.color);

car.showCar();
car.run();
System.out.println(car.brand);
System.out.println(car.color);
car.showInfo();
System.out.println(car.seats);

truck.showTrunk();
truck.run();
truck.showInfo();
System.out.println(truck.brand);
System.out.println(truck.color);
System.out.println(truck.load);*/

/**
* 私有方法
*/
Vehicles veh=new Vehicles("雪佛兰", "白色");
Car car=new Car("雪佛兰", "黑色", 7);
Trunk truck=new Trunk("一汽", "蓝色", 70000);
car.showCar();
truck.showInfo();
}
}


阅读全文
0 0
原创粉丝点击