package lianxii;
import java.util.Date;
public class Person2 {
public String name;
public int age;
private Date birthDate;
public String getInfo() {
return "name:" + name + "\nage:" + age;
}
}
//类Person2
package lianxii;
public class Student2 extends Person2 {
private String school = "BeiJing";
private String className = "errr";
public String getSchool() {
return school;
}
@Override
public String getInfo() {
return super.getInfo() + "\nschool:" + school + "\nclassName:" + className;
}
public static void main(String[] args) {
Student2 y = new Student2();
y.school = "NanJing11";
y.className = "ChuYi";
System.out.println(y.getInfo() + "\n");
Person2 t = new Person2();
t.age = 23;
t.name = "zhong";
System.out.println(t.getInfo() + "\n");
//多态的情况下
Person2 p = new Student2();
System.out.println(p.getInfo() + "\n");
}
}
//类Student2
//一个引用类型变量如果声明为父类的类型,但实际引用的是子类对象,那么该变量就不能再访问子类中添加的方法和属性
package lianxii;
public class Test {
public String method(Person2 r) {//形参
return r.getInfo();
}
public static void main(String[] args) {
Test u = new Test();
Student2 p = new Student2();
u.method(p);//实参
System.out.println(u.method(p));
}
}
//测试类Test
//方法声明的参数类型为父类类型,可以使用子类的对象作为实参调用该方法
import java.util.Date;
public class Person2 {
public String name;
public int age;
private Date birthDate;
public String getInfo() {
return "name:" + name + "\nage:" + age;
}
}
//类Person2
package lianxii;
public class Student2 extends Person2 {
private String school = "BeiJing";
private String className = "errr";
public String getSchool() {
return school;
}
@Override
public String getInfo() {
return super.getInfo() + "\nschool:" + school + "\nclassName:" + className;
}
public static void main(String[] args) {
Student2 y = new Student2();
y.school = "NanJing11";
y.className = "ChuYi";
System.out.println(y.getInfo() + "\n");
Person2 t = new Person2();
t.age = 23;
t.name = "zhong";
System.out.println(t.getInfo() + "\n");
//多态的情况下
Person2 p = new Student2();
System.out.println(p.getInfo() + "\n");
}
}
//类Student2
//一个引用类型变量如果声明为父类的类型,但实际引用的是子类对象,那么该变量就不能再访问子类中添加的方法和属性
package lianxii;
public class Test {
public String method(Person2 r) {//形参
return r.getInfo();
}
public static void main(String[] args) {
Test u = new Test();
Student2 p = new Student2();
u.method(p);//实参
System.out.println(u.method(p));
}
}
//测试类Test
//方法声明的参数类型为父类类型,可以使用子类的对象作为实参调用该方法