题目要求:
1.
定义一个父类
Person
类。有姓名、编号、类型三个属性。定义一个展示自己信息的方法。
2.
定义一个带参构造方法,为属性赋值。
3.
定义子类
worker
,继承自
Person
类。重写父类中的
speak( )
和
eat( )
方法。
4.
定义测试类
TestPerson
,创建
2
个
Worker
类对象,给对象赋值并打印
package com.zhou.com.Demo6;
public class Person {
public String name;
public int id;
private String type;
public Person() {
}
public Person(String name, int id, String type) {
this.name = name;
this.id = id;
this.type = type;
}
public void show(){
System.out.println("我是一位工人"+this.type+",我的名字叫"+this.name+",我的编号是"+this.id);
}
}
package com.zhou.com.Demo6;
public class Worker extends Person{
public Worker(String name, int id) {
super(name, id, "工人");
}
public void speak(){
System.out.println("我是"+this.name+",我在工地正在工作。");
}
public void eat(){
System.out.println("我是"+this.name+",我在吃热腾腾的饭菜");
}
}
package com.zhou.com.Demo6;
public class TestPerson {
public static void main(String[] args) {
Worker worker = new Worker("张三",1001);
Worker worker1 = new Worker("李四",1002);
worker.show();
worker.speak();
worker.eat();
worker1.show();
worker1.speak();
worker1.eat();
}
}