Week Seventh Java jobs

Source: Internet
Author: User

Topic: Define a Vehicle Class (Vehicle) in the package Bzu.aa:

Properties--Carrying capacity (capacity)

Method

The parameterless construction method (capacity Initializes a value of 2, and outputs "the parameterless construction method of the execution vehicle class." ”)

The method of parametric construction (capacity initialization and output "method of parametric construction of transport vehicles"). ”)

Capacity set, Get method

Print method: Output capacity

– Define an automobile class (car) in the package Bzu.aa to inherit the vehicle class:

Attribute--speed

Method

The parameterless construction method (Speed Initializes a value of 0, and outputs "the parameterless construction method that executes the automobile class." ”)

The method of parametric construction (using the Super keyword to invoke the parametric construction method of the parent class, passing the parameter to the speed initialization, and outputting "the parametric construction method of executing the automobile class". ”)

Acceleration (speedup): Speed+10 and return to speed;

Deceleration (Speeddown): Speed-15 and return to speed;

Rewrite the Print method: output speed and capacity.

– Define a final bus class in the package bzu.bb, inheriting the car category:

Properties--Patronage (capacity) < variable hide >

Method

The parameterless construction method (capacity Initializes a value of 20, and outputs "the parameterless construction method for executing bus class." ”)

The method of parametric construction (using the Super keyword to invoke the method of parametric construction of the parent class, which is used to initialize the capacity, and outputs "the method of parametric construction of the bus class." ”)

Rewrite the Print method: output speed, capacity, and capacity of the parent class.

– Write a main class test in the package BZU.BB:

Main function

Call the parameterless constructor to create an object car for the car; Call the acceleration method to increase the speed to 50, call the Print method, call the deceleration method, reduce the speed to 20, and call the Print method.

Calls the parameter constructor to create an object bus for a bus; Call the Print method.

Code:

Package BZU.AA;

public class car extends vehicle{
public int speed;

Public car () {
Super ();
Speed = 0;
SYSTEM.OUT.PRINTLN ("Non-parametric construction method for vehicle class");
}
public car (int speed) {
Super (3);
This.speed = speed;
System.out.println ("To carry out the parametric construction method of the Automobile class");
}
public void setspeedup (int speed) {
speed=speed+10;

}
public int getspeedup () {
return speed;
}
public void Setspeeddown (int speed) {
speed=speed-15;

}
public int Getspeeddown () {
return speed;
}
public void print () {
SYSTEM.OUT.PRINTLN ("Carrying Capacity:" +capacity);
SYSTEM.OUT.PRINTLN ("Speed is:" +speed);
}
}

Package BZU.AA;

public class Vehicle {
protected int capacity;
Public Vehicle () {
capacity=2;
System.out.print (" construction method for implementing Vehicle class");
}
Public Vehicle (int C) {
Capacity=c;
System.out.print (" method of implementing means of transport");
}
public void setcapacity () {

}
public int getcapacity () {
return capacity;
}
void print () {
SYSTEM.OUT.PRINTLN ("Carrying Capacity:" +capacity);
}
}

Package bzu.bb;

Import Bzu.aa.Car;

public class Bus extends car{
int capacity;
Bus () {
capacity=20;
System.out.print (" Construction method for bus class execution");

}
public bus (int capacity) {
Super (20);
this.capacity=capacity;
System.out.println ("The method of parametric construction of bus classes". ");
}
public void print () {
SYSTEM.OUT.PRINTLN ("Carrying Capacity:" +capacity);
SYSTEM.OUT.PRINTLN ("Speed is:" +speed);
SYSTEM.OUT.PRINTLN ("Capacity for;" +super.capacity);
}
}

Package bzu.bb;

Import Bzu.aa.Car;

public class Test {
public static void Main (string[] args) {
Car car=new car ();
Car.setspeedup (40);
Car.getspeedup ();
Car.setspeeddown (35);
Car.getspeeddown ();
Bus bus = new bus (25);
Bus.print ();
}
}

Package bzu.bb;

Import Bzu.aa.Car;

public class Test {
public static void Main (string[] args) {
Car car=new car ();
Car.setspeedup (40);
Car.getspeedup ();
Car.setspeeddown (35);
Car.getspeeddown ();
Bus bus = new bus (25);
Bus.print ();
}
}

Package bzu.bb;

Import Bzu.aa.Car;

public class Bus extends car{
int capacity;
Bus () {
capacity=20;
System.out.print (" Construction method for bus class execution");

}
public bus (int capacity) {
Super (20);
this.capacity=capacity;
System.out.println ("The method of parametric construction of bus classes". ");
}
public void print () {
SYSTEM.OUT.PRINTLN ("Carrying Capacity:" +capacity);
SYSTEM.OUT.PRINTLN ("Speed is:" +speed);
SYSTEM.OUT.PRINTLN ("Capacity for;" +super.capacity);
}
}

Screenshots:

Summary: 1. In a class, you can choose to define a constructor method that has parameters, or you can define a parameterless constructor method.

2. Test class must write the main function.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.