用JAVA写,定义一个学生类(Student),成员变量有private的姓名(name)和年龄(age),在主类main方法中创建一个学生对象,通过构造方法给成员name和age赋值, 最后输出学生的姓名name和年龄age。
时间: 2024-02-13 11:07:35 浏览: 42
好的,以下是JAVA代码实现:
```java
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("张三", 18);
System.out.println("学生姓名:" + student.getName() + ",学生年龄:" + student.getAge());
}
}
```
以上代码创建了一个名为Student的类,其中包含了两个成员变量name和age,并通过构造方法对它们进行初始化。在main方法中,创建了一个名为student的学生对象,并通过调用getName和getAge方法输出了学生的姓名和年龄。
相关问题
1. 定义一个学生类Student,定义3个成员变量id,name,age,生成相应的getter和setter方法
好的,以下是一个Java类Student,包含id、name和age三个成员变量,以及相应的getter和setter方法:
```
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
在这个例子中,我们使用了Java的封装特性,将成员变量声明为private,然后通过公共的getter和setter方法来访问和修改这些变量的值。
希望这个例子能够帮助您理解如何定义一个带有成员变量和getter/setter方法的Java类。
JAVA 定义一个学生类(Student),包含学号、姓名、年龄(7-60岁)三个私有成员
以下是 JAVA 定义一个学生类(Student)的代码,其中包含学号、姓名、年龄(7-60岁)三个私有成员:
```java
public class Student {
private String id; // 学号
private String name; // 姓名
private int age; // 年龄,取值范围为7-60岁
// 构造方法
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
if (age < 7 || age > 60) {
throw new IllegalArgumentException("年龄必须在7-60岁之间");
}
this.age = age;
}
// getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 7 || age > 60) {
throw new IllegalArgumentException("年龄必须在7-60岁之间");
}
this.age = age;
}
}
```
在这个类中,我们使用了 private 关键字来修饰学号、姓名、年龄这三个成员变量,使得它们只能在类内部被访问。同时,我们还提供了一个构造方法来初始化这三个成员变量,并在构造方法中对年龄进行了范围检查。除此之外,我们还提供了 getter 和 setter 方法来获取和设置这三个成员变量的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![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)