preface

Constructor: a method with the same class name but no return type. Used to instantiate the current or an object and return the current or an object. If you use the new keyword, you are actually calling the no-parameter constructor. 2. No-parameter constructs are usually used to initialize some values. Parameterized constructs: Once a parameterized construct is defined, it must be shown that a parameterized construct is defined without parameters.

1. The constructor must have the same name as the class (if there are multiple classes in a source file, the constructor must have the same name as the public class) 2. Each class can have more than one constructor 3. Constructors can have zero, one, or more arguments 4. The constructor returns no value. 5. The constructor is always called with the new operationCopy the code

Here is an example of a parameterless construct:

1. Write a Vehicle class. Has: property — Capacity, method :(1) no-parameter constructor (initialize Capacity to 2, and output “Execute no-parameter constructor for the vehicle class.” ); (2) Parameterized constructor (pass parameter to Capacity initialization and print “Execute parameterized constructor for vehicle.” ); (3) Set and GET methods of capacity; (4) Print method: output capacity. (5) Run method: output “this is the vehicle run method” from the console. 2. Create three subclasses of Vehicle: Motor represents car, Ship represents Ship, and Aeroplane represents aircraft. Have their own; Property — Speed, which stands for speed. Method: (1) no-argument constructor (initialize speed to 0 and print “execute no-argument constructor for class XX”.) ); (2) Parameter constructor (super keyword calls parent class parameter constructor, pass parameter to speed initialization, and print “execute XX class parameter constructor.”) (3) speedup: speed+10 and return speed; (4) Speeddown: speed-15 and return to speed; (5) Rewrite the print method: print speed and capacity. (6) Run methods for each of the three classes. Output “the Car is running on the highway”, “the ship is sailing on the sea”, “the plane is flying in the sky”. 3, create two subclasses of Motor, Bus and Car(both final class), respectively represent the Bus and Car, write their respective run methods. Each has an attribute — capacity; Method :(1) no-parameter constructor (initialize capacity to 20 and print “execute no-parameter constructor for class XX.”) (2) parameter constructor (super keyword) parameter constructor (parent), pass parameter to capacity initialization, and output “execute parameter constructor (XX).” (3) Write their own run methods. Similarly, write two subcategories of ships, aircraft carriers and luxury cruise ships. 4. Create a Car object (Car) by calling the main constructor; Call the speed method to increase the speed to 50, call the print method and run method; Call the deceleration method to reduce the speed to 20 and call the print method. Call the argument constructor to create a Bus object Bus; Call the print and run methods. Aircraft carriers and luxury cruise ships, fighter jets and passenger jets. Select one constructor, and call the print and run methods.

Package C. public class Main { public static void main(String[] args) { System.out.println("Auto ->");
        Car car = new Car();
        car.setSpeed(40);
        System.out.println("The initial velocity is 40.");
        System.out.println("Speed up * * * * * * * * * * *");
        car.speedUp();
        car.print();
        car.run();
        System.out.println("Slow down * * * * * * * * * * *");
        car.speedDown();
        car.speedDown();
        car.print();
        System.out.println("* * * * * * * * * * * * * * * *");
        Bus bus = new Bus(10,100);
        bus.print();
        bus.run();
        System.out.println("Boat subclass ->");
        Birdfarm birdfarm = new Birdfarm();
        birdfarm.setSpeed(40);
        System.out.println("The initial velocity is 40.");
        System.out.println("Speed up * * * * * * * * * * *");
        birdfarm.speedUp();
        birdfarm.print();
        birdfarm.run();
        System.out.println("Slow down * * * * * * * * * * *");
        birdfarm.speedDown();
        birdfarm.speedDown();
        birdfarm.print();
        System.out.println("* * * * * * * * * * * * * * * *"); Luxuryship = new Luxuryship(10,100); luxuryship.print(); luxuryship.run(); System.out.println("Aircraft Subclass ->");
        Fighter fighter = new Fighter();
        fighter.setSpeed(40);
        System.out.println("The initial velocity is 40.");
        System.out.println("Speed up * * * * * * * * * * *");
        fighter.speedUp();
        fighter.print();
        fighter.run();
        System.out.println("Slow down * * * * * * * * * * *");
        fighter.speedDown();
        fighter.speedDown();
        fighter.print();
        System.out.println("* * * * * * * * * * * * * * * *"); Aircraft = new Aircraft(10,100); aircraft.print(); aircraft.run(); }}Copy the code

The traffic tools

Package C. public class Vehicle { public int capacity;Vehicle() {
        capacity=2;
        System.out.println("Executes the no-argument constructor for the vehicle class.");
    }
    Vehicle(int capacity) {
        this.capacity=capacity;
        System.out.println("Implements a parametrized constructor for a vehicle.");
    }
    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public void print() {
        System.out.println("Capacity:"+capacity);
    }
    public void run() {
        System.out.println("This is the vehicle run method."); }}Copy the code

Motor vehicles

Package C. public class Motor extends Vehicle { public int speed; publicMotor() {
        speed=0;
        System.out.println("Executes the no-argument constructor for the car class.");

    }
    public Motor(int capacity,int speed) {
        super(capacity);
        this.speed=speed;
        System.out.println("Executes the parameterized constructor for the car class.");
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int speedUp(){
        speed=speed+10;
        return speed;
    }
    public int speedDown() {
        speed=speed-15;
        return speed;
    }
    public void print(){
        System.out.println("Speed:"+speed);
        System.out.println("Capacity:"+capacity);
    }
    public void run(){
        System.out.println("The car is running on the road."); }}Copy the code

The car

Package C. public final class Car extends Motor { int capacity; publicCar() {
        capacity=20;
		System.out.print("Implementation of a parameterless construction method for car classes.");
    }
    public Car(int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("Parametric construction method for implementing car class.");
    }
    public void run() {
        System.out.println("The car speed is:"+speed);		
        System.out.println("Passenger car capacity:"+capacity); }}Copy the code

The bus

Package C. public final class Bus extends Motor { public int capacity; publicBus() {
        capacity=20;
        System.out.println("Executes the no-argument constructor for the bus class.");
    }
    public Bus(int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("Executes the parameterized constructor for the bus class.");
    }
    public void run() {
        System.out.println("Speed:"+speed);
        System.out.println("Capacity:"+capacity); }}Copy the code

The ship class

Package C. public class Ship extends Vehicle { public int speed; // No argument constructor publicShip() {
        speed=0;
        System.out.println("Executes the no-argument constructor for the car class."); } public Ship(int capacity,int speed) {// call super(capacity); this.speed=speed; System.out.println("Executes the parameterized constructor for the car class.");
    }
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int speedUp() {
        speed=speed+10;
        return speed;
    }
    public int speedDown() {
        speed=speed-15;
        return speed;
    }
    public void print() {
        System.out.println("Speed"+speed);
        System.out.println("Capacity"+capacity);
    }
    public void run() {
        System.out.println("The ship is sailing on the sea."); }}Copy the code

The aircraft carrier

Package C. public final class Birdfarm extends Ship{ int capacity; publicBirdfarm () {
        capacity=20;
		System.out.print("Execute the no-argument constructor for the aircraft carrier class.");
    }
    public Birdfarm (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("Parametrized constructors for performing aircraft carrier classes.");
    }
    public void run() {
        System.out.println("Aircraft carrier speed is:"+speed);		
        System.out.println("Carrier vehicle capacity:"+capacity); }}Copy the code

Luxury cruise ship

Package C. public final class Luxuryship extends Ship{ int capacity; publicLuxuryship () {
        capacity=20;
		System.out.print("Implementation of a parameterless construction method for the luxury cruise class.");
    }
    public Luxuryship (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("Parametric construction method for implementing the luxury cruise class.");
    }
    public void run() {
        System.out.println("The speed of a luxury cruise is:"+speed);		
        System.out.println("Luxury cruise capacity:"+capacity); }}Copy the code

The plane class

Package C. public class Aeroplane extends Vehicle { public int speed; // No argument constructor publicAeroplane() {
        speed=0;
        System.out.println("Executes the no-argument constructor for the car class."); } public Aeroplane (int capacity,int speed) {// Call super(capacity); this.speed=speed; System.out.println("Executes the parameterized constructor for the car class.");
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int speedUp() {
        speed=speed+10;
        return speed;
    }
    public int speedDown() {
        speed=speed-15;
        return speed;
    }
    public void print() {
        System.out.println("Speed"+speed);
        System.out.println("Capacity"+capacity);
    }
    public void run() {
        System.out.println("Planes are flying in the sky."); }}Copy the code

fighter

Package C. public final class Fighter extends Aeroplane{ int capacity; publicFighter  () {
        capacity=20;
		System.out.print("Execute the no-argument constructor for the fighter class.");
    }
    public Fighter  (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("Execute a parametrized constructor for the fighter class.");
    }
    public void run() {
        System.out.println("The fighter's speed is:"+speed);		
        System.out.println("Aircraft capacity:"+capacity); }}Copy the code

jet

Package C. public final class Aircraft extends Aeroplane { int capacity; publicAircraft () {
        capacity=20;
		System.out.print("Parametric construction method for performing passenger aircraft classes.");
    }
    public Aircraft (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("Parametric construction method for performing aircraft classes.");
    }
    public void run() {
        System.out.println("The speed of the passenger plane is:"+speed);		
        System.out.println("Passenger capacity:"+capacity); }}Copy the code

The last

Thank you for reading here, after reading what do not understand, you can ask me in the comments section, if you think the article is helpful to you, remember to give me a thumbs up, every day we will share Java related technical articles or industry information, welcome to pay attention to and forward the article!