标签:name int age 练习 public Person 重写 id
要求:
编写一个Person类,包括属性/privte(name,age),构造器、方法say(返回自我介绍1字符串)。
编写一个Student类,继承Person类,增加id、scoer属性/private,以及构造器,定义一say方法(返回自我介绍的信息)。
在main中,分别创建Person和Student的对象,调用say方法输出自我介绍。
package src.com.qiQi.oop.Demo06;
//子类
public class Student extends Person{
private int id;
private double score;
public Student(String name, int age, int id, double score) {
super(name, age);//这里会调用父类构造器
this.id = id;
this.score = score;
}
//方法 这个方法重写了Person 并且增加了一些东西
public String say(){ //super 的一个作用 复用
return super.say() + " 学号:" + id + " 成绩:" + score;
}
//get set
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
/*
package src.com.qiQi.oop.Demo06;
//父类
public class Person {
public String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//方法
public String say(){
return "姓名:" + name + " 年龄:" +age ;
}
//get set
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;
}
*/
/*
package src.com.qiQi.oop;
import src.com.qiQi.oop.Demo06.Person;
import src.com.qiQi.oop.Demo06.Student;
//测试类
public class Application {
public static void main(String[] args) {
Person lala = new Person("lala", 7);
System.out.println(lala.say());
Student su = new Student("suSu",37,7,77.7);
System.out.println(su.say());
}
}
*/
标签:name,int,age,练习,public,Person,重写,id 来源: https://www.cnblogs.com/shuqiqi/p/16463724.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。