person4.java

来自「JAVA 基础例题包含了JAVA常见的问题和常见的习题」· Java 代码 · 共 40 行

JAVA
40
字号
public class Person4
{
    static int count=0;
    protected String name;
    protected int age;
    public Person4(String n1,int a1)        //构造方法
    {
        this.name = n1;
        this.age = a1;
        this.count++;
    } 
    public int olderthen(Person4 b)         //比较两个人的年龄
    {
        Person4 a = this;                   //指代对象本身
        return a.age - b.age;
    }
    public void print()
    {
        System.out.print(this.getClass().getName()+"  ");
        System.out.print("count="+this.count+"    ");
        System.out.println("  "+this.name+", "+this.age);
    }
}
class Student4 extends  Person4   
{
    protected String dept;
    Student4(String n1,int a1,String d1)    //不能继承超类的构造方法
    {
        super(n1,a1);                       //调用超类的构造方法
        dept = d1;
    }    
    public static void main(String args[])
    {
        Person4 p1 = new Person4("李大广",21);
        p1.print();
        Student4 s1 = new Student4("陈小瑞",19,"计算机系") ; 
        s1.print();
        System.out.println("年龄差=  "+p1.olderthen(s1));
    }
}

⌨️ 快捷键说明

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