定义一个人类
属性包括:姓名、年龄、性别
属性私有化,对外提供公开的set和get方法
提供无参数构造和有参数构造
提供一个生病方法:ill(),在该方法中打印信息,例如:张三,男,29岁生病了
定义一个病毒类virus
属性包括:病毒名称name、病毒体积size、病毒类型type
属性私有化,对外提供公开的set和get方法
提供无参数构造和有参数构造
提供一个攻击attack()方法,该方法的参数是“人”,例如:attack(Person p)
在attack()方法中调用人对象的生病方法。
编写测试程序,创建病毒对象,创建人对象,模拟病毒攻击人。
public class peopleTest {
public static void main(String[] args) {
Person p = new Person("张三",29,'男');
Virus v = new Virus("大肠杆菌",0.00054,"寄生病毒");
v.attack(p);
}
}
//定义一个人类
class Person{
private String name;
private int age;
private char sex;
//无参构造方法
public Person() {
}
//有参构造方法
public Person(String name,int age,char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
//getter and setter
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 char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public void ill(){
System.out.println("姓名:"+this.getName()+" 性别:"+this.getSex()+"年龄:"+this.getAge()+",生病了!");
}
}
//定义一个病毒类
class Virus{
private String name;
private double size;
private String type;
//无参构造方法
public Virus() {
}
//有参构造方法
public Virus(String name,double size,String type) {
this.name = name;
this.size = size;
this.type = type;
}
//getter and setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void attack(Person p){
p.ill();
}
}
截图: