public class Automobile {
private String brand;// 品牌
private String color;// 颜色
private double emissions;// 排量
private int price;// 价格
private int passengers;// 载客人数
public Automobile(){
System.out.println("父类Automobile的无参构造");
}
public Automobile(String brand, String color, double emissions, int price, int passengers) {
super();//调用父类(Object)的无参构造方法
this.brand = brand;
this.color = color;
this.emissions = emissions;
this.price = price;
this.passengers = passengers;
}
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 double getEmissions() {
return emissions;
}
public void setEmissions(double emissions) {
this.emissions = emissions;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getPassengers() {
return passengers;
}
public void setPassengers(int passengers) {
this.passengers = passengers;
}
//收费方法
public void getCharge(){
System.out.println("根据具体情况收费");
}
//重写Object类的toString方法
//为了返回子类中具体的属性信息
public String toString(){
return this.brand+","+
this.color+","+
this.emissions+","+
this.price+","+
this.passengers;
}
}
public class Car extends Automobile{
public Car(){
//super();//调用父类的无参构造
}
public void getCharge(){
System.out.println("7元起步,每公里1.5元"); //重写
//super.getCharge();//调用父类的成员方法
}
public void start(){
System.out.println("小型汽车启动……");
}
}
public class Bus extends Automobile{
public void getcharge(){
System.out.println("现金2元,打卡8折"); 重写
}
}
public class TestCar {
public static void main(String[] args) {
//Car mycar1 = new Car();
// mycar1.setBrand("大众");
// mycar1.setColor("黑色");
// mycar1.setEmissions(1.8);
// mycar1.setPrice(110000);
// mycar1.setPassengers(5);
// System.out.println(mycar1);
// mycar1.getCharge();//子类对象调用的是子类中的方法
// System.out.println(mycar1.toString());
//mycar1.getCharge();
// Automobile myauto = new Automobile();
// Car mycar = new Car();
Automobile auto = new Car();
auto.getCharge();
System.out.println(auto.getClass().getName());
System.out.println(auto.getClass().getMethods()[0]);
}
}
总结:
1.类与类之间的继承
2.方法的重写 可以写父类的构造成员方法 具体方法在子类中重新定义
3.多态总结
4.super用法
5.object类