Java面向对象编程的题目

定义一个人类
属性包括:姓名,年龄,性别
属性私有化,对外提供公开的set和get方法
提供无参数构造器和有参数构造器
提供一个生病方法:ill(),在改方法中打印信息,例如:张三,男,29岁生病了

再定义一个类
属性包括:病毒名称name ,病毒体积size,病毒类型type
属性私有化,对外提供公开的set和get方法
提供无参数结构和有参数结构
提供一个攻击attack()方法,该方法的参数是“人”,例如attack(Person p)
在attack()方法中调用人对象的生病方法。
编写测试程序,创建病毒对象,创建人对象,模拟病毒攻击人。


public class Person {
    private String name;
    private Integer age;
    private String gender;

    public Person() {

    }

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

    public void ill() {
        System.out.println(name + ", " + gender + ", " + age + "岁生病了");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

public class Virus {
    private String name;
    private Integer size;
    private String type;

    public Virus() {

    }

    public Virus(String name, int size, String type) {
        this.name = name;
        this.size = size;
        this.type = type;
    }

    public void attack(Person p) {
        System.out.println(name + " 攻击 " + p.getName());
        p.ill();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

public class TestVirus {
    public static void main(String[] args) {
        Virus virus = new Virus("新冠病毒", 10, "冠状病毒");
        Person person = new Person("张三 ", 29, "男");
        virus.attack(person);
    }
}

img


public class person{
//属性
private String name;
private String sex;
private int age;
//方法
}