用JAVA语言编写 (1)创建一个student的类:属性:姓名、性别、学号;行为:说话、计算加法。编写能为所有属性赋值的构造方法。 (2)创建主类:创建一个对象:名叫“王明”,年龄19岁,性别“男”,学号00001;让该对象调用成员方法:说出“你好!”,计算19+20的值
时间: 2024-06-10 12:05:45 浏览: 12
第一个问题:
```
public class Student {
// 属性:姓名、性别、学号
private String name;
private String gender;
private String studentNumber;
// 行为:说话、计算加法
public void speak() {
System.out.println("你好!");
}
public int add(int a, int b) {
return a + b;
}
// 编写能为所有属性赋值的构造方法
public Student(String name, String gender, String studentNumber) {
this.name = name;
this.gender = gender;
this.studentNumber = studentNumber;
}
}
```
第二个问题:
```
public class Main {
public static void main(String[] args) {
// 创建一个对象:名叫“王明”,年龄19岁,性别“男”,学号00001
Student student = new Student("王明", "男", "00001");
// 让该对象调用成员方法:说出“你好!”,计算19 20的值
student.speak();
int result = student.add(19, 20);
System.out.println(result);
}
}
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)