目录
练习一:宠物类
设计一个宠物(Pets)类,包含昵称(Nickname),年龄(age),体重(weight),品种(type)等属性,
及日常行为(跑(run),吃(eat),睡(sleep),玩()),实现并测试这个类
package com.company.pets;
/**
设计一个宠物(Pets)类
包含属性:
昵称(nickname)
年龄(age)
体重(weight)
及日常行为:
跑(run)
吃(eat)
睡(sleep)
实现并测试这个类
*/
public class Pets {
private String nickname;
private String age;
private String weight;
public void run() {
System.out.println(nickname+"跑!");
}
public void eat() {
System.out.println(nickname+"吃!");
}
public void sleep() {
System.out.println(nickname+"睡!");
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
package com.company.pets;
//测试类
public class MainPets {
public static void main(String[] args) {
Pets pets = new Pets();
pets.setNickname("二哈");
pets.run();
pets.eat();
pets.sleep();
}
}
练习二:图形类
定义一个图形类,带有面积计算函数,
在此基础上派生出矩形类及圆形类,二者重写父类的面积计算函数来求面积。
package com.company.graph;
//图形类
public class Graph {
//面积计算
public Double area() {
return null;
}
}
package com.company.graph;
//矩形类
public class Rectangular extends Graph{
private double length;
private double width;
public Double area(){
return length*width;
}
}
package com.company.graph;
//圆形类
public class Circular extends Graph{
private double radius;
public Double area() {
return Math.PI*radius*radius;
}
}
练习三:员工类
1、定义员工类
属性:姓名,基本工资
方法: 计算工资 ,返回这个员工的基本工资。
2、子类:小时工类
属性:工作的小时,每小时工资。
方法:
1) 设置工作的小时
2) 设置每小时的工资。
3) 重写计算工资方法:
小时工没有基本工资,他的工资就是工作的小时*每小时工资。
3、子类:销售员工类
属性:销售的总金额
方法
1) 设置销售的总金额
2) 重写计算工资方法: 基本工资+提成。
提成的计算:
当销售额在10000 以下时,则提成10%,
当销售额在10000-100000之间时,则提成15%。
当销售额在100000以上时,提成18%。
4、子类: 普通员工类
属性: 工作的小时
方法:
1) 设置工作的小时
2) 重写计算工资的方法:
当每月工作的小时数超过196 小时时,超出的部分按每小时200 元。
基本工资+(工作的小时-196)*200。
5、在 main 函数中生成这三种员工的对象,并调用计算工资的方法。
package com.company.employee;
/**
1、定义员工类
属性:姓名,基本工资
方法: 计算工资 ,返回这个员工的基本工资。
*/
public class Employee {
private String name; //姓名
private double basePrice; //基本工资
//计算工资
public Double checkPrice() {
return basePrice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasePrice() {
return basePrice;
}
public void setBasePrice(double basePrice) {
this.basePrice = basePrice;
}
}
package com.company.employee;
/**
2、子类:小时工类
属性:工作的小时,每小时工资。
方法:
1. 设置工作的小时
2. 设置每小时的工资。
3. 重写计算工资方法:
小时工没有基本工资,他的工资就是工作的小时*每小时工资。
*/
public class EmpHours extends Employee{
private Integer hours; //工作小时
private Double price4hours; //每小时的工资
public Double checkPrice() {
return hours * price4hours;
}
public Integer getHours() {
return hours;
}
public void setHours(Integer hours) {
this.hours = hours;
}
public Double getPrice4hours() {
return price4hours;
}
public void setPrice4hours(Double price4hours) {
this.price4hours = price4hours;
}
}
package com.company.employee;
/**
3、子类:销售员工类
属性:销售的总金额
方法
1) 设置销售的总金额
2) 重写计算工资方法: 基本工资+提成。
提成的计算:
当销售额在10000 以下时,则提成10%,
当销售额在10000-100000之间时,则提成15%。
当销售额在100000以上时,提成18%。
*/
public class EmpSaler extends Employee {
private Double sumSaler;
public Double checkPrice() {
double t=0 ;
//计算提成
if(sumSaler<10000) t = sumSaler*0.1;
else if(sumSaler>100000) t = sumSaler*0.18;
else t = sumSaler*0.15;
return getBasePrice()+t;
}
public Double getSumSaler() {
return sumSaler;
}
public void setSumSaler(Double sumSaler) {
this.sumSaler = sumSaler;
}
}
package com.company.employee;
/**
4、子类: 普通员工类
属性: 工作的小时
方法:
1) 设置工作的小时
2) 重写计算工资的方法:
当每月工作的小时数超过196 小时时,超出的部分按每小时200 元。
基本工资+(工作的小时-196)*200。
*/
public class EmpOrdinary extends Employee {
private Integer hours4month;
public Double checkPrice() {
double t=0; //计算加班工资
if (hours4month > 196) t = (hours4month-196)*200;
return getBasePrice()+t;
}
public Integer getHours4month() {
return hours4month;
}
public void setHours4month(Integer hours4month) {
this.hours4month = hours4month;
}
}
package com.company.employee;
/**
5、在 main 函数中生成这三种员工的对象,并调用计算工资的方法。
*/
public class MainEmp {
public static void main(String[] args) {
EmpHours empHours = new EmpHours();
//EmpSaler empSaler = new EmpSaler();
//EmpOrdinary empOrdinary = new EmpOrdinary();
// 填写数据,计算工资
empHours.setName("张三");
empHours.setHours(12);
empHours.setPrice4hours((double) 15);
System.out.println(empHours.getName()+"工资:"+empHours.checkPrice());
}
}