上机作业5.21
package llm; 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 showAll(); } package llm; public class Rectangle extends Shape { double width; double height; public Rectangle() { } public Rectangle(double width, double height, String color) { super(); this.width = width; this.height = height; this.color = color; } public void getArea() { area = width * height; } public void getPer() { per = (width + height) * 2; } public void showAll() { System.out.println("矩形面积为:" + area + ",周长为:" + per+",颜色:"+color); } } package llm; public class Circle extends Shape { double radius; public Circle() { } public Circle(double radius, String color) { this.color = color; this.radius = radius; } public void getArea() { area = radius * radius * 3.14; } public void getPer() { per = 2 * radius * 3.14; } public void showAll() { System.out.println("圆的面积为:" + area + ",周长为:" + per + ",颜色:" + color); } } package llm; public class PolyDemo { public static void main(String[] args) { Circle circle = new Circle(4, "red"); Rectangle rectangle = new Rectangle(8, 9, "blue"); circle.getArea(); circle.getPer(); circle.showAll(); rectangle.getArea(); rectangle.getPer(); rectangle.showAll(); } }
package mmm; public class ColaEmployee { String name; int month; public ColaEmployee() { } public ColaEmployee(String name, int month) { super(); this.name = name; this.month = month; } public double getSalary(int month) { return 0; } } package mmm; public class SalariedEmployee extends ColaEmployee { double n; public SalariedEmployee(String name, int month, double n) { super(name, month); this.n = n; } public double getSalary(int month) { if (super.month == month) { return n + 100; } else { return n; } } } package mmm; public class HourlyEmployee extends ColaEmployee { private int a; private int b; public HourlyEmployee(String name1, int month, int a1, int b1) { super(); this.name=name1; this.b = b1; this.a = a1; } public double getSalary(int month) { if (super.month == month) { if (b > 160) { return a * 160 + a * (b - 160) * 1.5 + 100; } else { return a + b + 100; } } else { if (b > 160) { return a * 160 + a * (a - 160) * 1.5; } else { return a * b; } } } } package mmm; public class SalesEmployee extends ColaEmployee { private int yx; private double m; public SalesEmployee(String name, int month, int yx, double m) { super(name, month); this.yx = yx; this.m = m; } public double getSalary(int month) { if (super.month == month) { return yx * m + 100; } else { return yx * m; } } } package mmm; public class Company { public void getSalary(ColaEmployee c, int month) { System.out.println(c.name + " 在" + month + "月的月工资数额为 " + c.getSalary(month) + "元"); } } package mmm; public class TextCompany { public static void main(String[] args) { ColaEmployee[] m = { new SalariedEmployee("固定员工", 4, 100), new HourlyEmployee("小时工",8,300,100), new SalesEmployee("销售额和提成的", 4, 2000, 0.3) }; for (int i = 0; i < m.length; i++) { new Company().getSalary(m[i], 10); } } }
package pppp; public interface Fruit { } package pppp; public class Apple implements Fruit{ public Apple() { System.out.println("创建了一个苹果类的对象。"); } } package pppp; public class Banana implements Fruit{ public Banana() { System.out.println("创建了一个香蕉类的对象。"); } } package pppp; public class Grape implements Fruit { public Grape(){ System.out.println("创建了一个葡萄类的对象。"); } } package pppp; import java.util.Scanner; public class Gardener { public void creater() { System.out.println("请输入水果名字"); Scanner input = new Scanner(System.in); String name1= input.nextLine(); switch (name1) { case "苹果": new Apple(); break; case "香蕉": new Banana(); break; case "葡萄": new Grape(); break; } input.close(); } } package pppp; public class Text { public static void main(String[] args) { Gardener g = new Gardener(); g.creater(); } }