(创建父类Person属性有姓名年龄方法有两个构造方法,1个普通方法用于显示姓名和年龄,还有针对属性的输入输出方法创建子类Student属性有学号方法有两个构造方法;1个普通方法用于显示学号,姓名,年龄;创建测试类StudentTest进行测试。)的代码是什么
时间: 2024-06-01 08:12:49 浏览: 32
以下是代码示例:
//父类Person
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
this.name = "";
this.age = 0;
}
public void showNameAndAge() {
System.out.println("姓名:" + name + ",年龄:" + age);
}
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;
}
}
//子类Student
public class Student extends Person {
private String stuNo;
public Student(String name, int age, String stuNo) {
super(name, age);
this.stuNo = stuNo;
}
public Student() {
super();
this.stuNo = "";
}
public void showStuInfo() {
System.out.println("学号:" + stuNo + ",姓名:" + getName() + ",年龄:" + getAge());
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
}
//测试类StudentTest
public class StudentTest {
public static void main(String[] args) {
Student stu = new Student("小明", 18, "20210001");
stu.showStuInfo();
}
}
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)