第十七次作业

1、设计四个类,分别是:(知识点:抽象类及抽象方法)

(1)Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。

(2)2个子类:

1)Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。

2)Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

(3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

package mjm;

public abstract class Shape {
    protected double area;
    protected double per;
    protected String color;

    public Shape() {
        
    }

    public Shape(String color) {
        this.color = color;
    }

    public abstract void getarea();

    public abstract void getper();

    public abstract void getcolor();
    
    public abstract void showall();
    

}
package mjm;

public class Rectangle extends Shape {
    double Width;
    double height;

    public Rectangle() {

    }

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

    @Override
    public void getarea() {
        // TODO Auto-generated method stub
        area = (Width * height);

    }

    @Override
    public void getper() {
        // TODO Auto-generated method stub
        per = (Width + height) * 2;
    }

    @Override
    public void showall() {
        // TODO Auto-generated method stub
        System.out.println("面积是" + area + "周长是" + per + "颜色是" + color);

    }

    @Override
    public void getcolor() {
        // TODO Auto-generated method stub

    }

}
package mjm;

public class Circle extends Shape {
    double radius;

    public void Shape() {
        
    }

    public Circle(double radius, String color) {
        super(color);
        this.radius = radius;
        this.color = color;
    }

    @Override
    public void getarea() {
        // TODO Auto-generated method stub
        area = 3.14 * radius * radius;
    }

    @Override
    public void getper() {
        // TODO Auto-generated method stub
        per = 3.14 * radius * 2;
    }

    @Override
    public void getcolor() {
        // TODO Auto-generated method stub

    }

    @Override
    public void showall() {
        // TODO Auto-generated method stub
        System.out.println("面积是" + area + "周长是" + per + "颜色是" + color);
    }

}
package mjm;

public class PolyDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Shape a=new Rectangle(5.0,3.0,"蓝色");
        a.getarea();
        a.getper();
        a.showall();
        Shape b= new Circle(2.0,"红色");
        b.getarea();
        b.getper();
        b.showall();
        
    }

}

2、Cola公司的雇员分为以下若干类:(知识点:多态)

(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

(2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。

(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。

(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

package mmm;

public class ColaEmployee {
    String name;
    int month;

    public ColaEmployee() {
        super();
    }

    public ColaEmployee(String name, int month) {
        super();
        this.name = name;
        this.month = month;
    }

    public double getmoney(int month) {
        return 0;
    }

}
package mmm;

public class SalariedEmployee extends ColaEmployee {
    double money;

    public SalariedEmployee() {
        super();
    }

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

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

public class HourlyEmployee extends ColaEmployee {
    double hmoney;
    int hour;

    public HourlyEmployee() {
        super();
    }

    public HourlyEmployee(String name, int month, double hmoney, int hour) {
        super(name, month);
        this.hmoney = hmoney;
        this.hour = hour;
    }

    public double getmoney(int month) {
        if (super.month == month) {
            if (hour > 160) {
                return hmoney * 160 + (hour - 160) * 1.5 + 100;
            } else {
                return hmoney * hour + 100;
            }
        } else {
            if (hour > 160) {
                return hmoney * 160 + (hour - 160) * 1.5;
            } else {
                return hmoney * hour;
            }
        }
    }

}
package mmm;

public class SalesEmployee extends ColaEmployee {
    int sales;
    double rate;

    public SalesEmployee() {
        super();
    }

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

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

    }
}
package mmm;

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

public class TestCompany {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColaEmployee[] all = { 
                new SalariedEmployee("张一", 9, 6000), // name,month,月薪
                new HourlyEmployee("张二", 5, 120, 165), // name,month,时薪,小时数
                new SalesEmployee("张三",5,900,25)// name,month,月销售额,提成率
        };
        for (int i = 0; i < all.length; i++) {
            new Company().getmoney(all[i], 5);
        }
    }

}

3、

package mjm;

public interface Fruit {

}



package mjm;

public class apple implements Fruit {
    public apple() {
        System.out.println("创建了一个苹果类对象");
    }

}
package mjm;

public class banana implements Fruit {
    public banana() {
        System.out.println("创建了一个香蕉类对象");
    }
}
package mjm;

public class grape implements Fruit {
    public grape() {
        System.out.println("创建了一个葡萄类对象");
    }

}
package mjm;

import java.util.Scanner;

public class gardener {
    public void creater() {
        Scanner input = new Scanner(System.in);
        String a = input.next();
        if (a.equals("苹果")) {
            new apple();
        } else if (a.equals("香蕉")) {
            new banana();
        } else if (a.equals("葡萄")) {
            new grape();
        } else {
            System.out.println("请重新运行");
        }
    }

    public static void main(String[] args) {
        new gardener().creater();
    }
}

 

 

posted @ 2020-05-21 18:52  mjmmmmmua  阅读(144)  评论(0编辑  收藏  举报