class Student extends Person
{
String school;
int score;
//void sayHello(){
// System.out.println("Hello! My name is " + name + ". My school is " + school );
//} void sayHello( Student another ){
System.out.println("Hi!");
if( school == another.school ) System.out.println(" Shoolmates ");
} boolean isGoodStudent(){
return score>=90;
} void testThisSuper(){
int a;
a = age;
a = this.age;
a = super.age;
} void sayHello(){
super.sayHello();
System.out.println( "My school is " + school );
} Student(String name, int age, String school ){
super( name, age );
this.school = school;
} Student(){} public static void main( String [] arggs )
{
Person p = new Person( "Liming", 50 );
Student s = new Student( "Wangqiang", 20, "PKU" );
Person p2 = new Student( "Zhangyi", 18, "THU" );
Student s2 = (Student) p2;
p.sayHello();
p.sayHello(s);
s.sayHello();
s.sayHello(p);
s.sayHello(s2);
}
}
上面这个程序有两个类,一个Person类,一个Student类
运行Student类后,执行的结果是:
Hello! My name is Liming
Hello,Wangqiang! My name is Liming
Hello! My name is Wangqiang
My school is PKU
Hello,Liming! My name is Wangqiang
Hi!
这里有方法的继承和调用,谁能把结果详细的解析一下呢?最后那几个语法分别调用的是父类的方法吗?
s.sayHello();调用的是子类的方法吗?
{
String school;
int score;
//void sayHello(){
// System.out.println("Hello! My name is " + name + ". My school is " + school );
//} void sayHello( Student another ){
System.out.println("Hi!");
if( school == another.school ) System.out.println(" Shoolmates ");
} boolean isGoodStudent(){
return score>=90;
} void testThisSuper(){
int a;
a = age;
a = this.age;
a = super.age;
} void sayHello(){
super.sayHello();
System.out.println( "My school is " + school );
} Student(String name, int age, String school ){
super( name, age );
this.school = school;
} Student(){} public static void main( String [] arggs )
{
Person p = new Person( "Liming", 50 );
Student s = new Student( "Wangqiang", 20, "PKU" );
Person p2 = new Student( "Zhangyi", 18, "THU" );
Student s2 = (Student) p2;
p.sayHello();
p.sayHello(s);
s.sayHello();
s.sayHello(p);
s.sayHello(s2);
}
}
上面这个程序有两个类,一个Person类,一个Student类
运行Student类后,执行的结果是:
Hello! My name is Liming
Hello,Wangqiang! My name is Liming
Hello! My name is Wangqiang
My school is PKU
Hello,Liming! My name is Wangqiang
Hi!
这里有方法的继承和调用,谁能把结果详细的解析一下呢?最后那几个语法分别调用的是父类的方法吗?
s.sayHello();调用的是子类的方法吗?
解决方案 »
- 请问什么原因HTTP Status 404 - /webproject/RegServlet.aspx
- 阿拉伯数字转中文数字
- 当子类重写父类的方法时,是不是子类的方法的可访问权限一定要比父类的高啊?为什么啊?
- 100分求解决方案:用POI的HSSF写excel的问题:数据条数多的时候,会产生outofmemory错误~
- 关于OBJECT的一个问题。
- 来来来,帮忙介绍点资料。谢谢
- hash table转移
- Java中如何监控指定的类所创建的对象、所占用的内存的大小?
- 请教 如何得到一个string中包含几个某字符
- 兄弟们辛苦了.散发!
- 请教使用双缓冲区播放GIF动画的问题
- JDBC的问题!!!!!
String name;
int age; static int x; Person( String n, int a ){
name = n;
age = a;
} Person( String n ){
name = n;
age = -1;
} Person( int age, String name )
{
this.age = age;
this.name = name;
} Person( ){
this( 0, "" );
} void sayHello(){
System.out.println("Hello! My name is " + name );
} void sayHello( Person another ){
System.out.println("Hello," + another.name + "! My name is " + name );
} boolean isOlderThan( int anAge ){
boolean flg;
if( age > anAge ) flg = true; else flg=false;
return flg;
} public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
抱歉啊,疏忽了,这是Person类