编写一个Person类,包括属性、参构造器、方法。 编写一个 Student类,继承Person类。

  • Post author:
  • Post category:其他



编写一个Person类,包括属性(name、age),有参构造器、方法say(返回自我介绍的字符串)。 编写一个Student类,继承Person类,增加sno、score属性,以及有参构造器,在子类中调用父类的构造器。编写一个方法showInfo(返回自我介绍的字符串)。

public class Person1 {
	private String name;
	private int age;

	public Person1(String name, int age) {
		super();
		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 String say() {
		return "我叫" + name + ",我今年" + age + "岁了,";
	}
	
	public static void main(String[] args) {
		Student s=new Student("camila",20,17111107,100);
		System.out.println(s.showInfo());
	}
}

class Student extends Person1 {
	private int sno;
	private int score;

	public Student(String name, int age, int sno, int score) {
		super(name, age);
		this.sno = sno;
		this.score = score;
	}

	public int getSno() {
		return sno;
	}

	public void setSno(int sno) {
		this.sno = sno;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public String showInfo() {
		return say()+"我的学号是:" + sno + ",我的分数是:" + score;
	}
		
}




版权声明:本文为Camila_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。