本帖最后由 黑马刘岩 于 2012-12-9 21:39 编辑
//定义一个父类
abstract class Student
{
long id;
String name;
Student(long id,String name)
{
this.id=id;
this.name=name;
}
abstract void study();
public void show(long id,String name)
{
System.out.println("id:"+id+" name:"+name);
}
}
//定义一个子类
class Student1 extends Student
{
Student1(long id,String name)
{
super(id,name);
}
void study()
{
this.show(id,name);
}
}
//定义一个子类
class Student2 extends Student
{
Student2(long id,String name)
{
super(id,name);
}
void study()
{
super.show(id,name);
}
}
public class ExtendsDemo
{
public static void main(String[] args)
{
Class1 c1=new Class1(123456789,"leo");
Class2 c2=new Class2(987654321,"yean");
c1.study();
c2.study();
}
}
注意红色代码部分,用this和super都能得到想要的结果,但是区别在哪里?用哪个比较好?
|