java继承接口刷题-1

1公司薪水 月薪日薪类 备注 :数组用数组的方法 fori 链表用iter方法 否则会因为空值报错

package homework;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

class Employee {
    protected String name;

    public Employee() {
    }

    public Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //计算该员工的薪资
    public double earnings() {
        return 0;
    }

    @Override
    public String toString() {
        return "员工的姓名是" + name + '\n';

    }
}

//月工作者类
class MonthWeeker extends Employee {
    private double monthSal;

    public MonthWeeker() {
    }

    public MonthWeeker(String name, double monthSal) {
        super(name);
        this.monthSal = monthSal;
    }

    public double getMonthSal() {
        return monthSal;
    }

    public void setMonthSal(double monthSal) {
        this.monthSal = monthSal;
    }

    @Override
    public double earnings() {
        return monthSal * 12;
    }

    @Override
    public String toString() {
        return super.toString() +
                "月薪是" + monthSal + '\n' +
                "年薪是" + earnings();

    }
}

//周工作者类
class WeekerWorker extends Employee {
    private double weekSal;

    public WeekerWorker() {
    }

    public WeekerWorker(String name, double weekSal) {
        super(name);
        this.weekSal = weekSal;
    }

    public double getMonthSal() {
        return weekSal;
    }

    public void setMonthSal(double monthSal) {
        this.weekSal = monthSal;
    }

    @Override
    public double earnings() {
        return weekSal * 52;
    }

    @Override
    public String toString() {
        return super.toString() +
                "周薪是" + weekSal + '\n' +
                "年薪是" + earnings();

    }
}

class Company {
    //问题2 用list写为什么不对
    private Employee[] employees = new Employee[3];
    //    private ArrayList<Employee> employees = new ArrayList<>();
//    private List<Employee> employees =new ArrayList<>();
    int numberOfWorks = 0;

    public Company() {
    }


    public double compueTotalSal() {
//            提问 这个为什么不能这样用
        double sum = 0;
//            if (employees == null) {
//                return 0;
//            } else {
        //里面不能存在空值
        for (Employee employee : employees) {
            sum = sum + employee.earnings();
        }
        return sum;
//            }
//            double sum = 0;
//            for (int i = 0; i < numberOfWorks; i++) {
//                sum += employees[i].earnings();
//            }
//            return sum;
    }

    public void addEmployee(Employee ee) {
        employees[numberOfWorks++] = ee;
    }

    @Override
    public String toString() {

        for (int i = 0; i < numberOfWorks; i++) {
            System.out.println(employees[i].toString());
        }
        return "公司每年需要支付的薪水" + compueTotalSal();
    }

}


//    public double computeTotalSalary() {
//        double sum = 0;
//        for (Employee employee : employees) {
//            sum = sum += employee.earnings();
//        }
//        return sum;
//    }
//
//    public void addEmployee(Employee ee) {
//        employees.add(ee);
//    }
//
//    @Override
//    public String toString() {
//        for (Employee employee : employees) {
//            System.out.println(employee.toString());
//        }
//        return "公司每年需要支付的薪水" + computeTotalSalary();
//    }
//}

//测试类
public class CompanySalary {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Company company = new Company();
//        Employee employee1 = new Employee();
        MonthWeeker monthWeeker=new MonthWeeker(scanner.next(), scanner.nextDouble());
        Employee employee2 = new WeekerWorker(scanner.next(), scanner.nextDouble());
        company.addEmployee(monthWeeker);
        company.addEmployee(employee2);
        System.out.println(company);
    }
}

2该题目用的是抽象类 abstract 抽象类里面可以写抽象函数 也可以用不是抽象的函数 

抽象的函数不能写实际的函数实现方法

3静态函数可以在类外直接调用 用类名加函数名  父类可以强制类型转换成子类

Circle circle = (Circle) GeometricObject.max(circle1, circle2)

4 能调试出来 但是不能解决问题

6计算矩形的周长跟面积

接口其实就是支付这个过程 支付实现的方法有微信支付 阿里支付 其他支付 这些类来实现 这就是多态 接口当中写了大家要实现的内容--支付 本题是求周长求面积

package homework2;

import java.util.Scanner;

interface GeometricObject {
//    void explaination();

    double perimeter();

    double area();
}

class Rectangle implements GeometricObject {
    protected double width;
    protected double length;

    public Rectangle() {
    }

    public Rectangle(double width, double length) {
        this.width = width;
        this.length = length;
    }

    @Override
    public double perimeter() {
        return 2 * (width + length);
    }

    @Override
    public double area() {
        return width * length;
    }
    //    @Override
//    public void explaination() {
//        System.out.println("这是矩形的信息");
//        System.out.println("矩形的宽度和长度是" + width + length);
//        System.out.println("矩形周长是" + 2 * (width + length));
//        System.out.println("矩形面积" + (width * length));
//    }
}

class Circle implements GeometricObject {
    protected double radius;

    public Circle(double radius) {
        this.radius = radius;

    }

    public Circle() {
    }

    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

//    @Override
//    public void explaination() {
//        System.out.println("这是圆形的信息");
//        System.out.println("圆形的半径是" + radius);
//
//
//    }
}


//测试类
public class TestRectangle {

    public static void displayGeometricObject(GeometricObject object) {
        System.out.println(object.perimeter());
        System.out.println(object.area());
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        GeometricObject object1 = new Rectangle(scanner.nextDouble(), scanner.nextDouble());
        GeometricObject object2 = new Circle(scanner.nextDouble());
        displayGeometricObject(object1);
        displayGeometricObject(object2);
    }
}

晚上又写了五道左右 差不多刷完了 各个语言的代码都大同小异 加油呀 菜狗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值