java,类的继承

public class Father {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Father()
    {
        System.out.println("父类的构造方法");
    }
    //工作
    public void work()
    {
        System.out.println("我劳动我光荣");
    }
    

}
public class TestJiCheng {

    public static void main(String[] args) {
        Father f=new Father();
        f.setName("父亲");
        f.setAge(50);
        System.out.println("名字是"+f.getName()+"年龄是"+f.getAge());
        f.work();
        Son s=new Son();
        s.setName("儿子");
        s.setAge(20);
        System.out.println("名字是"+s.getName()+"年龄是"+s.getAge());
        s.work();
        s.sing();

    }

}
public class TestJiCheng {

    public static void main(String[] args) {
        Father f=new Father();
        f.setName("父亲");
        f.setAge(50);
        System.out.println("名字是"+f.getName()+"年龄是"+f.getAge());
        f.work();
        Son s=new Son();
        s.setName("儿子");
        s.setAge(20);
        System.out.println("名字是"+s.getName()+"年龄是"+s.getAge());
        s.work();
        s.sing();

    }

}
public class Son extends Father {
    public Son()
    {
        System.out.println("子类的构造方法");
    }

    //Object a;所有类的父类
    public void sing()
    {
        System.out.println("我喜欢唱歌");
    }
    //覆盖(重写),子类要同名同参数父类才能形成覆盖
    public void work()
    {
        System.out.println("我不喜欢上班,我要去海选");
    }
}
原文地址:https://www.cnblogs.com/jiang2538406936/p/5250983.html