1.阅读下面的Person类源代码,并回答下面的问题。 --------程序清单------------------------------------------------------------------------------------------------------------ public class Person { private String name; // 名字 public Person() { name = ""; } public Person(String initialName) { set(initialName); } public void setName(String newName) { set(newName); } public String getName() { return (name.isEmpty() ? "N/A" : name); } public final boolean hasSameName(Person otherPerson) { return name.equals(otherPerson.name); } private void set(String newName) { name = ((newName == null) ? "" : newName.trim()); } @Override public String toString() { return "姓名: " + getName(); } public static void showAll(Person[] people) { for (Person p : people) { System.out.println(p); } } } --------------------------------------------------------------------------------------------------------------------------------- (a)(5分)Person类中的toString方法是重写哪个类里的toString方法? (b)(5分)Person类中的哪些方法不能被它的子类重写?请说明你的理由(没有理由不给分)。 (5分)要使Person类不能被其他类继承,需要使用哪个关键字修饰该类?
时间: 2023-08-15 09:34:13 浏览: 46
(a) Person类中的toString方法是重写Object类里的toString方法。
(b) Person类中的getName方法和hasSameName方法都是final方法,不能被它的子类重写。getName方法是因为它是返回name字段的值,如果被子类重写可能会导致出现意料之外的错误;hasSameName方法是因为它是判断两个Person对象的name字段是否相同,如果被子类重写可能会导致判断出错。
(c) 要使Person类不能被其他类继承,可以使用final关键字修饰该类。例如:public final class Person。
相关问题
R-CNN源代码
R-CNN源代码是一个基于深度学习的目标检测算法,用于识别图像中的物体。下面是R-CNN的Python源代码示例:
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the input image
image = cv2.imread('input_image.jpg')
# Load the pre-trained network for object detection
net = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'model.caffemodel')
# Define the class labels
class_labels = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
# Create a blob from the input image
blob = cv2.dnn.blobFromImage(image, size=(300, 300), swapRB=True)
# Set the input to the network
net.setInput(blob)
# Run the forward pass to get the output of the model
detections = net.forward()
# Loop over the detections
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
# Check if the detection has a high enough confidence
if confidence > 0.5:
class_id = int(detections[0, 0, i, 1])
# Get the coordinates of the bounding box
box = detections[0, 0, i, 3:7] * np.array([image.shape[1], image.shape[0], image.shape[1], image.shape[0]])
x1, y1, x2, y2 = box.astype('int')
# Draw the bounding box and class label
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), thickness=2)
cv2.putText(image, class_labels[class_id], (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(0, 255, 0), thickness=2)
# Show the output image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()
```
该代码使用OpenCV和Caffe实现了R-CNN目标检测算法。在这个代码示例中,我们首先加载输入图像,然后加载预先训练好的网络模型。我们创建一个blob对象,然后将其输入到网络中进行前向传递,并获得模型的输出结果。最后,我们遍历输出结果,将置信度高于0.5的检测框绘制在图像上,并标注检测到的物体的类别。
Java面向对象-类定义1
Java是一种面向对象的编程语言,类是Java中最基本的编程单元。类定义了对象的属性和方法。以下是一个简单的Java类定义示例:
```java
public class Person {
// 属性
private String name;
private int age;
// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
public void sayHello() {
System.out.println("Hello, my name is " + name + ", I'm " + age + " years old.");
}
}
```
在上面的示例中,我们定义了一个名为Person的类,它有两个属性:name和age,以及一个构造方法和一个方法sayHello()。构造方法用于创建对象时初始化属性,而方法则用于执行某些操作。
要使用这个类,我们可以在另一个类中创建一个Person对象,并调用它的方法:
```java
public class Main {
public static void main(String[] args) {
Person person = new Person("Tom", 18);
person.sayHello(); // 输出:Hello, my name is Tom, I'm 18 years old.
}
}
```
在上面的示例中,我们创建了一个名为Main的类,并在其中创建了一个Person对象,然后调用了它的sayHello()方法。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)