JAVA编程——父子类

编程需求

需求如下: 编写父类People,子类Student继承自People类。父类People具有姓名,性别,年龄等性质,还具有吃和说的行为。子类Student继承父类People,还拥有学号性质和学习行为。构造People类和Student类的对象,调用吃、说、学习的方法输出有关信息

代码描述:

第一个类

class People {
    protected static String name;
    protected static String sex;
    protected static int age;

    public void eat(){
        System.out.println("吃吃");
    }
    public void speak(){
        System.out.println("喝喝");
    }

    public People (String name,String sex,int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}

class Student extends People {
    protected long stuNo;

    public void eat() {
        System.out.println("饭饭");
    }

    public void speak() {
        System.out.println("话话");
    }

    public void learn() {
        System.out.println("学学");
    }
    public Student(String name,String sex,int age,long
                   stuNoNo){
        super(name,sex,age);
        this.stuNo =stuNoNo;
    }
}

第二个类

public class Test {
    public static void main(String[] args) {
        Student student = new Student("大丘丘","boy", 1, 0000);
        People people = new People("二丘丘","boy", 2);
        System.out.println("学生:");
        System.out.println("姓名"+student.name);
        System.out.println("性别"+student.sex);
        System.out.println("年纪"+student.age);
        System.out.println("学号"+student.stuNo);
        student.eat();
        student.speak();
        student.learn();
        System.out.println("普通人:");
        System.out.println("姓名:"+People.name);
        System.out.println(""+people.name);
        System.out.println("年龄:"+people.sex);
        people.eat();
        people.speak();
    }
}

总结

希望对大家有帮助!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值