欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java基础之面向对象:继承,封装,多态

程序员文章站 2022-05-28 14:26:50
...

父类:学生
私有属性:名字 学号
方法:构造函数,
set/get test(int score) 判断是否及格

子类 大学生 (extends)
私有属性:主修专业/学分
方法:构造

public class Student {
    private String s_name;
    private String s_num;

    public String getS_num() {
        return s_num;
    }

    public void setS_num(String s_num) {
        this.s_num = s_num;
    }

    public String getS_name() {
        return s_name;
    }

    public void setS_name(String s_name) {
        this.s_name = s_name;
    }


    public Student(){};
    public Student( String s_name,String s_num){
        this.s_name=s_name;
        this.s_num=s_num;
    }

 public void test(int score){
        if((score<0)|(score>100)){
            System.out.println("成绩无效");
        }else{
            if(score>60){
                System.out.println("考试合格");
            }

     }
 }


}
public class CollegeStudent extends Student{
    private String major;
    private int credit;

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public int getCredit() {
        return credit;
    }

    public void setCredit(int credit) {
        this.credit = credit;
    }


    public CollegeStudent(){
        super();}

    public CollegeStudent(String s_name,String s_num,String major,int credit){
        super(s_name,s_num);
        this.major=major;
        this.credit=credit;
    }


    public void test(int score,int credit){
        if(score<0||score>100){
            System.out.println("成绩无效");
        }else{
            if(score>60){

                System.out.println("考试合格,学分增加:"+credit+"分");
                System.out.println();
                this.credit+=credit;
                System.out.println("现有学分:"+this.credit+"分");
            }else{
                System.out.println("考试不及格");
            }

        }
    }

}

相关标签: 从零开始java