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方法。
1 package text; 2 3 public abstract class Shape1 { 4 protected double area; // 面积 5 protected double per; // 周长 6 protected String color; // 颜色 7 8 public Shape1() { 9 } 10 11 public Shape1(String color) { 12 this.color = color; 13 } 14 15 public abstract void getArea(); 16 17 public abstract void getPer(); 18 19 public abstract void showAll(); 20 21 } 22 23 package text; 24 25 public class Rectangle extends Shape1 { 26 double height; 27 double width; 28 29 public Rectangle() { 30 31 } 32 33 public Rectangle(double width, double height, String color) { 34 super(); 35 this.width = width; 36 this.height = height; 37 this.color = color; 38 } 39 40 @Override 41 public void getArea() { 42 // TODO Auto-generated method stub 43 area = height * width; 44 } 45 46 @Override 47 public void getPer() { 48 // TODO Auto-generated method stub 49 per = (height + width) * 2; 50 } 51 52 @Override 53 public void showAll() { 54 // TODO Auto-generated method stub 55 System.out.println("矩形面积为:" + area + " , 周长为:" + per + " , 颜色为:" + color); 56 } 57 } 58 59 60 package text; 61 62 public class Circle extends Shape1 { 63 double radius; 64 65 public Circle() { 66 67 } 68 69 public Circle(double radius, String color) { 70 this.color = color; 71 this.radius = radius; 72 } 73 74 @Override 75 public void getArea() { 76 // TODO 自动生成的方法存根 77 area = radius * radius * 3.14; 78 } 79 80 @Override 81 public void getPer() { 82 // TODO 自动生成的方法存根 83 per = 2 * radius * 3.14; 84 } 85 86 @Override 87 public void showAll() { 88 // TODO 自动生成的方法存根 89 System.out.println("圆的面积为:" + area + " , 周长为:" + per + " , 颜色为:" + color); 90 } 91 92 } 93 94 95 package text; 96 97 public class Test3 { 98 99 public static void main(String[] args) { 100 // TODO Auto-generated method stub 101 Circle circle = new Circle(5, "white"); 102 Rectangle rectangle = new Rectangle(2, 7, "yellow"); 103 circle.getArea(); 104 circle.getPer(); 105 circle.showAll(); 106 107 rectangle.getArea(); 108 rectangle.getPer(); 109 rectangle.showAll(); 110 111 } 112 113 }
2、Cola公司的雇员分为以下若干类:(知识点:多态)
(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
Ÿ 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
(2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。
Ÿ 属性:月薪
(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
Ÿ 属性:每小时的工资、每月工作的小时数
(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。
Ÿ 属性:月销售额、提成率
(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。
1 package text; 2 //4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工 3 //的生日月份。方法:getSalary(int month) 根据参数月份来确定工资, 4 //如果该月员工过生日,则公司会额外奖励100 元。 5 public class ColaEmployee { 6 String name; 7 int month; 8 9 public ColaEmployee() { 10 } 11 12 public ColaEmployee(String name, int month) { 13 this.name = name; 14 this.month = month; 15 16 } 17 18 public double getSalary(int month) { 19 return 0; 20 } 21 } 22 23 24 package text; 25 //4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪 26 public class SalariedEmployee extends ColaEmployee { 27 double monSalary;//月薪 28 29 public SalariedEmployee(String name, int month, double monSalary) { 30 super(name, month); 31 this.monSalary = monSalary; 32 } 33 34 // 方法:getSalary(int month) 根据参数月份来确定工资, 35 // 如果该月员工过生日,则公司会额外奖励100 元。 36 public double getSalary(int month) { 37 if (super.month == month) { 38 return monSalary + 100; 39 } else { 40 return monSalary; 41 } 42 } 43 } 44 45 package text; 46 //4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工, 47 //每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工 48 //资、每月工作的小时数 49 50 public class HourlyEmployee extends ColaEmployee { 51 private int hourSalary;//时薪 52 private int hourNum;//每月工作的小时数 53 54 public HourlyEmployee(String name, int month, int hourSalary, int hourNum) { 55 super(name, month); 56 this.hourSalary = hourSalary; 57 this.hourNum = hourNum; 58 } 59 60 /* 61 * 情况: 1.每月工作超出160小时; 62 2.每月工作超出160 小时 + (该月过生日)100; 63 * 64 * 3.每月工作没超出160小时; 65 4.每月工作没超出160小时+ (该月过生日)100; 66 */ 67 public double getSalary(int month) { 68 if (super.month == month) { 69 if (hourNum > 160) { 70 return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5 + 100; 71 } else { 72 return hourSalary * hourNum + 100; 73 } 74 } else { 75 if (hourNum > 160) { 76 return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5; 77 } else { 78 return hourSalary * hourNum; 79 } 80 } 81 } 82 } 83 84 85 package text; 86 // 4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销 87 // 售额和提成率决定。属性:月销售额、提成率 88 public class SalesEmployee extends ColaEmployee { 89 private int monthSales;//月销售额 90 private double royaltyRate;//提成率 91 92 public SalesEmployee(String name, int month, int monthSales, double royaltyRate) { 93 super(name, month); 94 this.monthSales = monthSales; 95 this.royaltyRate = royaltyRate; 96 } 97 // 工资=月销售额*提成率 98 public double getSalary(int month) { 99 if (super.month == month) { 100 return monthSales * royaltyRate + 100; 101 } else { 102 return monthSales * royaltyRate; 103 } 104 } 105 } 106 107 package text; 108 //打印出某月某个员工的工资数额 109 public class Company { 110 public void getSalary(ColaEmployee c, int month) { 111 System.out.println(c.name + "在" + month + "月的月薪为:" + c.getSalary(month) + "元"); 112 } 113 } 114 115 116 package text; 117 118 public class TestCompany { 119 120 public static void main(String[] args) { 121 // TODO Auto-generated method stub 122 //创建、初始化数组 123 ColaEmployee[] cel = { new SalariedEmployee("李姬亦", 3, 20000), 124 new HourlyEmployee("赵刚", 4, 200, 500), 125 new SalesEmployee("薛岳", 9, 450000, 0.2) }; 126 for (int i = 0; i < cel.length; i++) { 127 new Company().getSalary(cel[i], 3); 128 } 129 } 130 131 }
3、利用接口实现动态的创建对象:(知识点:接口 )
(1)创建4个类
1苹果
2香蕉
3葡萄
4园丁
(2)在三种水果的构造方法中打印一句话.
以苹果类为例
class apple
{
public apple()
{
System.out.println(“创建了一个苹果类的对象”);
}
}
(3)类图如下:
(4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象。
运行结果如图:
1 package text; 2 3 import java.util.Scanner; 4 5 interface Fruit { 6 7 } 8 9 package text; 10 11 class Apple implements Fruit{ 12 public Apple(){ 13 System.out.println("创建一个苹果对象"); 14 } 15 } 16 17 18 package text; 19 20 class Pear implements Fruit{ 21 public Pear(){ 22 System.out.println("创建一个梨对象"); 23 } 24 25 } 26 27 28 package text; 29 30 class Orange implements Fruit{ 31 public Orange(){ 32 System.out.println("创建一个句子对象"); 33 } 34 } 35 36 package text; 37 38 import java.util.Scanner; 39 //接口作为方法返回值的意义:返回实现了该接口的对象 40 class Gardener { 41 @SuppressWarnings("resource") 42 public void create(){ 43 Scanner input=new Scanner(System.in); 44 String name=input.next(); 45 46 Fruit fruit=null; 47 switch(name){ 48 case"苹果": 49 switch(name){ 50 case "苹果": 51 fruit = new Apple(); 52 break; 53 case "梨": 54 fruit = new Pear(); 55 break; 56 case "桔子": 57 fruit = new Orange(); 58 break; 59 } 60 61 input.close(); 62 63 return; 64 } 65 } 66 } 67 68 package text; 69 70 public class Test4 { 71 72 public static void main(String[] args) { 73 // TODO Auto-generated method stub 74 Gardener g = new Gardener(); 75 g.create(); 76 } 77 78 }