Experiment 6 inheritance

1, Experimental purpose

1. Practice class inheritance and subclass construction methods.
2. Master method rewriting and method overloading.
3. Master the use of this and super.

2, Experiment type and class hours

Verification / design / synthesis: verification; Class hours: 2.

3, Experimental environment

Several microcomputers equipped with JAVA language tool software (Eclipse)

4, Experimental content

Practice knowledge points:

  • Use of inheritance
  • Overload of constructor
  • Method rewriting
  1. Exercise of encoding the constructor of the subclass:
    The parent class is the "Person" class, and the "Teacher" class inherits the "Person" class. requirement:
    (1) Person class: private attributes - name, native place, location, constructor (with parameters), info method (output information of member variables);
    (2) Teacher class: add "professional title" attribute and overload construction method.
    The first construction method has only two formal parameters, name and caption, but requires the native place information of teachers to be Henan; The second construction method has three parameters, name, location and caption.
    Rewrite the info () method of the "Person" class and add the "professional title" information.
    (3) Create objects and output their information.

  2. The parent class Box and the subclass BoxWeight calculate and output the volume and weight of the subclass object. Specific requirements and steps:
    (1) Define the class Box with the following attribute fields: width, height and depth; Define the constructor to initialize these three fields; Define a member function volume to calculate the volume of the Box (= widthheightdepth).
    (2) Define BoxWeight, a subclass of Box, so that it has one more attribute field density than Box; Define the constructor to initialize these four fields; Define a member method to find its weight.
    (3) Define the main class, declare a Box object and a subclass BoxWeight object in the main class, calculate the volume of the output Box object, and calculate the volume and weight of the output BoxWeight object.

  3. Define the base class Vehicle of a Car, which contains members speed and weight. This class derives Bicycle class, which adds high height members; Derived the Car class and increased the number of seatnum seats. Then, compile the test class, establish the Bicycle object and Car object, and output their relevant data.

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 is"+name);
        System.out.println("Native place"+location);
    }
}
class Teacher extends Person{
    private String captical;
    public Teacher(String name,String captical){
        super(name, "Henan");
        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("title");
    }
}
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 "volume"+super.volume()+"weight"+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 "The speed of the car is:" + speed + "km/h" + "\t The volume of this vehicle is:" + 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 The height of the car is:" + 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 The number of seats in the car is:" + seatbnum + "individual";
    }

}
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());

    }
}

Keywords: Java Class

Added by johnb352 on Tue, 01 Feb 2022 10:45:27 +0200