王雪奇

博客园 首页 新随笔 联系 订阅 管理

1.

设计三个类,分别如下:(知识点:抽象类及抽象方法) [必做题]
• 3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
• 3.2 设计 2个子类:
• 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
• 3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
• 3.3 在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

复制代码
package wang;

public abstract class Shape {
    double area;
    double per;
    char color;
    public Shape() {
        super();
    }
    public Shape(char color) {
        super();
        this.color = color;
    }
    public abstract double getPer();
    public abstract double getArea();
    public  abstract void showAll();
    public char getColor() {
        return color;
    }
    
}package www;

public class Rectangle extends Shape {
    @Override
    public double getPer() {
        // TODO Auto-generated method stub
        return (width + height) * 2;
    }

    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return width * height;
    }

    double width;
    double height;

    public Rectangle(double width, double height, char color) {
        super();
        this.width = width;
        this.height = height;
        this.color = color;
    }

    public Rectangle() {
        super();
    }
    public void showAll() {
        System.out.println("矩形的长是" + width + ",宽是" + height + ",颜色是" + color + ",周长是" + getPer() + ",面积是" + getArea());
    }
}
package wang;
public class Circle extends Shape{
    double radius;

    public void showAll(){
        System.out.println("圆形的半径是"+radius+",颜色是"+color+",周长是"+getPer()+",面积是"+getArea());
    }
    @Override
    public double getPer() {
        // TODO Auto-generated method stub
        return 2*3.14*radius;
    }
    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return 3.14*radius*radius;
    }
    public Circle(double radius,char color) {
        super();
        this.radius = radius;
        this.color=color;
    }
    
}package wang;

public class testshape {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根

        Rectangle r = new Rectangle(2, 3, '黑');
        r.showAll();
        Circle c = new Circle(4, '红');
        c.showAll();

    }

}
复制代码

 4

 

复制代码
package wang;

public class ColaEmployee {
    protected String name;
    protected int month;
    public ColaEmployee() {
        super();
    }
    public ColaEmployee(String name,int month) {
        super();
        this.name = name;
        this.month = month;
    }
    public double getSalary(int month) {
        return 0;
    }

}




package wang;

public class Company extends ColaEmployee {
    public void getSalary(ColaEmployee c,int month) {
        System.out.println(c.name+"在"+month+"月的月薪为");
    }

}


package wang;

public class HourlyEmployee extends ColaEmployee {
    protected int hourmoney;
    protected int daymonth;
    public HourlyEmployee() {
        super();
    }
    public HourlyEmployee(String name,int month, int hourmoney, int daymonth) {
        super(name, month);
        this.hourmoney=hourmoney;
        this.daymonth=daymonth;
    }
    public double getSalary(int month) {
        if(super.month==month) {
            if(daymonth>160) {
                return hourmoney * 160+hourmoney * (daymonth-160) * 1.5
                        +100;
            }else {
                return hourmoney * daymonth +100;
            }
        }else {
            if(daymonth>160) {
                return hourmoney * 160 + hourmoney * (daymonth-160) *1.5;
            }else {
                return hourmoney * daymonth;
            }
        }
    }

}

复制代码
public class SalariedEmployee extends ColaEmployee {
    protected double money;

    public SalariedEmployee() {
        super();
    }

    public SalariedEmployee(String name, int month, double money) {
        super(name, month);
        this.money = money;
    }

    public double getSalary(int month) {
        if (super.month == month) {
            return money + 100;
        } else {
            return money;
        }
    }

}
复制代码
复制代码
public class SalesEmployee extends ColaEmployee {
    protected int monthsales;
    protected double rate;

    public SalesEmployee() {
        super();
    }

    public SalesEmployee(String name, int month, int monthsales, double rate) {
        super(name, month);
        this.monthsales = monthsales;
        this.rate = rate;
    }

    public double getSalary(int month) {
        if (super.month == month) {
            return monthsales * rate + 100;
        } else {
            return monthsales * rate;
        }
    }

}
复制代码
复制代码
public class Testcompany {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ColaEmployee c1[] = { 
                new SalariedEmployee("小李",5,12000),
                new HourlyEmployee("小红",7,16,190),
                new SalesEmployee("小f",7,19999,6) };
        for (int i = 0; i < c1.length; i++) {
            new Company().getSalary(c1[i], 2);

        }
    }

}
复制代码

posted on 2023-07-05 20:46  雪.?!  阅读(7)  评论(0编辑  收藏  举报