编程需求
需求如下: 编写父类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();
}
}
总结
希望对大家有帮助!!!