阿里校招的一道面向对象编程面试题,你做出来要花多久时间?

前程有光 2020-11-06 01:16:18
阿里 一道 面向对象 面向 对象


前言

前几天朋友给我分享了一道很简单的校招面试题,我觉得还蛮有意思的,分享出来给你们做一下试一下,看你们做出来要花多久时间!

题目

类的成员变量:
请定义一个交通工具(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("我是小车减速");
}
}

最后

不知道你做出来花了多久的时间呢,如果有什么不懂的和需要探讨的可以在评论区留言!

版权声明
本文为[前程有光]所创,转载请带上原文链接,感谢
https://segmentfault.com/a/1190000037740485

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云