当前位置:网站首页>How long does it take you to work out an object-oriented programming interview question from Ali school?

How long does it take you to work out an object-oriented programming interview question from Ali school?

2020-11-06 01:16:18 Bright future

Preface

A few days ago, my friend shared a very simple interview question for school recruitment , I think it's quite interesting , I'll share it with you and try it , See how long it will take you to make it !

subject

Member variables of class :
Please define a vehicle (Vehicle) Class , Among them is : attribute : Speed (speed), Volume
(size) wait
Method : Move (move()), set speed (setSpeed(int speed)), Speed up speedUp(), reduce
speed speedDown() wait .
Finally, test the class Vehicle Medium main() Instantiate a vehicle object in , And through Fang
Can initialize it speed,size Value , And print it out . in addition , Call acceleration , reduce
The speed method changes the speed .

public class Vehicle {
    private int speed;
    private int size;

    public Vehicle() {
    }
    public Vehicle(int speed, int size) {
        this.speed = speed;
        this.size = size;
    }
    // Move 
    public void move(){ }
    // Speed up 
    public void speedUp(int speed){
        this.speed+=speed;
    }
    // Slow down 
    public void speedDown(int speed){
        this.speed-=speed;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }

    public static void main(String[] args) {
       Vehicle vehicle = new Vehicle();
        vehicle.setSpeed(160);
        vehicle.setSize(100);
        System.out.println(" Speed is :"+vehicle.getSpeed());
        System.out.println(" The volume is :"+vehicle.getSize());

        vehicle.speedUp(10);
        System.out.println(" Accelerate to :"+vehicle.getSpeed());
        vehicle.speedDown(100);
        System.out.println(" Slow down to :"+vehicle.getSpeed());
    }
}

The result is :

Class member variables and methods 、 Construction method

In the program , Time is often manipulated , But there's no time type data . that
Well , We can implement a time class ourselves , To meet the needs of the program .
Definition is named MyTime Class , There should be three integer members : when (hour), branch
(minute), second (second), In order to ensure the security of data , These three member variables
Should be declared private . by MyTime Construct method definitions , To facilitate the initialization of
Member variables . Redefine diaplay Method , Used to print out time information .
by MyTime Class add the following methods :
addSecond(int sec)
addMinute(int min)
addHour(int hou)
subSecond(int sec)
subMinute(int min)
subHour(int hou)
Separate the time 、 branch 、 Add and subtract seconds .
Code

public class MyTime {
    private int hour;
    private int minute;
    private int second;
    public MyTime(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }
    public void display() {
        System.out.println("hour=" + hour +
                ", minute=" + minute +
                ", second=" + second +
                '}');
    }
    public void addSecond(int sec){
        this.second+=sec;
    }
    public void addMinute(int min){
        this.minute+=min;
    }
    public void addHour(int hou){
        this.hour+=hou;
    }
    public void subSecond(int sec){
        this.second-=sec;
    }
    public void subMinute(int min){
        this.minute-=min;
    }
    public void subHour(int hou){
        this.hour-=hou;
    }
}

Construction method :

To write Java Program , Used to show a person's name and age . Define a human being (Person),
There should be two private properties in this class , full name (name) And age (age). Define construction
Method , Used to initialize data members . Redefine the display (display) Method , Name and year
Print it out .
Code :

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void display() {
        System.out.println( "name='" + name + '\'' +
                ", age=" + age);
    }
    public static void main(String[] args) {
        Person yc = new Person("yc", 6);
        yc.display();
    }
}

get Methods and set Method

Define a class , This class has a private member variable , By means of construction method, it is given the beginning
value , And provide the member's getXXX() and setXXX() Method

public class Lab04 {
    private int age;

    public Lab04() {
        age = 10;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) {
        Lab04 lab04 = new Lab04();
        System.out.println(lab04.getAge());
    }
}

Construction methods and overloading

Build a car class , Including the number of tires , Car color , Body weight and other attributes . And pass
Different constructors create cases .
At the very least : The car can accelerate , Slow down , Parking .
requirement : Naming specification , The code reflects the hierarchy , There are friendly operating tips .

public class Car {

    private int TiresNum;

    private String color;

    private double weight;

    private int speed=30;

    public Car() {
    }

    public Car(int tiresNum, String color, double weight) {
        this.TiresNum = tiresNum;
        this.color = color;
        this.weight = weight;
    }

    // Speed up 
    public void speedUp(int speed){
        System.out.println(" The car accelerates "+speed+"km/s");
        this.speed+=speed;
    }
    // Slow down 
    public void speedDown(int speed){
        System.out.println(" Slow down the car "+speed+"km/s");
        this.speed-=speed;
    }
    public void stop(){
       speed=0;
        System.out.println(" The car stops ");
    }
    // Speed 
    public void carStatus(){
        System.out.println(" Cars to "+getSpeed()+"km/s Speed forward ");
    }

    public int getTiresNum() {
        return TiresNum;
    }

    public void setTiresNum(int tiresNum) {
        TiresNum = tiresNum;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    @Override
    public String toString() {
        return "Car" +
                " The number of tires =" + TiresNum +
                " individual ,  Color ='" + color + '\'' +
                ",  weight =" + weight +
                "kg,  Speed =" + speed +
                "km/s";
    }
}
class CarTest{
    public static void main(String[] args) {
        Car car = new Car();
        car.setTiresNum(4);
        car.setColor(" white ");
        car.setWeight(100.1);
        System.out.println(car.toString());
        car.carStatus();
        car.stop();
        car.carStatus();
        car.speedUp(100);
        car.carStatus();
        car.speedDown(20);
        car.carStatus();
        System.out.println("----------------------");
        Car car1 = new Car(4, " black ", 100.0);
        car1.toString();
        car.carStatus();
        car.stop();
        car.carStatus();
        car.speedUp(180);
        car.carStatus();
        car.speedDown(30);
        car.carStatus();
    }
}

The result is :

Static The difference between keywords and ordinary variables :

Create a name StaticDemo Class , And declare a static variable and a normal variable
The amount . Assign to variables respectively 10 and 5 The initial value of the . stay main() The output variable value in the method .

public class StaticDemo {
    public static int num=10;
    public int num1=5;

    public static void main(String[] args) {
        System.out.println("num="+StaticDemo.num);
        StaticDemo staticDemo = new StaticDemo();
        System.out.println("num1="+staticDemo.num1);
    }
}

The result is :

package 、 Inherit 、 Constructors

Create a name MainPackage My bag , Make it contain MainClass and MainSubClass
class .MainClass Class should contain variable declaration , Its value is output from the constructor .
MainSubClass Class from MainClass Derived from . Try to do the following : Create a name
by SamePackage Class , Import it into the above package , And create a MainSubClass Class pair
like

package day06.MainPackage;

public class MainClass {
    int num=10;

    public MainClass() {
        System.out.println(num);
    }
}
package day06.MainPackage;

public class MainSubClass extends MainClass {
}
package day06;

import day06.MainPackage.MainSubClass;

public class SamePackage {
    public static void main(String[] args) {
        MainSubClass mainSubClass = new MainSubClass();
    }
}

result :

Cover 、 Inherit

Build a car Auto class , Including the number of tires , Car color , Body weight 、 Speed is equal to
Member variables . And create instances through different construction methods . At the very least : The car can accelerate ,
Slow down , Parking . Define another car class Car, Inherit Auto, And add air conditioning 、CD etc.
Member variables , Coverage acceleration , How to slow down
Auto.java

public class Auto {
    private int TiresNum;

    private String color;

    private double weight;

    private int speed=30;

    public Auto() {
    }

    public Auto(int tiresNum, String color, double weight) {
        this.TiresNum = tiresNum;
        this.color = color;
        this.weight = weight;
    }

    // Speed up 
    public void speedUp(int speed){
        System.out.println(" The car accelerates "+speed+"km/s");
        this.speed+=speed;
    }
    // Slow down 
    public void speedDown(int speed){
        System.out.println(" Slow down the car "+speed+"km/s");
        this.speed-=speed;
    }
    public void stop(){
        speed=0;
        System.out.println(" The car stops ");
    }
    public int getTiresNum() {
        return TiresNum;
    }

    public void setTiresNum(int tiresNum) {
        TiresNum = tiresNum;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
}

car.java

public class Car extends Auto{
    private String airCondition;
    private String cdName;

    public Car() {

    }
    public Car(int tiresNum, String color, double weight, String airCondition, String cdName) {
        super(tiresNum, color, weight);
        this.airCondition = airCondition;
        this.cdName = cdName;
    }

    @Override
    public void speedUp(int speed) {
        super.speedUp(speed);
        System.out.println(" I'm a little car, accelerating ");
    }

    @Override
    public void speedDown(int speed) {
        super.speedDown(speed);
        System.out.println(" I slow down the car ");
    }
}

Last

I don't know how long it took you to make it , If there is anything you don't understand and need to discuss, you can leave a message in the comments area !

版权声明
本文为[Bright future]所创,转载请带上原文链接,感谢