| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6414 人关注过本帖
标题:Java继承类有没有懂的
只看楼主 加入收藏
HAO遇见
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2023-4-7
结帖率:100%
收藏
 问题点数:0 回复次数:6 
Java继承类有没有懂的
设计一 个学生类Student,并派出小学生,中学生,大学生

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

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

小学生属性:学号,年级

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

大学生属性:系别,专业

大一,二,三,四属性:学号,班级,政治面貌
搜索更多相关主题的帖子: 属性 学生 Java 大学生 方法 
2023-04-08 14:26
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册: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
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册: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
changen_java
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-10-10
收藏
得分:0 
缺少main方法
2023-10-10 12:24
yiyanxiyin
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:9
帖 子:238
专家分:1810
注 册:2023-6-29
收藏
得分:0 
提示很明确了, 需要在Student类里面添加main方法, main如何定义已经给出了 ,  但入口点函数往往需要写在一个单独的类中, 或者你可以百度:java 入口函数
2023-10-10 14:54
快速回复:Java继承类有没有懂的
数据加载中...
 
   



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

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