实验六 继承

一、实验目的

1.练习类的继承,子类的构造方法。
2. 掌握方法的重写和方法的重载。
3. 掌握this和super的使用。

二、实验类型及课时

验证/设计/综合:验证;课时:2。

三、实验环境

装有JAVA语言工具软件 (Eclipse)的微机若干

四、实验内容

练习知识点:

  • 继承的使用
  • 构造函数的重载
  • 方法的重写
  1. 编码实现子类的构造函数的练习:
    父类“Person”类, “Teacher”类继承“Person”类。要求:
    (1) Person类:私有属性——名字name,籍贯location,构造函数(有参的),info方法(输出成员变量的信息);
    (2) Teacher类:增加“职称“属性,重载构造方法。
    第一个构造方法,只有name和captical两个形参,但是要求教师的籍贯信息为河南;第二个构造方法有三个参数,name,location和captical。
    重写“Person“类的info()方法,增加“职称”信息。
    (3) 创建对象,并输出他们的信息。

  2. 父类Box,子类BoxWeight,计算输出子类对象的体积和重量。具体要求及步骤:
    (1) 定义类Box,具有如下属性字段:宽度(width)、高度(height)、深度(depth);定义构造函数初始化这三个字段;定义一个成员函数volume,计算Box的体积(=widthheightdepth)。
    (2) 定义Box的子类BoxWeight,使其比Box多一个属性字段密度(density);定义构造函数初始化这四个字段;定义一个求其重量的成员方法。
    (3) 定义主类,在主类中声明一个Box对象和一个子类BoxWeight对象,计算输出Box对象的体积,计算输出BoxWeight对象的体积和重量。

  3. 定义一个车的基类Vehicle,含有成员speed,weight。该类派生出自行车类Bicycle,增加high高度成员;派生出汽车类Car,增加seatnum座位数成员。然后,编制测试类,建立Bicycle对象和Car 对象,并输出它们的有关数据。

package class_object;

public class object_inherit {
    public static void main(String[] args) {
        Teacher xiaozhao = new Teacher(name, location,captical);
        Box one_1 = new Box(width,height,depth);
        BoxWeight two_2 =new BoxWeight(width, height, depth, density);
    }
}
class Person{
    private String name;
    private String location;
    public Person(String name,String location){
        this.location=location;
        this.name=name;
    }
    public void info(){
        System.out.println("姓名为"+name);
        System.out.println("籍贯为"+location);
    }
}
class Teacher extends Person{
    private String captical;
    public Teacher(String name,String captical){
        super(name, "河南");
        this.captical= captical;
    }
    public Teacher(String name,String location,String captical){
        super(name, location);
        this.captical=captical;
    }
    public void info(){
        super.info();
        System.out.println("职称");
    }
}
class Box{
    private float width;
    private float height;
    private float depth;
    public Box(float width,float height,float depth){
        this.width= width;
        this.height=height;
        this.depth = depth;
    }
    public float volume(){
        return width*depth*height;
    }
}
class BoxWeight extends Box{
    private float density;
    public BoxWeight(float width, float height, float depth,float density) {
        super(width, height, depth);
        this.density=density;
        //TODO Auto-generated constructor stub
    }
    public float vandt(){
        return "体积"+super.volume()+"重量"+super.volume()*density;
    }

}
class Vehicle{
    private int speed;
    private float weight;
    public Vehicle(double speed,double weigth) {
        this.speed = speed;
        this.weigth = weigth;
    }
    public String info() {
        return "该车速度为:" + speed + "km/h" + "\t该车量为:" + weigth + "kg";

}
class Bicycle extends Vehicle{
    private double high;
    
    public Bicycle(double speed,double weigth,double high) {
        super(speed, weigth);
        this.high = high;
    }
    public String info() {
        return super.info() + "\t该车高度为:" + high + "cm";
    }

}

}
class Car extends Vehicle{
    private int seatbnum;
    public Car(double speed,double weigth,int seatnum) {
        super(speed, weigth);
        this.seatbnum = seatnum;
        
    }
    public String info() {
        return super.info() + "\t该车座位数为:" + seatbnum + "个";
    }

}
public class VehTest {
    public static void main(String[] args) {
        Vehicle v1 = new Vehicle(50, 30);
        Bicycle b1 = new Bicycle(20, 15, 50);
        Car c1 = new Car(90,45,6);
        System.out.println(v1.info());
        System.out.println(b1.info());
        System.out.println(c1.info());

    }
}
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值