设计人(Person)及学生(Student)两个类,Student类继承自Person类。

设计人(Person)及学生(Student)两个类,Student类继承自Person类。其中:
一、Person类包含如下内容:
1、私有成员变量:姓名、年龄。
2、无参数的构造方法,和有参数的构造方法,实现构造方法的重载。
3、获取和设置私有成员变量的方法(getter、setter)。以及打招呼(sayHello)方法
二、Student类继承自Person类,且包含如下内容:
1、私有成员变量:语文、数学、外语成绩(int类型即可)。
2、无参数的构造方法,和有参数的构造方法,有参数的构造方法需要使用super关键字调用父类的构造方法。
3、获取和设置私有成员变量的方法(getter、setter)。以及计算总分(sum)的方法。
三、主类的main方法中,定义两个学生类的对象,分别使用这两个构造方法进行初始化操作,使用无参数构造函数时需要使用setter方法初始化变量;接下来调用打招呼以及计算总分两个方法,输出两个学生对象的总成绩,运行结果类似于:
My name is , my total score is .


class Person{
    private String name;
    private int age;
    
    public Person() {}
    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }
}
public class People {
    private String name;
    private int age;

    public People() {
    }

    public People(String name, int age) {
        this.name = name;
        this.age = 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;
    }

    public void sayHello() {
        System.out.println("My Name is " + name + ",and I am " + age + " years old!");
    }
}

public class Student extends People {
    private int chineseScore;
    private int mathScore;
    private int foreignScore;

    public Student() {
    }

    public Student(String name, int age, int chineseScore, int mathScore, int foreignScore) {
        super(name, age);
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.foreignScore = foreignScore;
    }

    public int getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(int chineseScore) {
        this.chineseScore = chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }

    public int getForeignScore() {
        return foreignScore;
    }

    public void setForeignScore(int foreignScore) {
        this.foreignScore = foreignScore;
    }

    public void sum() {
        System.out.println("My total score is " + (chineseScore + mathScore + foreignScore));
    }

    public static void main(String[] args) {
        Student student1 = new Student();
        student1.setName("张三");
        student1.setAge(16);
        student1.setChineseScore(80);
        student1.setMathScore(80);
        student1.setForeignScore(80);
        student1.sayHello();
        student1.sum();
        Student student2 = new Student("李四", 17, 90, 90, 90);
        student2.sayHello();
        student2.sum();
    }
}

参考示例代码如下:

class Person{
    private String name;
    private String addr;
    private char sex;
    private int age;
    //4参的构造方法
    public  Person(String name,String addr,char sex,int age){
        this.setName(name);
        this.setAddr(addr);
        this.setSex(sex);
        this.setAge(age);
    }
    //2参的构造方法
    public  Person(String name,String addr){
        this.setName(name);
        this.setAddr(addr);
    }
    //无参的构造方法
    public  Person(){
        
    }
    //输出方法
    public String  getInfo(){
        return this.getName()+ this.getAddr() + this.getSex() + this.getAge();        
    }
    public void sayHello(){
        System.out.println("你好");        
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name =name;
    }
    public String  getAddr(){
        return addr;
    }
    public void setAddr(String addr){
        this.addr = addr;
    }
    public char getSex(){
        return sex;
    }
    public void setSex(char sex){
        this.sex =sex;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age =age;
    }
}
 
class Student extends Person{
    private float math;
    private float english;
    //6参的构造方法
    public Student(String name,String addr,char sex,int age,float math,float english){
        super(name,addr,sex,age);        
        this.setMath(math);
        this.setEnglish(english);
    }
    //2参的构造方法
    public Student(float math ,float english){
        this.setMath(math);
        this.setEnglish(english);
        
    }
    //无参的构造方法
    public Student (){
        
    }
    //覆写输出方法
    public String getInfo(){
        return "姓名:"+super.getName()+"\n"+
               "地址:"+super.getAddr()+"\n"+
               "性别:"+super.getSex()+"\n" +
               "年龄:"+super.getAge() +"\n"+
               "数学成绩:"+this.getMath() +"\n"+ 
               "英语成绩:"+this.getEngish();    
    }    
    
    public float getMath(){
        return math;
    }
    public void setMath(float math){
        this.math = math;
    }
    public float getEngish(){
        return english;
    }
    public void setEnglish(float english){
        this.english =english;
    }
}
 
public class Test{
    public static void main(String args[]){
        //调用6参的构造函数
        Student stu = new Student("lzg","ynufe",'m',21,100,90);
        System.out.println(stu.getInfo());
//        //调用2参的构造函数
//        Student stu = new Student(80,90);
//        System.out.println(stu.getInfo());
        
    }
}