`
ahjdzx1990
  • 浏览: 24506 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Java面向对象

阅读更多

一:

1.写一个形状类Shape 有两个方法一个求周长,一个求面积

2.写一个长方形类Rect继承于形状类 增加属性长和宽 分别去覆盖求周长和求面积的方法

3.写一个圆形类Circle 增加属性半径 分别去覆盖求周长和求面积的方法

4.写一个测试类,在测试类中分别创建不同的对象放入进一个Shape 数组中进行,循环数组中的元素求周长和面积

二:

某公司的雇员分为以下若干类:

Employee:这是所有员工总的父类,属性:员工的姓名和生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。

SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪

HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数

SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定属性:月销售额、提成率 BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分 属性:底薪。

 

一、TestShape类:

package cn.com.corejava.homework;

public class TestShape{
	public static void main(String[] args){
		Shape[] ss=new Shape[3];
		ss[0]=new Rect(5,2);
		ss[1]=new Square(4);
		ss[2]=new Circle(10);
		
		for(int i=0;i<ss.length;i++){
			System.out.println(ss[i].line());
			System.out.println(ss[i].area());
		}
	}	
}
class Shape{
	public Shape(){}
	public double line(){
		return 0.0d;
	}
	public double area(){
		return 0.0d;
	}
}
class Rect extends Shape{
	private double x;
	private double y;
	public Rect(){}
	public Rect(double x,double y){
		this.x=x;
		this.y=y;
	}
	public double line(){
		return (x+y)*2;
	}
	public double area(){
		return x*y;
	}
}
class Square extends Rect{
	private double x;
	public Square(){}
	public Square(double x){
		this.x=x;
	}
	public double line(){
		return x*4;
	}
	public double area(){
		return x*x;
	}
}
class Circle extends Shape{
	private double r;
	public Circle(){}
	public Circle(double r){
		this.r=r;
	}
	public double line(){
		return 2*r*Math.PI;
	}
	public double area(){
		return r*r*Math.PI;
	}
}

 

运行结果:

14.0
10.0
16.0
16.0
62.83185307179586
314.1592653589793

 

 

 二、TestEmployee类:

package cn.com.corejava.homework;

public class TestEmployee {
	public static void main(String[] args){
		Employee[] es=new Employee[4];
		es[0]=new SalariedEmployee("allan",8,50000);
		es[1]=new HourlyEmployee("icexu",8,20,300);
		es[2]=new SalesEmployee("xbz",2,100000,0.1);
		es[3]=new BasedPlusSalesEmployee("sss",8,50000,0.3,8000);
		
		for(int i=0;i<es.length;i++){
			System.out.println(es[i].getName()+":"+es[i].getSalary(8));
		}
	}
}
class Employee{
	private String name;
	private int birth;
	public Employee(String name, int birth) {
		super();
		this.name = name;
		this.birth = birth;
	}
	/*
	 * 所有员工的共性
	 * 判断是否过生日
	 */
	public double getSalary(int month){
		if (month==birth) return 100;
		else return 0;
	}
	public String getName(){
		return this.name;
	}
	
}
class SalariedEmployee extends Employee{
	private double salary;
	/*
	 用super关键字把name和birth传给父类构造方法
	 去设置父类的属性
	*/
	public SalariedEmployee(String name,int birth,double salary){
		super(name,birth);
		this.salary=salary;
	}
	public double getSalary(int month){
		return salary+super.getSalary(month);
	}
	
}
class HourlyEmployee extends Employee{
	private double salaryPerHour;
	private int hours;
	public HourlyEmployee(String name, int birth, double salaryPerHour, int hours) {
		super(name, birth);
		this.salaryPerHour = salaryPerHour;
		this.hours = hours;
	}
	public double getSalary(int month){
		double result=0;
		if (hours<=160) result=this.salaryPerHour*this.hours;
		else result=this.salaryPerHour*160+this.salaryPerHour*(this.hours-160)*1.5;
		return result+super.getSalary(month);
	}
}
class SalesEmployee extends Employee{
	private int sales;
	private double rate;
	public SalesEmployee(String name, int birth, int sales, double rate) {
		super(name, birth);
		this.sales = sales;
		this.rate = rate;
	}
	public double getSalary(int month){
		return sales*rate+super.getSalary(month);
	}
}
class BasedPlusSalesEmployee extends SalesEmployee{
	private double basedSalary;

	public BasedPlusSalesEmployee(String name, int birth, int sales, double rate, double basedSalary) {
		super(name, birth, sales, rate);
		this.basedSalary = basedSalary;
	}
	
	public double getSalary(int month){
		return this.basedSalary+super.getSalary(month);
	}
}

 

运行结果:

allan:50100.0
icexu:7500.0
xbz:10000.0
sss:23100.0

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics