注册 登录
编程论坛 JAVA论坛

Java继承类有没有懂的

HAO遇见 发布于 2023-04-08 14:26, 6294 次点击
设计一 个学生类Student,并派出小学生,中学生,大学生

其中大学生再派出大一,二,三,四年级学生4个类:属性全部使用封装,方法:无参,有参方法: get, set方法,自我介绍方法

学生类属性:姓名,年龄,性别

小学生属性:学号,年级

中学生属性:学号,年级,政治面貌

大学生属性:系别,专业

大一,二,三,四属性:学号,班级,政治面貌
6 回复
#2
东海ECS2023-04-08 18:11
实例代码:
程序代码:

// 学生类
class Student {
    private String name;
    private int age;
    private String gender;
    public Student() {}
    public Student(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    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; }
    public String getGender() { return gender; }
    public void setGender(String gender) { this.gender = gender; }
    public void introduce() {
        System.out.println("我是一名学生,我的名字是" + name + ",今年" + age + "岁,性别是" + gender);
    }
}
// 小学生类
class PrimaryStudent extends Student {
    private String studentNo;
    private int grade;
    public PrimaryStudent() {}
    public PrimaryStudent(String name, int age, String gender, String studentNo, int grade) {
        super(name, age, gender);
        this.studentNo = studentNo;
        this.grade = grade;
    }
    public String getStudentNo() { return studentNo; }
    public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
    public int getGrade() { return grade; }
    public void setGrade(int grade) { this.grade = grade; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是小学生,我的学号是" + studentNo + ",今年" + grade + "年级");
    }
}
// 中学生类
class MiddleStudent extends Student {
    private String studentNo;
    private int grade;
    private String politicalStatus;
    public MiddleStudent() {}
    public MiddleStudent(String name, int age, String gender, String studentNo, int grade, String politicalStatus) {
        super(name, age, gender);
        this.studentNo = studentNo;
        this.grade = grade;
        this.politicalStatus = politicalStatus;
    }
    public String getStudentNo() { return studentNo; }
    public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
    public int getGrade() { return grade; }
    public void setGrade(int grade) { this.grade = grade; }
    public String getPoliticalStatus() { return politicalStatus; }
    public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是中学生,我的学号是" + studentNo + ",今年" + grade + "年级,政治面貌是" + politicalStatus);
    }
}
// 大学生类
class CollegeStudent extends Student {
    private String department;
    private String major;
    public CollegeStudent() {}
    public CollegeStudent(String name, int age, String gender, String department, String major) {
        super(name, age, gender);
        this.department = department;
        this.major = major;
    }
    public String getDepartment() { return department; }
    public void setDepartment(String department) { this.department = department; }
    public String getMajor() { return major; }
    public void setMajor(String major) { this.major = major; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是大学生,所在系别是" + department + ",专业是" + major);
    }
}
// 大一学生类
class CollegeFreshman extends CollegeStudent {
    private String studentNo;
    private String className;
    private String politicalStatus;
    public CollegeFreshman() {}
    public CollegeFreshman(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
        super(name, age, gender, department, major);
        this.studentNo = studentNo;
        this.className = className;
        this.politicalStatus = politicalStatus;
    }
    public String getStudentNo() { return studentNo; }
    public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
    public String getClassName() { return className; }
    public void setClassName(String className) { this.className = className; }
    public String getPoliticalStatus() { return politicalStatus; }
    public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是大一学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
    }
}
// 大二学生类
class CollegeSophomore extends CollegeStudent {
    private String studentNo;
    private String className;
    private String politicalStatus;
    public CollegeSophomore() {}
    public CollegeSophomore(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
        super(name, age, gender, department, major);
        this.studentNo = studentNo;
        this.className = className;
        this.politicalStatus = politicalStatus;
    }
    public String getStudentNo() { return studentNo; }
    public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
    public String getClassName() { return className; }
    public void setClassName(String className) { this.className = className; }
    public String getPoliticalStatus() { return politicalStatus; }
    public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是大二学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
    }
}
// 大三学生类
class CollegeJunior extends CollegeStudent {
    private String studentNo;
    private String className;
    private String politicalStatus;
    public CollegeJunior() {}
    public CollegeJunior(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
        super(name, age, gender, department, major);
        this.studentNo = studentNo;
        this.className = className;
        this.politicalStatus = politicalStatus;
    }
    public String getStudentNo() { return studentNo; }
    public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
    public String getClassName() { return className; }
    public void setClassName(String className) { this.className = className; }
    public String getPoliticalStatus() { return politicalStatus; }
    public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是大三学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
    }
}
// 大四学生类
class CollegeSenior extends CollegeStudent {
    private String studentNo;
    private String className;
    private String politicalStatus;
    public CollegeSenior() {}
    public CollegeSenior(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
        super(name, age, gender, department, major);
        this.studentNo = studentNo;
        this.className = className;
        this.politicalStatus = politicalStatus;
    }
    public String getStudentNo() { return studentNo; }
    public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
    public String getClassName() { return className; }
    public void setClassName(String className) { this.className = className; }
    public String getPoliticalStatus() { return politicalStatus; }
    public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("我是大四学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
    }
}

#3
HAO遇见2023-04-08 20:26
回复 2楼 东海ECS
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2023-4-8 21:08编辑过]

#4
东海ECS2023-04-09 13:08
你添加了什么代码?
#5
HAO遇见2023-04-09 13:48
回复 4楼 东海ECS
我是用的notepad++  需要main方法 我不知道往哪加
#6
changen_java2023-10-10 12:24
缺少main方法
#7
yiyanxiyin2023-10-10 14:54
提示很明确了, 需要在Student类里面添加main方法, main如何定义已经给出了 ,  但入口点函数往往需要写在一个单独的类中, 或者你可以百度:java 入口函数
1