继承

烂漫一生 提交于 2019-11-29 03:09:21
  • 定义

父类派生出子类,子类继承父类,子类可以获得父类的属性和方法

  • 使用
  • extends关键字声明子类继承父类
  • super关键字,用于指代父类对象(创建子类对象之前,会先创建父类对象)
  • super()代表父类构造方法,super.方法名()调用父类方法,super.属性名调用父类的属性
  • 重写
  • 发生在父子类中
  • 方法名相同 ,参数列表相同, 返回值类型相同
  • 权限修饰符范围不能比父类小
  • super vs this
  • this代表本类对象的引用,super代表父类对象的引用
  • this()代表本类构造方法,super()代表父类构造方法
  • this和super代表构造方法,只能在构造方法中使用,并且必须是第一行,不能同时存在
package org.jgs1904.entity;

/**
 *  汽车父类
 * @author Renyw
 * @date 2019年11月25日
 */
public class Car {
    //类型
    private String type;
    //品牌
    private String brand;
    //型号
    private String model;
    //价格
    private double price;
    
    
    public Car() {
        super();
    }


    public Car(String type, String brand, String model, double price) {
        super();
        this.type = type;
        this.brand = brand;
        this.model = model;
        this.price = price;
    }
    

    public String getType() {
        return type;
    }



    public void setType(String type) {
        this.type = type;
    }



    public String getBrand() {
        return brand;
    }



    public void setBrand(String brand) {
        this.brand = brand;
    }



    public String getModel() {
        return model;
    }



    public void setModel(String model) {
        this.model = model;
    }



    public double getPrice() {
        return price;
    }



    public void setPrice(double price) {
        this.price = price;
    }

    //载人行驶的方法
    public void travel() {
        System.out.println("载人行驶");
    }

    @Override
    public String toString() {
        return "Car [type=" + type + ", brand=" + brand + ", model=" + model + ", price=" + price + "]";
    }
 
    
}
package org.jgs1904.entity;
/**
 * 轿车子类
 * @author Renyw
 * @date 2019年11月25日
 */
public class SaloonCar extends Car {
    
    //颜色
    private String color;

    public SaloonCar() {
        super();
    }

    public SaloonCar(String brand, String model, double price, String color) {
        super("轿车", brand, model, price);
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public void test() {
        System.out.println("我是轿车");
    }
    @Override
    public void setType(String type) {//不可以改public为private,因为访问修饰符范围与重写冲突
        System.out.println("不可改变类型");
    }

    @Override
    public String toString() {
        return "SaloonCar [color=" + color + ", type=" + getType() + ", brand=" + getBrand() + ", model="
                + getModel() + ", price=" + getPrice() + "]";
    }    
    
    
}

package org.jgs1904.entity;
/**
 * 客车子类
 * @author Renyw
 * @date 2019年11月25日
 */
public class PassengerCar extends Car {
    
    //载客量
    private int busload;

    public PassengerCar() {
        super();
    }

    public PassengerCar(String brand, String model, double price, int busload) {
        super("客车", brand, model, price);
        this.busload = busload;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }
    
    public void test() {
        System.out.println("我是客车");
        
    }

    @Override
    public String toString() {
        return "PassengerCar [busload=" + busload + ", type=" + getType() + ", brand=" + getBrand()
                + ", model=" + getModel() + ", price=" + getPrice() + "]";
    }
 
    
}
package org.jgs1904.entity;
/**
 * 货车子类
 * @author Renyw
 * @date 2019年11月25日
 */
public class Truck extends Car {
    
    //最大载重
    private String maxLoad;

    public Truck() {
        super();
    }

    public Truck(String brand, String model, double price, String maxLoad) {
        super("货车", brand, model, price);
        this.maxLoad = maxLoad;
    }

    public String getMaxLoad() {
        return maxLoad;
    }

    public void setMaxLoad(String maxLoad) {
        this.maxLoad = maxLoad;
    }
    
    public void test() {
        System.out.println("我是货车");
    }

    @Override
    public String toString() {
        return "Truck [maxLoad=" + maxLoad + ", type=" + getType() + ", brand=" + getBrand() + ", model="
                + getModel() + ", price=" + getPrice() + "]";
    }
    
}

package org.jgs1904.demo;

import org.jgs1904.entity.PassengerCar;
import org.jgs1904.entity.SaloonCar;
import org.jgs1904.entity.Truck;

/**
 * 测试汽车类
 * 
 * @author Renyw
 * @date 2019年11月25日
 */
public class Demo03 {
    public static void main(String[] args) {
        
        SaloonCar saloonCar = new SaloonCar("2", "3", 200000.0, "黄色");
        PassengerCar passengerCar = new PassengerCar("5", "6", 300000.0, 50);
        Truck truck = new Truck("8", "9", 400000.0, "10t");
        
        System.out.println("------------------------");
        truck.test();
        System.out.println(truck.toString());
        System.out.println("------------------------");
        passengerCar.test();
        System.out.println(passengerCar.toString());
        System.out.println("------------------------");
        saloonCar.test();
        System.out.println(saloonCar.toString());
        
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!