class test{
public static void main(String[] args){
Student s=new Student();
Person p=s;//另一种写法Person p=new Student();
p.name="张三";
p.age=20;
//p.address="湖北";//p是Person类型
//Person中没有address 这个变量,所以
//是错误的
p.introduce();
// 一个引用能够调用哪些成员(变量,和函
//函数),取决于这个引用的类型;
//一个引用能调动哪些方法,取决于这个引用所以指向的对象
}
}
class Person{
String name;
int age;
void introduce(){
System.out.println(name);
}
}
class Student extends Person{
String address;
void study(){
System.out.println("我正在学习");
}
void introduce(){
super.introduce();
System.out.println("my home");
}
}
/*
向下转型 ,将父类的对象赋值给子类的引用
Student s1=new Student();
Person p=s1;
Sudent s2=(Student)p;
另外一种写法
Person p=new Student();
Student s=(Student)p;
错误的写法:
Person p=new Person();
Student s=(Student)p;
*/
public static void main(String[] args){
Student s=new Student();
Person p=s;//另一种写法Person p=new Student();
p.name="张三";
p.age=20;
//p.address="湖北";//p是Person类型
//Person中没有address 这个变量,所以
//是错误的
p.introduce();
// 一个引用能够调用哪些成员(变量,和函
//函数),取决于这个引用的类型;
//一个引用能调动哪些方法,取决于这个引用所以指向的对象
}
}
class Person{
String name;
int age;
void introduce(){
System.out.println(name);
}
}
class Student extends Person{
String address;
void study(){
System.out.println("我正在学习");
}
void introduce(){
super.introduce();
System.out.println("my home");
}
}
/*
向下转型 ,将父类的对象赋值给子类的引用
Student s1=new Student();
Person p=s1;
Sudent s2=(Student)p;
另外一种写法
Person p=new Student();
Student s=(Student)p;
错误的写法:
Person p=new Person();
Student s=(Student)p;
*/