您当前的位置: 百科371电脑/网络JAVA相关 → 电脑/网络内容 退出登录 用户管理 用户注册
本类热门文章
相关文章
中原建材网
Java 中关于继承:父类和子类之间的关于成员的互相调用,以及抽象类和父类的对象的实现(详细介绍)?

减小字体 增大字体

问题:Java 中关于继承:父类和子类之间的关于成员的互相调用,以及抽象类和父类的对象的实现(详细介绍)?  

有段代码自己看下
public class Person6
{
static int count=0;
protected String name;
protected int age;
public Person6(String n1,int a1)
{
name = n1;
age = a1;
this.count++; //超类对象计数
}
public String toString()
{
return this.name+", "+this.age;
}
public void print()
{
System.out.println("本类名="+this.getClass().getName()+" "+
"超类名="+this.getClass().getSuperclass().getName()+" ");
System.out.print("Person6.count="+this.count+" ");
System.out.print("Student6.count="+Student6.count+" ");
Object s1=this;
if (s1 instanceof Person6) //判断对象属于哪个类
System.out.println(s1.toString()+"是Person6类对象。");
if (s1 instanceof Student6)
System.out.println(s1.toString()+"是Student6类对象。");
}
}
class Student6 extends Person6
{
static int count=0; //隐藏了超类的count
protected String dept;
protected Student6(String n1,int a1,String d1)
{
super(n1,a1); //调用超类的构造方法
dept = d1;
this.count++; //子类对象计数
}
public String toString() //覆盖超类的同名方法
{
return super.toString() +", " + dept; //调用超类的同名方法
}
public void print()
{
super.print(); //调用超类的方法
System.out.println("super.count = "+super.count); //引用超类变量
System.out.println("this.count = "+this.count);
}
public static void main(String args[])
{
Person6 p1 = new Person6("王小明",21) ;
p1.print();
Student6 s1 = new Student6("陈小瑞",19,"计算机系");
s1.print();
}
}

如果你对Java 中关于继承:父类和子类之间的关于成员的互相调用,以及抽象类和父类的对象的实现(详细介绍)?这个问题有好的意见或 建议,请留言
[] [返回上一页] [打 印]
电脑/网络评论 (评论内容只代表网友观点,与本站立场无关!)

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码

中原建材网 简单版 站长QQ:382546553