• 第17次作业


    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 d18;
    
    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 d18;
    
    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 d18;
    
    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 d18;
    
    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 d19;
    
    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 d19;
    
    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 d19;
    
    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 d19;
    
    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 d19;
    
    public class Company {
         public void getmoney(ColaEmployee c,int month) {
                System.out.println(c.name + "在" + month + "月的月薪为" + c.getmoney(month)+"元");
           }
    }
    package d19;
    
    public class TestCompany {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ColaEmployee[] all = { 
                    new SalariedEmployee("张三", 5, 9000), // name,month,月薪
                    new HourlyEmployee("李四", 5, 120, 165), // name,month,时薪,小时数
                    new SalesEmployee("王五",5,100,2.5)// name,month,月销售额,提成率
            };
            for (int i = 0; i < all.length; i++) {
                new Company().getmoney(all[i], 5);
            }
        }
    
    }

    3、利用接口实现动态的创建对象:(知识点:接口 )

    (1)创建4个类

    1苹果

    2香蕉

    3葡萄

    4园丁

    (2)在三种水果的构造方法中打印一句话.

    以苹果类为例

    class apple

    {

    public apple()

    {

    System.out.println(“创建了一个苹果类的对象”);

    }

    package d20;
    
    public interface Fruit {
    
    }
    package d20;
    
    public class apple implements Fruit {
        public apple() {
            System.out.println("创建了一个苹果类对象");
        }
    
    }
    package d20;
    
    public class banana implements Fruit {
        public banana() {
            System.out.println("创建了一个香蕉类对象");
        }
    }
    package d20;
    
    public class grape implements Fruit {
        public grape() {
            System.out.println("创建了一个葡萄类对象");
        }
    
    }
    package d20;
    
    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();
        }
    }

     

  • 相关阅读:
    智能指针之 auto_ptr
    UML在线绘图
    inline使用
    工作随笔—2017-12-12
    链表排序
    转——浅谈如何提高服务器并发处理能力
    使用re开发python计算器
    Linux-centos7下python3 环境设置
    C语言中的static 详细分析
    pycharm import pygame 出现报错:No module named 'pygame'
  • 原文地址:https://www.cnblogs.com/ld1017/p/12929738.html
Copyright © 2020-2023  润新知