关于后端:阿里校招的一道面向对象编程面试题你做出来要花多久时间

42次阅读

共计 6320 个字符,预计需要花费 16 分钟才能阅读完成。

前言

前几天敌人给我分享了一道很简略的校招面试题,我感觉还蛮有意思的,分享进去给你们做一下试一下,看你们做进去要花多久工夫!

题目

类的成员变量:
请定义一个交通工具 (Vehicle) 的类,其中有: 属性:速度 (speed),体积
(size) 等等
办法:挪动 (move()),设置速度(setSpeed(int speed)),减速 speedUp(), 减
速 speedDown()等等.
最初在测试类 Vehicle 中的 main()中实例化一个交通工具对象,并通过方
法给它初始化 speed,size 的值,并且通过打印进去。另外,调用减速,减
速的办法对速度进行扭转。

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

    public Vehicle() {}
    public Vehicle(int speed, int size) {
        this.speed = speed;
        this.size = size;
    }
    // 挪动
    public void move(){}
    // 减速
    public void speedUp(int speed){this.speed+=speed;}
    // 加速
    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("速度为:"+vehicle.getSpeed());
        System.out.println("体积为:"+vehicle.getSize());

        vehicle.speedUp(10);
        System.out.println("减速为:"+vehicle.getSpeed());
        vehicle.speedDown(100);
        System.out.println("加速为:"+vehicle.getSpeed());
    }
}

后果为:

类的成员变量与办法、构造方法

在程序中,常常要对工夫进行操作,然而并没有工夫类型的数据。那
么,咱们能够本人实现一个工夫类,来满足程序中的须要。
定义名为 MyTime 的类,其中应有三个整型成员:时(hour),分
(minute),秒(second),为了保证数据的安全性,这三个成员变量
应申明为公有。为 MyTime 类定义构造方法,以不便创建对象时初始化
成员变量。再定义 diaplay 办法,用于将工夫信息打印进去。
为 MyTime 类增加以下办法:
addSecond(int sec)
addMinute(int min)
addHour(int hou)
subSecond(int sec)
subMinute(int min)
subHour(int hou)
别离对时、分、秒进行加减运算。
代码

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

构造方法:

编写 Java 程序,用于显示人的姓名和年龄。定义一个人类(Person),
该类中应该有两个公有属性,姓名(name)和年龄(age)。定义结构
办法,用来初始化数据成员。再定义显示(display)办法,将姓名和年
龄打印进去。
代码:

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 办法和 set 办法

定义一个类,该类有一个公有成员变量,通过构造方法将其进行赋初
值,并提供该成员的 getXXX()和 setXXX()办法

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

构造方法与重载

建设一个汽车类,包含轮胎个数,汽车色彩,车身分量等属性。并通过
不同的构造方法创立事例。
至多要求:汽车可能减速,加速,停车。
要求:命名标准,代码体现档次,有敌对的操作提醒。

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

    // 减速
    public void speedUp(int speed){System.out.println("汽车减速"+speed+"km/s");
        this.speed+=speed;
    }
    // 加速
    public void speedDown(int speed){System.out.println("汽车加速"+speed+"km/s");
        this.speed-=speed;
    }
    public void stop(){
       speed=0;
        System.out.println("汽车停车");
    }
    // 速度
    public void carStatus(){System.out.println("汽车以"+getSpeed()+"km/ s 的速度前行");
    }

    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" +
                "轮胎数 =" + TiresNum +
                "个, 色彩 ='" + color + '\'' +
                ", 分量 =" + weight +
                "kg, 速度 =" + speed +
                "km/s";
    }
}
class CarTest{public static void main(String[] args) {Car car = new Car();
        car.setTiresNum(4);
        car.setColor("红色");
        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, "彩色", 100.0);
        car1.toString();
        car.carStatus();
        car.stop();
        car.carStatus();
        car.speedUp(180);
        car.carStatus();
        car.speedDown(30);
        car.carStatus();}
}

后果为:

Static 关键字与一般变量的区别:

创立一个名称为 StaticDemo 的类,并申明一个动态变量和一个一般变
量。对变量别离赋予 10 和 5 的初始值。在 main()办法中输入变量值。

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

后果为:

包、继承、构造函数

创立一个名称为 MainPackage 的包,使它蕴含 MainClass 和 MainSubClass
类。MainClass 类该当蕴含变量申明,其值从构造方法中输入。
MainSubClass 类从 MainClass 派生而来。试执行下列操作:创立一个名称
为 SamePackage 的类,使它导入上述包,并创立一个 MainSubClass 类的对

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

后果:

笼罩、继承

建设一个汽车 Auto 类,包含轮胎个数,汽车色彩,车身分量、速度等成
员变量。并通过不同的构造方法创立实例。至多要求:汽车可能减速,
加速,停车。再定义一个小汽车类 Car,继承 Auto,并增加空调、CD 等
成员变量,笼罩减速,加速的办法
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;
    }

    // 减速
    public void speedUp(int speed){System.out.println("汽车减速"+speed+"km/s");
        this.speed+=speed;
    }
    // 加速
    public void speedDown(int speed){System.out.println("汽车加速"+speed+"km/s");
        this.speed-=speed;
    }
    public void stop(){
        speed=0;
        System.out.println("汽车停车");
    }
    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("我是小车减速");
    }

    @Override
    public void speedDown(int speed) {super.speedDown(speed);
        System.out.println("我是小车加速");
    }
}

最初

不晓得你做进去花了多久的工夫呢,如果有什么不懂的和须要探讨的能够在评论区留言!

正文完
 0