Discuss / Java / 差点把我绕晕

差点把我绕晕

Topic source

/**

 * Learn Java from https://www.liaoxuefeng.com/

 * 

 * @author liaoxuefeng

 */

public class Main {

public static void main(String[] args) {

Person p = new Person("小明", 12);

Student s = new Student("小红", 20, 99);

// TODO: 定义PrimaryStudent,从Student继承,新增grade字段:

PrimaryStudent ps = new PrimaryStudent("小军", 9, 100, 5);

System.out.println(p.getName()+ ' ' + p.getAge());

System.out.println(s.getName()+' ' + s.getAge()+ ' '+ s.getScore());

System.out.println(ps.getName()+' ' + ps.getAge()+ ' '+ ps.getScore() + ' '+ ps.getgrade());

}

}

//

public class Person {

protected String name;

protected int age;

public Person(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 class Student extends Person {

protected int score;

public Student(String name, int age, int score) {

super(name, age);

this.score = score;

}

public int getScore() {

return score;

}

}

//

public class PrimaryStudent extends Student{

protected int grade;

public PrimaryStudent(String name, int age, int score,int grade) {

super(name, age,score);

this.grade = grade;

}

public int getgrade() {

return grade;

}

}


  • 1

Reply