阿里校招的一道面向对象编程面试题,你做出来要花多久时间?
前言
前几天朋友给我分享了一道很简单的校招面试题,我觉得还蛮有意思的,分享出来给你们做一下试一下,看你们做出来要花多久时间!
题目
类的成员变量:
请定义一个交通工具(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("我是小车减速");
}
}
最后
不知道你做出来花了多久的时间呢,如果有什么不懂的和需要探讨的可以在评论区留言!