有同学可以帮我看一下我写的是否正确嘛
题目:完成一个多态作为参数的使用的demo
自由发挥,做出demo,注意两种方式
1,子类重写父类抽象方法
2,子类自带的方法(强制转换)
代码:package com.dmd;
public class Person {
public String name;
public int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public Person(){
}
public void getInfo(){
System.out.println(name);
System.out.println(age);
}
}
package com.dmd;
public class Student extends Person{
String school;
public Student(String name,int age,String school){
super(name,age);
this.school=school;
}
public Student(){
}
public void study() {
System.out.println("好好学习,天天向上!");
}
public void getInfo() {
super.getInfo();
System.out.println(school);
}
public static void main(String[] args) {
Person p=new Person();
p.name="person";
p.age=40;
p.getInfo();
Student s=new Student();
s.name="student";
s.age=20;
s.school="清华大学";
s.getInfo();
s.study();
}
}