当前位置:   article > 正文

1.定义Person类 属性: 姓名name、性别gender、年龄age、国籍nationality; 方法:吃饭eat、睡觉sleep,工作work。_定义person类,属性:姓名:name

定义person类,属性:姓名:name

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public class Person {

/*
姓名
*/
private String name;
/*年


* /
private int age;
/

* 国际*/
private String nationality;

public Person() {
}


public Person(String name, int age, String nationality) {
    this.name = name;
    this.age = age;
    this.nationality = nationality;
}


@Override
public String toString() {
    return "Student{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", nationality='" + nationality + '\'' +
            '}';
}

public void eat(){
    /*
     * 吃方法*/
}
public void sleep(){
    /*睡觉方法*/

}

public void work(){
    /*工作方法*/

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

}

public class Student extends Person {

private String school;
private int stuNumber;

public Student(String 小斌, int i) {
}


@Override
public void work() {
    System.out.println("学生的工作是学习");
    super.work();
}

public Student(String name, int age, String nationality) {
    super(name, age, nationality);
}


public String getSchool() {
    return school;
}

public void setSchool(String school) {
    this.school = school;
}

public int getStuNumber() {
    return stuNumber;
}

public void setStuNumber(int stuNumber) {
    this.stuNumber = stuNumber;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

}

public class StudentLeader extends Person{

@Override
public void work() {
    System.out.println("学生会的工作是开会");
    super.work();
}

private String job;


public StudentLeader(String 学生会主席) {
}

public StudentLeader(String name, int age, String nationality) {
    super(name, age, nationality);
}


public String getJob() {
    return job;
}

public void setJob(String job) {
    this.job = job;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

}

public class Worker extends Person {

private String unit;
private  int workAge;


public Worker(String 小名, int i) {
}

public Worker(String name, int age, String nationality) {
    super(name, age, nationality);
}


public String getUnit() {
    return unit;
}

public void setUnit(String unit) {
    this.unit = unit;
}

public int getWorkAge() {
    return workAge;
}

public void setWorkAge(int workAge) {
    this.workAge = workAge;
}

@Override
public void work() {
    System.out.println("工人的工作是盖房子");
    super.work();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

}

public static void main(String[] args) {

    Student s = new Student("小李",2016094949);
    s.eat();
    s.sleep();

    s.work();

    Worker w = new Worker("大力士",165464);
    w.work();

    StudentLeader st = new StudentLeader("学生会主席");

    st.work();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/136567
推荐阅读
相关标签
  

闽ICP备14008679号