标题:Java继承类有没有懂的
只看楼主
HAO遇见
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2023-4-7
结帖率:100%
 问题点数:0 回复次数:4 
Java继承类有没有懂的
设计一 个学生类Student,并派出小学生,中学生,大学生

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

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

小学生属性:学号,年级

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

大学生属性:系别,专业

大一,二,三,四属性:学号,班级,政治面貌
搜索更多相关主题的帖子: 属性 学生 Java 大学生 方法 
2023-04-08 14:26
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:19
帖 子:376
专家分:1454
注 册:2023-1-24
得分:0 
实例代码:
程序代码:
// 学生类
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);
    }
}


会当凌绝顶,一览众山小.
2023-04-08 18:11
HAO遇见
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2023-4-7
得分:0 
回复 2楼 东海ECS


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

2023-04-08 20:26
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:19
帖 子:376
专家分:1454
注 册:2023-1-24
得分:0 
你添加了什么代码?

会当凌绝顶,一览众山小.
2023-04-09 13:08
HAO遇见
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2023-4-7
得分:0 
回复 4楼 东海ECS
我是用的notepad++  需要main方法 我不知道往哪加
2023-04-09 13:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511837-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.466795 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved