->记录锚点
面向对象编程-overrider
- 编写一个Person类,包括属性/private(name,age),构造器,方法say(返回自我介绍的字符串)。
- 编写一个Student类,继承Person类,增加id,score属性/private,以及构造器,定义say方法(返回自我介绍的信息)
- 在main中,分别创建Person和Student对象,调用say方法输出自我介绍
提示知识点
super关键字
封装
通关源码
Person类
package com.tao.overrider_.homework;
public class Person {
private String name;
private int 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 Person(String name,int age){
this.name = name;
this.age = age;
}
public void say(){
System.out.println("我的名字是:" + name + "\n我的年龄是:" + age);
}
}
Student类
package com.tao.overrider_.homework;
public class Student extends Person{
private int id;
private int score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name,int age,int id,int score){
super(name,age);
this.id = id;
this.score = score;
}
public void say() {
super.say();
System.out.println("id为:" + id + "\n成绩为:" + score);
}
}
Test类
package com.tao.overrider_.homework;
public class Test {
public static void main(String[] args) {
Person person = new Person("小李",22);
Student 小张 = new Student("小张", 18, 5522, 90);
小张.say();
}
}
->返回
|