- public class Test_Person {
- public static void main(String[] args) {
- Student s = new Student("张三", 23); // 调用构造函数赋值
- System.out.println(s.getName() + "..." + s.getAge());
- s.video();// 重写的video()
- s.study(); // 学生的study()
- System.out.println("----------------------------");
- Teacher t = new Teacher(); // 调用setXXX()赋值
- t.setName("波多老师");
- t.setAge(24);
- System.out.println(t.getName() + "..." + t.getAge());
- t.video();// 重写的video()
- t.teach(); // 老师的teach()
- }
- }
- class Person { // 定义Preson父类
- private String name;
- private int age;
- public Person() {
- }
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public void video() {
- System.out.println("日本影视");
- }
- }
- class Teacher extends Person { // 定义Teacher类继承Person类
- public Teacher() {
- }
- public Teacher(String name, int age) {
- super(name, age); // 调用父类有参构造函数
- }
- public void video() { // 重写父类video()方法
- System.out.println("喜欢拍日本电影");
- }
- public void teach() { // 创建成员方法teach()
- System.out.println(this.getName() + "老师喜欢教日本文化");
- }
- }
- class Student extends Person { // 定义Student类继承Person类
- public Student() {
- }
- public Student(String name, int age) { // 调用父类有参构造函数
- super(name, age);
- }
- public void video() { // 重写父类video()方法
- System.out.println("喜欢看日本电影");
- }
- public void study() { // 创建成员方法study()
- System.out.println(this.getName() + "喜欢学习日本文化");
- }
- }
复制代码 |
|