Java 中定义一个学生类Student,在类中定义4个成员变量id,age,last_name(姓),first_name(名),定义两个方法一个用于设置姓和名,另外一个方法getName返回该人的全名,
时间: 2024-05-06 10:20:08 浏览: 14
代码如下:
```
public class Student {
private int id;
private int age;
private String last_name;
private String first_name;
public void setName(String last_name, String first_name) {
this.last_name = last_name;
this.first_name = first_name;
}
public String getName() {
return this.last_name + " " + this.first_name;
}
}
```
在上述代码中,定义了一个Student类,其中包含了4个成员变量:id、age、last_name和first_name。其中,id和age是int类型,而last_name和first_name是String类型。
在该类中,定义了两个方法,一个用于设置姓和名,另外一个方法getName返回该人的全名。setName方法接受两个参数,即姓和名,将其分别赋值给last_name和first_name成员变量。getName方法返回该人的全名,使用字符串拼接的方式,将last_name和first_name连接起来返回。
这样,当我们需要创建一个学生对象时,可以通过调用setName方法来设置该学生的姓和名,然后通过调用getName方法来获取该学生的全名。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)