解释一下这串代码public String testPerson(){
时间: 2024-04-18 08:33:53 浏览: 14
这段代码是一个Java方法的定义,方法名为testPerson,返回类型为String(字符串类型)。代码中的public关键字表示该方法可以被其他类访问。方法声明后面的括号内没有参数,表示该方法不接受任何输入参数。
在方法的主体中,你可以编写一些逻辑来实现特定的功能。由于你只提供了方法声明而没有提供具体的实现内容,所以无法确切知道该方法的功能。通常情况下,testPerson方法可能会执行一些操作,然后返回一个字符串作为结果。
例如,testPerson方法可能会用来测试一个人的某些属性或功能,并返回一个描述结果的字符串。具体的实现内容需要根据代码上下文和需求来确定。
相关问题
用 java编写一个测试类,1)创建方法public static void m1(Person p),显示p的姓名;2)创建方法public static void m2(Person p),打印p的toString方法返回的字符串;3)创建方法public static void m3(Person p),如果p是Student类或者Employee类的实例,分别调用它们的displayObject();4)在主函数中创建Person、Student、Employee的对象实例,将它们均声明为Person类对象。将它们分别传入m1、m2和m3,体会多态。
好的,以下是你所需的Java代码:
```
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John");
Student student = new Student("Jack", "CS");
Employee employee = new Employee("Tom", "Google");
m1(person);
m2(student);
m3(employee);
Person studentAsPerson = new Student("Alice", "Math");
Person employeeAsPerson = new Employee("Bob", "Facebook");
m1(studentAsPerson);
m2(employeeAsPerson);
m3(studentAsPerson);
m3(employeeAsPerson);
}
public static void m1(Person p) {
System.out.println(p.getName());
}
public static void m2(Person p) {
System.out.println(p.toString());
}
public static void m3(Person p) {
if (p instanceof Student) {
((Student) p).displayObject();
} else if (p instanceof Employee) {
((Employee) p).displayObject();
}
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return "Person " + name;
}
}
class Student extends Person {
private String major;
public Student(String name, String major) {
super(name);
this.major = major;
}
public void displayObject() {
System.out.println("Student " + getName() + ", Major: " + major);
}
public String toString() {
return "Student " + getName() + ", Major: " + major;
}
}
class Employee extends Person {
private String company;
public Employee(String name, String company) {
super(name);
this.company = company;
}
public void displayObject() {
System.out.println("Employee " + getName() + ", Company: " + company);
}
public String toString() {
return "Employee " + getName() + ", Company: " + company;
}
}
```
在这个代码中,我们创建了一个`TestPerson`类,其中包含了4个方法和3个类。我们在`main`方法中创建了`Person`、`Student`和`Employee`的实例,并将它们均声明为`Person`类对象,然后分别传入了`m1`、`m2`和`m3`方法,体会了多态。
`m1`方法接受一个`Person`对象,并显示它的姓名。`m2`方法接受一个`Person`对象,并打印它的`toString`方法返回的字符串。`m3`方法接受一个`Person`对象,并如果它是`Student`类或者`Employee`类的实例,分别调用它们的`displayObject`方法。
`Person`、`Student`和`Employee`都是类,其中`Student`和`Employee`都继承自`Person`,并且它们都有`displayObject`和`toString`方法。`displayObject`方法在子类中被重写,以便我们可以使用多态来调用它们。`toString`方法也在子类中被重写,以便我们可以使用多态来打印它们的字符串表示形式。
package semster2lab9; import java.util.Scanner; public class TestPerson { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Person p[]=new Person[3]; p[0]=new Person(sc.nextLine(),sc.nextLine(),sc.nextInt()); p[1]=new Person(sc.nextLine(),sc.nextLine(),sc.nextInt()); p[2]=new Person(sc.nextLine(),sc.nextLine(),sc.nextInt()); System.out.println("Person 1 is called "+p[0].getName()); System.out.println("They live in "+p[0].getAddress()); System.out.println("Their PPS Number is"+p[0].getppsNo()); p[1].setName("Paul Smyth"); p[1].setAddress("Dublin"); for(int i=0;i<3;i++) { System.out.println("*****PERSONAL DETAILS*****"); p[0].showPerson(); p[1].showPerson(); p[2].showPerson(); } System.out.print(p[2].toString()); } }
这代码的问题可能是输入不匹配。在第 8、11 和 14 行中,您正在尝试通过调用 `nextInt()` 来获取输入,但是如果输入不是整数,就会抛出 `InputMismatchException`。因此,如果您输入了一个不是整数的值,就会发生异常。
为了解决这个问题,您可以考虑使用 `nextLine()` 方法获取输入,并将其转换为适当的类型。例如,您可以像这样修改代码:
```
p[0] = new Person(sc.nextLine(), sc.nextLine(), Integer.parseInt(sc.nextLine()));
p[1] = new Person(sc.nextLine(), sc.nextLine(), Integer.parseInt(sc.nextLine()));
p[2] = new Person(sc.nextLine(), sc.nextLine(), Integer.parseInt(sc.nextLine()));
```
这样,您将首先获取一个字符串,然后将其转换为适当的类型,以避免输入不匹配的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)