面向对象开发期末笔试概念解释汇总(押中率100%)

What are four features of Constructors in Java?
What are four features of Constructors in Java?
- A constructor with no parameters(参数) is referred to as a no-arg constructor
- Constructors must have the same name as the class itself
- Constructors do not have a return type—not even void
- Constructors are invoked(被调用) using the new operator when a object is created. Constructors play the role of initializing(初始化) objects
Qusetion about class naming and access
public:全局访问,任何类都可访问;
protected:包内+子类访问,同一包中的类或子类可访问;
默认(无修饰符):包内访问,仅同一包中的类可访问;
private:本类访问,仅当前类内部可访问(最严格的封装)
What is the define of “class”?
Classes are constructs that define objects of the same type.
What are the characteristics of classes and objects respectively?
- class:
- name
- variables
- method
- object:
- identity
- state
- behavior
What is Immutable Class?
If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class.
Explain Class Abstraction and Encapsulation
Class abstraction means separating class implementation from how the class is used. The details of implementation are encapsulated and hidden from the user. This is known as class encapsulation.
Explain Abstract Class
In the inheritance hierarchy, sometimes a superclass is so abstract that it cannot be used to create any specific instances. Such a class is called an abstract class.
What are the characteristics of an abstract class?
Abstract classes are like regular classes with data and methods, but you cannot create instances of abstract classes using the new operator.
A class that contains abstract methods must be abstract.
Explain Interface
An interface is a class-like construct for defining common operations for objects
What are the characteristics of the interface?
The intent of an interface is to specify common behavior for objects of related classes or unrelated classes.(接口的目的是为具有相关或不相关类别的对象规定共同的行为。)
A class can implement multiple interfaces, but it can only extend one superclass.(A class can implement multiple interfaces, but it can only extend one superclass.)
Explain Inheritance
Object-oriented programming allows you to define new classes from existing classes.This is called inheritance.
Explain Polymorphism(多态)
An object of a subclass can be used wherever its superclass object is used.
Explain Dynamic Binding
A method can be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime… This is known as dynamic binding.
How does Dynamic Binding works?
Dynamic binding works as follows: Suppose that an object o is an instance of classes C1, C2, . . . , Cn-1, and Cn, where C1 is a subclass of C2, C2 is a subclass of C3, . . . , and Cn-1 is a subclass of Cn… If o invokes a method p, the JVM searches for the implementation of the method p in C1, C2, . . . , Cn-1, and Cn, in this order, until it is found. Once an implementation is found, the search stops and the first-found implementation is invoked.
(动态绑定的工作原理如下:假设对象 o 是类 C1,C2,…,Cn 的实例,其中 C1 是 C2 的子类,C2 是 C3 的子类……以此类推。如果 o 调用一个方法 p,JVM 会按 C1,C2,…,Cn 的顺序搜索方法 p 的实现。一旦找到,搜索就会停止,并调用第一个找到的实现)
Explain Casting
Casting is an operation that converts a value of one data type into a value of another data type.
Two types of Casting Objects
“It is always possible to cast an instance of a subclass to a variable of a superclass (known as upcasting) because an instance of a subclass is always an instance of its superclass.”
(总是可以将子类的实例转换为父类的变量(称为向上转换),因为子类实例永远是父类的实例。)
“When casting an instance of a superclass to a variable of its subclass (known as downcasting), explicit casting must be used to confirm your intention to the compiler.”
(当将父类的实例转换为其子类的变量(称为向下转换)时,必须使用显式转换以向编译器确认你的意图。)
What are the characteristics and usage of type conversion?
“Casting an object reference does not create a new object.”(转换对象引用并不会创建一个新的对象)
“It is a good practice… to ensure the object is an instance of another object before attempting a casting. This can be accomplished by using the instanceof operator.”(在尝试转换之前,确保该对象是目标类的一个实例是一个好习惯,这可以通过使用 instanceof 运算符来实现)
Explain Method Overloading
Two methods have the same name but different parameter lists within one class. This is referred to as method overloading.
( 在同一个类中,两个方法具有相同的名称但参数列表不同,这被称为方法重载)
Explain Method Overriding
It is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding.(子类需要修改父类中定义的方法的实现,这被称为方法重写(或覆盖))
What is the different between Overriding vs. Overloading?
Overloading means to define multiple methods with the same name but different signatures.
Overriding means to provide a new implementation for a method in the subclass.
Please compare the usage of the key words “this” and “super”
this定义:“The keyword this refers to the calling object. It can also be used inside a constructor to invoke another constructor of the same class.”
“this 用法1”:“this” keyword to reference the object’s instance members in the class.( this 关键字来引用类中对象的实例成员)
“this” 用法2:“this” keyword is needed to reference a data field hidden by a method or constructor parameter.(当数据域被方法或构造方法的参数隐藏(重名)时,需要使用 this 关键字来引用该数据域)super 定义: “The keyword super refers to the superclass and can be used to invoke the superclass’s methods and constructors.”
super 用法1:To call a superclass constructor.
super 用法2:To call a superclass method.
Please explain the usage of Comparable and Cloneable
“Many classes in the Java library implement Comparable to define a natural order for objects. The classes Byte, Short, Integer, Long, Float, Double, Character, BigInteger, BigDecimal, Calendar, String, and Date all implement the Comparable interface.” “Since all Comparable objects have the compareTo method, the java.util.Arrays.sort(Object[]) method in the Java API uses the compareTo method to compare and sorts the objects in an array.”(Java 库中的许多类都实现了 Comparable 以定义对象的自然顺序。Byte、Short、Integer、Long、Float、Double、Character、BigInteger、BigDecimal、Calendar、String 和 Date 类都实现了 Comparable 接口。 由于所有 Comparable 对象都具有 compareTo 方法,Java API 中的 java.util.Arrays.sort(Object[]) 方法使用 compareTo 方法来比较和排序数组中的对象。)
“A class that implements the Cloneable interface is marked cloneable, and its objects can be cloned using the clone() method defined in the Object class.” “To define a custom class that implements the Cloneable interface, the class must override the clone() method in the Object class.” “The clone method in the Object class copies each field from the original object to the target object… This is referred to as a shallow copy rather than a deep copy.”( 实现 Cloneable 接口的类被标记为可克隆的,其对象可以使用 Object 类中定义的 clone() 方法进行克隆。 要定义一个实现 Cloneable 接口的自定义类,该类必须重写 Object 类中的 clone() 方法。 Object 类中的 clone 方法将原始对象的每个字段复制到目标对象……这被称为浅拷贝(shallow copy)而非深拷贝(deep copy)。)
Explain Errors and Exceptions
“Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors.” “Exceptions are runtime errors. Exception handling enables a program to deal with runtime errors and continue its normal execution.” “In Java, runtime errors are thrown as exceptions.”
编程错误可分为三种类型:语法错误、运行时错误和逻辑错误。“异常是运行时错误。异常处理使程序能够处理运行时错误并继续正常执行。”“在 Java 中,运行时错误以异常的形式抛出。”
Explain Exception
“An exception is an object that represents an error or a condition that prevents execution from proceeding normally.”
异常是一个表示错误或阻止程序正常执行的条件的对象。
Explain Declaring and Throwing Exceptions
“To declare an exception in a method, use the throws keyword in the method header.” “A program that detects an error can create an instance of an appropriate exception type and throw it. This is known as throwing an exception.” “The keyword to declare an exception is throws, and the keyword to throw an exception is throw.”
(要在方法中声明异常,请在方法头中使用 throws 关键字。检测到错误的程序可以创建一个适当异常类型的实例并将其抛出,这被称为抛出异常。声明异常的关键字是 throws,而抛出异常的关键字是 throw。)
Describe the Catching and Propagation
“The exception is caught by the catch block. The code in the catch block is executed to handle the exception.” “A handler for an exception is found by propagating the exception backward through a chain of method calls, starting from the current method.” “If no handler is found in the chain of methods being invoked, the program terminates and prints an error message on the console.”
(异常被 catch 块捕获,执行 catch 块中的代码来处理异常。异常处理程序的查找是通过从当前方法开始,沿着方法调用链向后传播异常来实现的。如果在被调用的方法链中没有找到处理程序,程序将终止并在控制台上打印错误消息)
Usage of try-catch-finally
“The try block contains the code that is executed in normal circumstances. The catch block contains the code that is executed in exceptional circumstances.” “The finally clause is always executed regardless of whether an exception occurred or not.” “The code in the finally clause is often for closing files and for cleaning up resources.”
(try 块包含在正常情况下执行的代码。catch 块包含在异常情况下执行的代码。无论是否发生异常,finally 子句总是会执行。finally 子句中的代码通常用于关闭文件和清理资源)
Describe the relationship between classes in Java
“The common relationships among classes are association, aggregation, composition, and inheritance.” “Association is a general binary relationship that describes an activity between two classes.” “Aggregation is a special form of association that represents an ownership relationship between two objects. Aggregation models has-a relationships.” “We refer aggregation between two objects as composition if the existence of the aggregated object is dependent on the aggregating object.” “Inheritance enables you to define a general class (i.e., a superclass) and later extend it to more specialized classes (i.e., subclasses).”
( 类之间的常见关系包括关联、聚合、组合和继承。 关联是一种描述两个类之间活动的一般二元关系。聚合是关联的一种特殊形式,代表两个对象之间的所有权关系,建模为“包含(has-a)”关系。如果被聚合对象的存在依赖于聚合对象,则这种聚合关系称为组合。继承使你能够定义一个通用类(父类),随后将其扩展为更具体的类(子类)。)
Explain Garbage Collection
If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The JVM will automatically collect the space if the object is not referenced by any variable.
List out the similarities and differences among classes, abstract classes, and interfaces
“An interface is a class-like construct that contains only constants, abstract methods, default methods, and static methods. In many ways, an interface is similar to an abstract class, but an abstract class can contain data fields.” “A class can implement multiple interfaces, but it can only extend one superclass.”
( 接口是一种类似于类的结构,只包含常量、抽象方法、默认方法和静态方法。在许多方面,接口与抽象类相似,但抽象类可以包含数据域。 一个类可以实现多个接口,但只能继承一个父类。)
What is the relationship between abstract classes and abstract methods?
“A class that contains abstract methods must be defined as abstract.” “An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined as abstract.” “In a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented.”(包含抽象方法的类必须定义为抽象类。 抽象方法不能包含在非抽象类中。如果抽象父类的子类没有实现所有的抽象方法,该子类必须定义为抽象类。 在继承自抽象类的非抽象子类中,必须实现所有(继承来的)抽象方法。)
What is the usage of Array of Objects?
“An array can hold objects as well as primitive-type values.” “An array of objects is actually an array of reference variables. Thus, invoking circleArray.getArea() involves two levels of referencing… circleArray references the entire array, and circleArray references a Circle object.” “When an array of objects is created using the new operator, each element in the array is a reference variable with a default value of null.”(数组既可以保存对象,也可以保存基本类型值。 对象数组实际上是引用变量的数组。 因此,调用 circleArray.getArea() 涉及两级引用:circleArray 引用整个数组,而 circleArray 引用一个具体的对象。当使用 new 运算符创建对象数组时,数组中的每个元素都是一个引用变量,默认值为 null。)
Briefly describe the UML diagrams
The illustration of class templates and objects… can be standardized using Unified Modeling Language (UML) notation. This notation… is called a UML class diagram, or simply a class diagram." "In the class diagram, the data field is denoted as dataFieldName: dataFieldType.
What are the principles of events and event-driven programming?
“An event can be defined as a signal to the program that something has happened. Events are triggered by external user actions, such as mouse movements, mouse clicks, and keystrokes. The program can choose to respond to or ignore an event.” “The component that creates an event and fires it is called the event source object… An event is an instance of an event class.” “Java uses a delegation-based model for event handling: A source object fires an event, and an object interested in the event handles it. The latter object is called an event handler.”
(事件可以定义为向程序发送的一个信号,表明某事已经发生。事件由外部用户动作触发,如鼠标移动、点击和按键。程序可以选择响应或忽略事件。创建并触发事件的组件称为事件源对象。事件是事件类的实例。Java 使用基于委托的模型进行事件处理:源对象触发事件,而对此事件感兴趣的对象处理它。后者被称为事件处理程序。)
Please explain the differences between the dynamic binding and the method matching
Method Matching: This process occurs at compile time. The compiler uses the declared type of the reference variable to decide which method to match based on the method name and the number, type, and order of parameters.
Dynamic Binding: This process occurs at runtime. The JVM determines which specific implementation of a method to invoke based on the actual type of the object referenced by the variable at runtime
方法匹配:此过程在编译时进行。编译器根据引用变量的声明类型,依据方法名称以及参数的数量、类型和顺序来决定匹配哪个方法。
动态绑定:此过程在运行时进行。JVM 根据变量在运行时所引用对象的实际类型,确定要调用的方法的具体实现。

浙公网安备 33010602011771号