实验一
package FuCircle;
public class Circle {
public int r;
public double s;
//无参构造方法
public Circle(){}
//有参构造方法
public Circle(int r,int s) {
this.r=r;
this.s=s;
}
//求圆的面积
public double getarea() {
s=r*r;
s*=3.14;
return s;
}
}
package ZiCylinder;
import FuCircle.Circle;//调包
public class Cylinder extends Circle{//继承
private int h;
private double v;
public Cylinder() {}
public Cylinder(int h) {
this.h=h;
}
public void seth(int h) {
this.h=h;
}
public int geth() {
return h;
}
public double getvolume() {
v=s*h;
return v;
}
}
package ZiCylinder;
public class CylinderDemo {
public static void main(String[] args) {
Cylinder cr =new Cylinder();//创建对象
cr.seth(3);//调用set方法给圆柱高h赋值
System.out.println("圆的半径是:"+cr.geth());
//给圆的半径r赋值
cr.r=3;
//获取圆的面积s并输出
System.out.println("圆的面积是:"+cr.getarea());
//获取圆柱的体积v并输出
System.out.println("圆柱体体积是:"+cr.getvolume());
}
}
实验二
package Employee;
public class Employee {
String name;
int birthdaymonth;
int Basesalary;
int salary;
public Employee() {}
public Employee(String name,int birthdaymonth) {
this.name=name;
this.birthdaymonth=birthdaymonth;
}
public int getsalay(int month) {
if(month==birthdaymonth) {
salary+=100;
}
return salary;
}
public void setname(String name ) {
this.name=name;
}
public String getname() {
return name;
}
public void setbirthdaymonth( int birthdaymonth) {
this.birthdaymonth=birthdaymonth;
}
public int getbirthdaymonth() {
return birthdaymonth;
}
public void show() {
System.out.println("name:"+name);
System.out.println("birthdaymonth:"+birthdaymonth);
System.out.println("Basesalary:"+Basesalary);
System.out.println("salary:"+salary);
}
}
package Employee;
public class SalariedEmployee extends Employee{
int Fixedsalary;
int monthsalary;
public SalariedEmployee() {}
public SalariedEmployee(int Fixedsalary,int monthsalary) {
this.Fixedsalary=Fixedsalary;
this.monthsalary=monthsalary;
}
public int getSalary(int month) {
if(month==birthdaymonth) {
Fixedsalary+=100;
}
monthsalary=Fixedsalary;
return monthsalary;
}
public void show() {
System.out.println("name:"+name);
System.out.println("birthdaymonth:"+birthdaymonth);
System.out.println("Fixedsalary:"+Fixedsalary);
System.out.println("monthsalary:"+monthsalary);
}
}
package Employee;
public class HourlyEmployee extends Employee{
int Hourlywage;
int WorkingHours;
int wages;
public HourlyEmployee(){}
public HourlyEmployee(int Hourlywage,int WorkingHours,int wages){
this.Hourlywage=Hourlywage;
this.WorkingHours=WorkingHours;
this.wages=wages;
}
public int getSalary(int month) {
wages=Hourlywage*WorkingHours;
if(WorkingHours>=160) {
wages*=1.5;
}
if(month==birthdaymonth) {
wages+=100;
}
return wages;
}
public void setWorkingHours(int WorkingHours) {
this.WorkingHours=WorkingHours;
}
public int getWorkingHours() {
return WorkingHours;
}
public void show() {
System.out.println("name:"+name);
System.out.println("birthdaymonth:"+birthdaymonth);
System.out.println("Hourlywage:"+Hourlywage);
System.out.println("WorkingHours:"+WorkingHours);
System.out.println("wages:"+wages);
}
}
package Employee;
public class SalesEmployee extends Employee {
int MonthlySalas;
double Commissionrate;
double wages;
public SalesEmployee() {}
public SalesEmployee(int MonthlySalas,int Commissionrate,int wages) {
this.MonthlySalas=MonthlySalas;
this.Commissionrate=Commissionrate;
this.wages=wages;
}
public double getSalry(int month){
if(MonthlySalas>=60000) {
Commissionrate=0.125;
}
else {
Commissionrate=0.1;
}
wages=MonthlySalas*Commissionrate;
if(month==birthdaymonth) {
wages+=100;
}
return wages;
}
public void setMonthlySalas(int MonthlySalas ) {
this.MonthlySalas=MonthlySalas;
}
public int getMonthlySalas() {
return MonthlySalas;
}
public void show(){
System.out.println("name:"+name);
System.out.println("birthdaymonth:"+birthdaymonth);
System.out.println("MonthlySalas:"+MonthlySalas);
System.out.println("Commissionrate:"+Commissionrate);
System.out.println("wages"+wages);
}
}
package Employee;
import java.util.Scanner;
public class EmployeeDemo {
public static void main(String[] args) {
System.out.println("输入月份");
//输入月份
Scanner sc= new Scanner(System.in);
int month=sc.nextInt();
//父类无参数
System.out.println("父类Employee");
System.out.println("无参数");
Employee E=new Employee();
E.name="小明";
E.birthdaymonth=6;
E.Basesalary=1000;
E.getsalay(month);
E.show();
System.out.println("==========");
//有参数
System.out.println("有参数");
Employee E1=new Employee("小明",6 );
E1.Basesalary=1000;
E.getsalay(month);
E1.show();
System.out.println("==================");
//子类SalariedEmployee 无参数
System.out.println("子类SalariedEmployee");
System.out.println("无参数");
SalariedEmployee SE = new SalariedEmployee();
SE.name="小明";
SE.birthdaymonth=6;
SE.Fixedsalary=1000;
SE.monthsalary=0;
SE.getSalary(month);
SE.show();
System.out.println("==========");
//有参数
System.out.println("有参数");
SalariedEmployee SE1 = new SalariedEmployee(1000,0);
SE1.name="小明";
SE1.birthdaymonth=6;
SE1.getSalary(month);
SE1.show();
System.out.println("==================");
//子类HourlyEmployee 无参数
System.out.println("子类HourlyEmployee");
System.out.println("无参数");
HourlyEmployee he =new HourlyEmployee();
he.name="小明";
he.birthdaymonth=6;
he.Hourlywage=10;
he.WorkingHours=150;
he.wages=0;
he.getSalary(month);
he.show();
System.out.println("==========");
//有参数
System.out.println("有参数");
HourlyEmployee he1 =new HourlyEmployee(10,160,0);
he1.name="小明";
he1.birthdaymonth=6;
he1.getSalary(month);
he1.show();
System.out.println("==================");
//子类 SalesEmployee无参数
System.out.println("子类SalesEmployee");
System.out.println("无参数");
SalesEmployee se =new SalesEmployee();
se.name="小明";
se.birthdaymonth=6;
se.MonthlySalas=50000;
se.Commissionrate=0;
se.wages=0;
se.getSalry(month);
se.show();
System.out.println("==========");
//有参数
System.out.println("有参数");
SalesEmployee se1 =new SalesEmployee(60000,0,0);
se1.name="小明";
se1.birthdaymonth=6;
se1.getSalry(month);
se1.show();
System.out.println("==================");
}
}
创作不易给个免费的点赞谢谢!