java定义一个person类类中定义一个方法返回所有属性
时间: 2024-05-08 12:19:23 浏览: 96
好的,下面是Java定义一个Person类并定义一个方法返回所有属性的代码:
```
public class Person {
private String name;
private int age;
private String job;
// constructor
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
// getter methods for each property
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getJob() {
return job;
}
// method to return all properties
public String getAllProperties() {
return "Name: " + name + " Age: " + age + " Job: " + job;
}
}
```
你可以通过创建一个Person对象,然后调用getAllProperties()方法来返回所有属性的值。例如:
```
Person john = new Person("John", 30, "Software Engineer");
String allProperties = john.getAllProperties();
System.out.println(allProperties);
```
这将输出:Name: John Age: 30 Job: Software Engineer
阅读全文