建了一个群:908722740 ,欢迎小伙伴门的加入,平时可以互相学习交流,不管是学习还是工作上的都可以多多交流,本人在校学生,技术有限,错误的地方欢迎指正。
目录
【1】部门和员工
要求:
1、请使用面向对象的思想设计部门和员工信息
2、分析部门和员工的属性和方法,设计部门类和员工类
3、在员工类中通过属性描述所在部门
4、利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
5、编写测试类,分别测试部门类,员工类的对象及相关方法 (测试数据信息自定)
6、定义名为emp的包存放部门类,员工类和测试类
参考分析思路:
部门类:
属性:部门编号,部门名称
员工类:
属性:姓名,性别,年龄,所在部门,职务
方法:自我介绍
运行效果:
/**
* 部门类
*/
public class Department {
private int departmentId;
private String departmentName;
public Department(int departmentId, String departmentName) {
this.departmentId = departmentId;
this.departmentName = departmentName;
}
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
/**
* 员工类
*/
public class Staff {
private String name;
private String sex;
private int age;
Department departmentname;
private String zhiwu;
public Staff(String name, String sex, int age, Department departmentname, String zhiwu) {
this.name = name;
this.sex = sex;
this.age = age;
this.departmentname = departmentname;
this.zhiwu = zhiwu;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Department getDepartmentname() {
return departmentname;
}
public void setDepartmentname(Department departmentname) {
this.departmentname = departmentname;
}
public String getZhiwu() {
return zhiwu;
}
public void setZhiwu(String zhiwu) {
this.zhiwu = zhiwu;
}
public void information() {
System.out.println("大家好!我叫" + name + ",今年" + age + "岁");
System.out.println("我现在任职在" + departmentname.getDepartmentName() + ",是" + zhiwu);
System.out.println("==========================================");
}
}
public class Test1 {
public static void main(String[] args) {
Department d = new Department(001, "人事部");
Staff s = new Staff("张晓彤", "女", 36, d, "人事部经理");
Department d2 = new Department(001, "人事部");
Staff s2 = new Staff("李大宝", "男", 24, d2, "人事部助理");
s.information();
s2.information();
}
}
【2】出租车和家用轿车
要求:
- 请使用面向对象的思想,设计自定义类,描述出租车和家用轿车
- 分析出租车和家用轿车的公共成员,提取出父类---汽车类
- 利用继承机制,实现出租车类和家用轿车类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问 5、 编写测试类,分别测试汽车类,出租车类和家用轿车类的对象及相关方法(测试数据信息自定)
- 定义名为car的包存放汽车类,出租车类、家用轿车类和测试类
参考分析思路:
出租车类:
属性包括:车型,车牌,所属出租公司
方法包括:启动、停止
家用轿车类:
属性包括:车型,车牌,车主姓名
方法包括:启动、停止
运行效果:
/**
* 父类
*/
public class Car {
private String chex;
private String chep;
public Car() {
}
public Car(String chex, String chep) {
this.chex = chex;
this.chep = chep;
}
public String getChex() {
return chex;
}
public void setChex(String chex) {
this.chex = chex;
}
public String getChep() {
return chep;
}
public void setChep(String chep) {
this.chep = chep;
}
public void start() {//启动
System.out.println("我是车,我启动!");
}
public void stop() {//停止
System.out.println("我是车,我停止!");
}
}
/**
* 轿车类
*/
public class Jiaoche extends Car {
private String name;
public Jiaoche(String chex, String chep, String name) {
super(chex, chep);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void start() {
System.out.println("我是" + name + ",我的汽车我做主");
}
public void stop() {
System.out.println("目的地到了,我们去玩吧");
}
}
/**
* 出租车类
*/
public class Taxi extends Car {
private String carcompany;
public Taxi(String chex, String chep, String carcompany) {
super(chex, chep);
this.carcompany = carcompany;
}
public String getCarcompany() {
return carcompany;
}
public void setCarcompany(String carcompany) {
this.carcompany = carcompany;
}
public void start() {
System.out.println("乘客你好");
System.out.println("我是" + carcompany + "的,我的车牌是" + this.getChep() + ",你要去哪里?");
}
public void stop() {
System.out.println("目的地到了,请您付费下车,欢迎再次乘坐!");
}
}
/**
* 测试
*/
public class Test {
public static void main(String[] args) {
Car c = new Car();
c.start();
c.stop();
System.out.println("===============================");
Jiaoche j = new Jiaoche("轿车","京A001","武大郎");
j.start();
j.stop();
System.out.println("=================================");
Taxi t = new Taxi("出租车","京B123","景顺出租车公司");
t.start();
t.stop();
}
}
【3】计算机和CPU
要求:
- 请使用面向对象的思想,设计自定义类,描述计算机和CPU
- 以属性的方式描述CPU和计算机的关系
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
- 在计算机的运行方法中,体现出“计算机的运行实际上是CPU在工作”的原理
- 编写测试类,分别测试CPU类,计算机类的对象及相关方法(测试数据信息自定)
- 定义名为comp的包存放CPU类,计算机类和测试类
参考分析思路:
CPU类:
属性包括:主频、核心数量、型号
方法包括:工作
计算机类:
属性包括:CPU、内存容量、硬盘容量,型号
方法包括:运行
运行效果:
/**
* 计算机类
*/
public class Computer {
private CPU CPU;
private String nMb;
private String yMb;
private String jsjtype;
public Computer(com.jr.dz315work3.CPU CPU, String nMb, String yMb, String jsjtype) {
this.CPU = CPU;
this.nMb = nMb;
this.yMb = yMb;
this.jsjtype = jsjtype;
}
public com.jr.dz315work3.CPU getCPU() {
return CPU;
}
public void setCPU(com.jr.dz315work3.CPU CPU) {
this.CPU = CPU;
}
public String getnMb() {
return nMb;
}
public void setnMb(String nMb) {
this.nMb = nMb;
}
public String getyMb() {
return yMb;
}
public void setyMb(String yMb) {
this.yMb = yMb;
}
public String getJsjtype() {
return jsjtype;
}
public void setJsjtype(String jsjtype) {
this.jsjtype = jsjtype;
}
public void start(){
System.out.println(jsjtype+"开机运行");
CPU.work();
}
}
/**
* CPU类
*/
public class CPU {
private String zhuPin;
private String hxnumber;
private String cputype;
public CPU(String zhuPin, String hxnumber, String cputype) {
this.zhuPin = zhuPin;
this.hxnumber = hxnumber;
this.cputype = cputype;
}
public String getZhuPin() {
return zhuPin;
}
public void setZhuPin(String zhuPin) {
this.zhuPin = zhuPin;
}
public String getHxnumber() {
return hxnumber;
}
public void setHxnumber(String hxnumber) {
this.hxnumber = hxnumber;
}
public String getCputype() {
return cputype;
}
public void setCputype(String cputype) {
this.cputype = cputype;
}
public void work(){
System.out.println(cputype+"开始工作。。。。");
System.out.println("主频:"+zhuPin);
System.out.println("核心数量:"+hxnumber);
}
}
/**
* 测试
*/
public class Test {
public static void main(String[] args) {
CPU c=new CPU("2600","双核心","Intel奔腾双核E5300");
Computer c2=new Computer(c,"512GB","1TB","戴尔530MT");
c2.start();
}
}
【4】卡车和小轿车
要求:
- 请用面向对象的思想设计卡车类和小轿车类
- 分析卡车和小轿车的公共成员,提取出父类---汽车类
- 利用继承机制,实现卡车类和小轿车类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
- 租赁的方法通过输入参数,接收租车人姓名和租赁时间,描述租赁状态。
- 编写测试类,分别测试卡车类和小轿车类的对象及相关方法(测试数据信息自定)
- 定义名为bus的包存放汽车类,卡车类、小轿车类和测试类
参考分析思路:
卡车类:
属性包括:车牌号、车型、颜色、日租金、载重量
方法包括:租赁(租金计算:10天内按日租金计算,10天以上每天每吨加收10%)
小轿车类:
属性包括:车牌号、车型、颜色、日租金
方法包括:租赁(租金计算:10天内按日租金计算,10天以上每天加收10%)
运行效果:
/**
* 父类
*/
public class Car {
private String platenumber;
private String cartype;
private String colour;
private double daymoney;
public Car(String platenumber, String cartype, String colour, double daymoney) {
this.platenumber = platenumber;
this.cartype = cartype;
this.colour = colour;
this.daymoney = daymoney;
}
public String getPlatenumber() {
return platenumber;
}
public void setPlatenumber(String platenumber) {
this.platenumber = platenumber;
}
public String getCartype() {
return cartype;
}
public void setCartype(String cartype) {
this.cartype = cartype;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public double getDaymoney() {
return daymoney;
}
public void setDaymoney(double daymoney) {
this.daymoney = daymoney;
}
}
/**
* 轿车类
*/
public class Jiaoche extends Car {
public Jiaoche(String platenumber, String cartype, String colour, double daymoney) {
super(platenumber, cartype, colour, daymoney);
}
public void zuping(String name2, int days2){
System.out.println("租凭信息");
System.out.println("车牌号:"+getPlatenumber());
System.out.println("车型:"+getCartype());
System.out.println("颜色:"+getColour());
System.out.println("租车人:"+name2);
System.out.println("应付金额:"+(10*getDaymoney()+(days2-10)*(getDaymoney()+0.1*getDaymoney())));
}
}
/**
* 卡车类
*/
public class Kache extends Car {
private double weight;
public Kache(String platenumber, String cartype, String colour, double daymoney, double weight) {
super(platenumber, cartype, colour, daymoney);
this.weight = weight;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void zuping(String name,int days){
System.out.println("租凭信息");
System.out.println("车牌号:"+getPlatenumber());
System.out.println("车型:"+getCartype());
System.out.println("颜色:"+getColour());
System.out.println("载重量:"+weight);
System.out.println("租车人:"+name);
System.out.println("应付金额:"+(10*getDaymoney()+(days-10)*(getDaymoney()+0.1*weight*getDaymoney())));
}
}
/**
* 测试
*/
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入租车人姓名:");
String name = input.next();
System.out.println("请输入租凭天数:");
int days = input.nextInt();
Kache k=new Kache("京D1111","东风","蓝色",100,500);
k.zuping(name, days);
System.out.println("=======================");
System.out.println("请输入租车人姓名:");
String name2 = input.next();
System.out.println("请输入租凭天数:");
int days2 = input.nextInt();
Jiaoche j=new Jiaoche("京R00000","奥迪A6","黑色",100);
j.zuping(name2, days2);
}
}
【5】麻雀和企鹅
要求:
- 请使用面向对象的思想,设计自定义类描述麻雀和企鹅的信息
- 分析麻雀和企鹅的公共成员,提取出父类---鸟类
- 利用继承机制,实现麻雀类和企鹅类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问 5、 编写测试类,分别测试鸟类,麻雀类和企鹅类的对象及相关方法(测试数据信息自定) 6、 定义名为bid的包存放鸟类、麻雀类、企鹅类和测试类
参考分析思路:
麻雀类:
属性包括:名字,颜色
方法包括:飞,吃
企鹅类:
属性包括:名字,颜色
方法包括:飞,吃,游泳
运行效果:
public class Bird {//鸟类
private String name;
private String colour;
public Bird() {
}
public Bird(String name, String colour) {
this.name = name;
this.colour = colour;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public void fei() {
System.out.println("我是鸟,我会飞!");
}
public void eat() {
System.out.println("我是鸟,我吃的是饭不是寂寞!");
System.out.println("=================================");
}
}
public class Penguin extends Bird {//企鹅
public Penguin(String name, String colour) {
super(name, colour);
}
public void fei() {
System.out.println("我是企鹅" + this.getName() + ",我太胖了,所以不会飞");
}
public void eat() {
System.out.println("我喜欢吃各种各样的!");
}
public void swim() {
System.out.println("我虽然很胖,但我比,,,,,游的快");
}
}
public class Sparrow extends Bird {//麻雀
public Sparrow(String name, String colour) {
super(name, colour);
}
public void fei(){
System.out.println("我是麻雀"+this.getName()+",我动作敏捷,飞的比火箭快");
}
public void eat(){
System.out.println("我不仅吃种子,也吃害虫!");
System.out.println("==================================");
}
}
public class Test {
public static void main(String[] args) {
Bird b = new Bird();
b.fei();
b.eat();
Sparrow s = new Sparrow("嘟嘟", "灰色");
s.fei();
s.eat();
Penguin p = new Penguin("QQ", "白色");
p.fei();
p.eat();
p.swim();
}
}
【6】汽车和发动机
要求:
- 请使用面向对象的思想,设计自定义类,描述汽车和发动机的信息
- 以属性的方式描述发动机和汽车的关系
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
- 在汽车的运行方法中,体现出“汽车的运行实际上是发动机在工作”的原理
- 编写测试类,分别测试汽车类,发动机类的对象及相关方法(测试数据信息自定)
- 定义名为com的包存放汽车类,发动机类和测试类
参考分析思路:
发动机类:
属性包括:排量、汽缸数、型号
方法包括:工作
汽车类:
属性包括:发动机、车牌、车型
方法包括:运行
运行效果:
public class Car {//汽车类
private Engine Engine;
private String chep;
private String chex;
public Car(com.jr.dz315work6.Engine engine, String chep, String chex) {
Engine = engine;
this.chep = chep;
this.chex = chex;
}
public com.jr.dz315work6.Engine getEngine() {
return Engine;
}
public void setEngine(com.jr.dz315work6.Engine engine) {
Engine = engine;
}
public String getChep() {
return chep;
}
public void setChep(String chep) {
this.chep = chep;
}
public String getChex() {
return chex;
}
public void setChex(String chex) {
this.chex = chex;
}
public void start(){
System.out.println("车牌为:"+chep+"的"+chex+"启动。。。。");
getEngine().work();
}
}
public class Engine {//发动机类
private String pailiang;
private int qigangnumber;
private String engineType;
public Engine(String pailiang, int qigangnumber, String engineType) {
this.pailiang = pailiang;
this.qigangnumber = qigangnumber;
this.engineType = engineType;
}
public String getPailiang() {
return pailiang;
}
public void setPailiang(String pailiang) {
this.pailiang = pailiang;
}
public int getQigangnumber() {
return qigangnumber;
}
public void setQigangnumber(int qigangnumber) {
this.qigangnumber = qigangnumber;
}
public String getEngineType() {
return engineType;
}
public void setEngineType(String engineType) {
this.engineType = engineType;
}
public void work(){
System.out.println("发动机开始工作");
System.out.println("型号:"+engineType);
System.out.println("排量:"+pailiang);
System.out.println("气缸数:"+qigangnumber);
}
}
public class Test {
public static void main(String[] args) {
Engine e = new Engine("2350", 4, "三菱4G63");
Car c = new Car(e, "京D588", "桑塔纳2000");
c.start();
}
}
【7】蛇怪和蜈蚣精
要求:
- 请用面向对象的思想,设计游戏中的蛇怪和蜈蚣精。
- 分析蛇怪和蜈蚣精的公共成员,提取出父类---怪物类
- 利用继承机制,实现蛇怪类和蜈蚣精类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问 5、 攻击方法,描述攻击状态。内容包括怪物名字,生命值,攻击力
- 编写测试类,分别测试蛇怪和蜈蚣精的对象及相关方法(测试数据信息自定)
- 定义名为mon的包存放怪物类,蛇怪类、蜈蚣精类和测试类
参数分析思路:
蛇怪类:
属性包括:怪物名字、生命值、攻击力
方法包括:攻击、移动(曲线移动)、补血(当生命值<10时,可以补加20生命值)
蜈蚣精类:
属性包括:怪物名字、生命值、攻击力
方法包括:攻击、移动(飞行移动)
运行效果:
/**
* 父类
*/
public class Monster {
private String name;
private int hp;
private int gjili;
public Monster(String name, int hp, int gjili) {
this.name = name;
this.hp = hp;
this.gjili = gjili;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getGjili() {
return gjili;
}
public void setGjili(int gjili) {
this.gjili = gjili;
}
public void attack(){//攻击
System.out.println("怪物"+name+"展开攻击");
System.out.println("当前生命值是:"+hp);
System.out.println("攻击力是:"+gjili);
}
public void move(){//移动
}
}
/**
* 蛇类
*/
public class Snake extends Monster {
public Snake(String name, int hp, int gjili) {
super(name, hp, gjili);
}
public void move(){
System.out.println("我是蛇,我走S线路!");
}
public void addhp(){
if (this.getHp()<10){
this.setHp(getHp()+20);
System.out.println("实施大蛇补血术。。。,当前生命值是"+this.getHp());
}
}
}
/**
* 蜈蚣类
*/
public class Wugong extends Monster{
public Wugong(String name, int hp, int gjili) {
super(name, hp, gjili);
}
public void move(){
System.out.println("我是蜈蚣精,我御风飞行!");
}
}
public class Test {
public static void main(String[] args) {
Snake s=new Snake("怪蛇甲",5,20);
s.attack();
s.addhp();
s.move();
System.out.println("==========================");
Wugong w=new Wugong("蜈蚣精已",60,15);
w.attack();
w.move();
}
}
【8】学生和老师
要求:
- 请使用面向对象的思想,设计自定义类描述学生和老师类
- 分析学生和老师的公共成员,提取出父类---人类
- 利用继承机制,实现学生类和老师类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问 5、 编写测试类,分别测试人类,学生类和老师类的对象及相关方法(测试数据信息自定)
参考分析思路:
学生类:
属性包括:姓名,年龄,性别,学号
方法包括:学习,睡觉
老师类:
属性包括:姓名,年龄,性别,所教科目,教龄
方法包括:授课,睡觉
运行效果:
/**
* 父类
*/
public class Person {
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void sleep() {//睡觉
System.out.println("我是普通人,要工作才有饭吃!");
System.out.println("我是普通人,吃饱了就睡觉!");
System.out.println("=================================");
}
}
/**
* 学生类
*/
public class Student extends Person {
private String id;
public Student(String name, int age, String sex, String id) {
super(name, age, sex);
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void xuexi(){
System.out.println("我叫"+this.getName()+",今年"+this.getAge()+"岁");
System.out.println("我的学号是S18901");
System.out.println("作为学生,我的职责就是:好好学习,天天向上");
}
public void sleep(){
System.out.println("作为一个好学生,一定要写完作业再睡觉");
System.out.println("=========================================");
}
}
/**
* 老师类
*/
public class Teacher extends Person{
private String kemu;
private int workage;
public Teacher(String name, int age, String sex, String kemu, int workage) {
super(name, age, sex);
this.kemu = kemu;
this.workage = workage;
}
public String getKemu() {
return kemu;
}
public void setKemu(String kemu) {
this.kemu = kemu;
}
public int getWorkage() {
return workage;
}
public void setWorkage(int workage) {
this.workage = workage;
}
public void work(){
System.out.println(this.getName()+"老师,今年"+this.getAge()+"岁");
System.out.println("教授"+kemu+",他具有"+workage+"年教龄");
}
public void sleep(){
System.out.println("作为一个好老师,一定要先备课,再睡觉");
System.out.println("=========================================");
}
}
public class Test {
public static void main(String[] args) {
Person p=new Person();
p.sleep();
Teacher t=new Teacher("齐天海",42,"男","中国近代史",15);
t.work();
t.sleep();
Student s=new Student("刘新",23,"男","S18901");
s.xuexi();
s.sleep();
}
}
【9】演员和运动员
要求:
- 请使用面向对象的思想,设计自定义类描述演员和运动员
- 分析演员和运动员的公共成员,提取出父类---人类
- 利用继承机制,实现演员类和运动员类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
- 编写测试类,分别测试人类,演员类和运动员类的对象及相关方法(测试数据信息自定)
- 定义名为act的包存放人类、演员类、运动员类和测试类
参考分析思路:
演员类:
属性包括:姓名,年龄,性别,毕业院校,代表作
方法包括:自我介绍
运动员类:
属性包括:姓名,年龄,性别,运动项目,历史最好成绩
方法包括:自我介绍
运行效果:
/**
* 父类
*/
public class Person {
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void information() {//自我介绍
System.out.println("我就是一个普通老百姓");
System.out.println("==================================");
}
}
/**
* 演员类
*/
public class Actor extends Person {
private String school;
private String masterwork;
public Actor(String name, int age, String sex, String school, String masterwork) {
super(name, age, sex);
this.school = school;
this.masterwork = masterwork;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getMasterwork() {
return masterwork;
}
public void setMasterwork(String masterwork) {
this.masterwork = masterwork;
}
public void information(){
System.out.println("大家好!我是"+this.getName());
System.out.println("今年"+this.getAge());
System.out.println("我毕业于:"+school);
System.out.println("代表作:"+masterwork);
}
}
/**
* 运动员类
*/
public class Sportsman extends Person {
private String sportevent;//运动项目
private String score;
public Sportsman(String name, int age, String sex, String sportevent, String score) {
super(name, age, sex);
this.sportevent = sportevent;
this.score = score;
}
public String getSportevent() {
return sportevent;
}
public void setSportevent(String sportevent) {
this.sportevent = sportevent;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public void information(){
System.out.println("大家好!我是"+this.getName());
System.out.println("今年"+this.getAge());
System.out.println("我擅长的运动项目是:"+sportevent);
System.out.println("历史最好成绩:"+score);
System.out.println("==========================================");
}
}
public class Test {
public static void main(String[] args) {
Person p=new Person();
p.information();
Sportsman s=new Sportsman("刘小翔",23,"男","200米短跑","22秒30");
s.information();
Actor a=new Actor("章依",27,"女","北京电影学院","《寄往天国的家书》");
a.information();
}
}
【10】医生和军人
要求:
- 请使用面向对象的思想,设计自定义类描述医生和军人类
- 分析医生和军人的公共成员,提取出父类---人类
- 利用继承机制,实现医生类和军人类
- 利用封装机制,设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问 5、 编写测试类,分别测试人类,医生类和军人类的对象及相关方法(测试数据信息自定) 6、 定义名为doc的包存放人类、医生类、军人类和测试类
参考分析思路:
医生类:
属性包括:姓名,年龄,性别,所在科室,职务
方法包括:工作
军人类: 属性包括:姓名,年龄,性别,所属部队,职务,军龄
方法包括:工作
运行效果:
/**
* 父类
*/
public class Person {
private String name;
private int age;
private String sex;
private String department;//所在地方
private String zhiwu;
private String occupation;//职业
public Person() {
}
public Person(String name, int age, String sex, String department, String zhiwu, String occupation) {
this.name = name;
this.age = age;
this.sex = sex;
this.department = department;
this.zhiwu = zhiwu;
this.occupation = occupation;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getZhiwu() {
return zhiwu;
}
public void setZhiwu(String zhiwu) {
this.zhiwu = zhiwu;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public void work() {
System.out.println("我是普通人,我要工作才有饭吃");
System.out.println("============================");
}
}
/**
* 医生类
*/
public class Doctor extends Person {
public Doctor(String name, int age, String sex, String department, String zhiwu, String occupation) {
super(name, age, sex, department, zhiwu, occupation);
}
public void work() {
System.out.println("姓名:" + this.getName());
System.out.println("职业:" + this.getOccupation());
System.out.println("年龄:" + this.getAge());
System.out.println("所在科室:" + this.getDepartment());
System.out.println("职务:" + this.getZhiwu());
System.out.println("==========================================");
}
}
/**
* 军人类
*/
public class Soldier extends Person {
private int junage;
public Soldier(String name, int age, String sex, String department, String zhiwu, String occupation, int junage) {
super(name, age, sex, department, zhiwu, occupation);
this.junage = junage;
}
public int getJunage() {
return junage;
}
public void setJunage(int junage) {
this.junage = junage;
}
public void work() {
System.out.println("姓名:" + this.getName());
System.out.println("职业:" + this.getOccupation());
System.out.println("年龄:" + this.getAge());
System.out.println("所属部队:" + this.getDepartment());
System.out.println("职务:" + this.getZhiwu());
System.out.println("军龄:" + this.junage);
System.out.println("==========================================");
}
}
public class Test {
public static void main(String[] args) {
Person p=new Person();
p.work();
Soldier s=new Soldier("刘天明",62,"男","北京军区","军区司令员","军人",43);
s.work();
Doctor d=new Doctor("刘新",23,"男","内科","主任医师","医生");
d.work();
}
}