⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 person6.java

📁 Java Classic Examples是我买的两本书:《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();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -