第十二周练习

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 methodc;
 2 
 3 public abstract class Shape {
 4     int area;
 5     int per;
 6     String color;
 7 
 8     public Shape() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12 
13     public Shape(int area, int per, String color) {
14         super();
15         this.area = area;
16         this.per = per;
17         this.color = color;
18     }
19 
20     public int getArea() {
21         return area;
22     }
23 
24     public void setArea(int area) {
25         this.area = area;
26     }
27 
28     public int getPer() {
29         return per;
30     }
31 
32     public void setPer(int per) {
33         this.per = per;
34     }
35 
36     public String getColor() {
37         return color;
38     }
39 
40     public void setColor(String color) {
41         this.color = color;
42     }
43 
44     public abstract void getAreaa();
45 
46     public abstract void getPera();
47 
48     public abstract void showAll();
49 
50 }
 1 package methodc;
 2 
 3 public class Rectangle extends Shape {
 4     int Width=5;
 5     int height=3;
 6     
 7     
 8     
 9     
10     @Override
11     public void getAreaa() {
12         System.out.println("area=" + Width*height );
13 
14     }
15 
16     public void getPera() {
17         System.out.println("per=" + (Width+height)*2 );
18 
19     }
20 
21     public void showAll() {
22         getAreaa();
23         getPera();
24         System.out.println("Width="+ Width + ",height=" + height);
25         
26 
27     }
28 }
 1 package methodc;
 2 
 3 public class Circle extends Shape{
 4     int Circle=2;
 5     
 6     
 7 
 8     @Override
 9     public void getAreaa() {
10         System.out.println("area="+ 3.14*Circle*Circle  );
11 
12     }
13 
14     public void getPera() {
15         System.out.println("per=" + 4*3.14*Circle);
16 
17     }
18 
19     public void showAll() {
20         getAreaa();
21         getPera();
22         System.out.println("Circle=" + Circle);
23         
24 
25     }
26 
27 }
 1 package methodc;
 2 
 3 public class PolyDemo {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Rectangle r = new Rectangle();
 8         r.getAreaa();
 9         r.getPera();
10         r.showAll();
11         
12         Circle c = new Circle();
13         c.getAreaa();
14         c.getPera();
15         c.showAll();
16         
17     
18 
19     }
20 
21 }

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 Cola;
 2 
 3 public class ColaEmployee {
 4     String name;
 5     int month;
 6     
 7     public ColaEmployee() {
 8         super();
 9         
10     }
11     
12 
13     public ColaEmployee(String name, int month) {
14         super();
15         this.name = name;
16         this.month = month;
17     }
18 
19 
20     public double getSalary(int month){    
21         return month;
22     }
23 
24 }
 1 package Cola;
 2 
 3 public class SalesEmployee extends ColaEmployee {
 4     double monthsalary;
 5     double monthcommission ;
 6     public SalesEmployee() {
 7         super();
 8         // TODO Auto-generated constructor stub
 9     }
10     public SalesEmployee(String name,int month,double monthsalary, double monthcommission) {
11         super(name,month);
12         this.monthsalary = monthsalary;
13         this.monthcommission = monthcommission;
14     }
15     @Override
16     public double getSalary(int month) {
17         if (super.month == month) {
18             return monthsalary*monthcommission + 100;
19         } else {
20             return monthsalary*monthcommission;
21         }
22 
23     }
24 
25     
26     
27 
28 }
 1 package Cola;
 2 
 3 public class HourlyEmployee extends ColaEmployee {
 4     int hourSalary;
 5     int hour;
 6 
 7     public HourlyEmployee() {
 8         super();
 9     }
10 
11     public HourlyEmployee(String name, int month, int hourSalary, int hour) {
12         super(name, month);
13         this.hourSalary = hourSalary;
14         this.hour = hour;
15     }
16     @Override
17 
18     public double getSalary(int month) {
19         if(super.month == month){
20             if (hour > 160) {
21                 return hourSalary * 160+ (hour-160)*1.5*hourSalary + 100;
22             } else {
23                 return hourSalary * hour + 100;
24             }
25         }else{
26             if (hour > 160) {
27                 return hourSalary * 160 +(hour - 160)*1.5*hourSalary  ;
28             } else {
29                 return hourSalary * hour;
30             }
31             
32         }
33         
34 
35 
36      
37     }
38    
39 
40 }
 1 package Cola;
 2 
 3 public class SalariedEmployee extends ColaEmployee {
 4     double monthsalary ;
 5 
 6     public SalariedEmployee() {
 7         super();
 8         
 9     }
10 
11     public SalariedEmployee(String name, int mouth ,double monthsalary) {
12         super(name, mouth);
13         this.monthsalary = monthsalary;
14         
15     }
16     @Override
17     public double getSalary(int month) {
18          
19             if (super.month == month) {
20                 return monthsalary + 100;
21             } else {
22                 return monthsalary;
23             }
24 
25         }
26     
27 
28 }
1 package Cola;
2 
3 public class Company {
4     public void getSalary(ColaEmployee c, int month) {
5         System.out.println(c.name + "在" + month + "月的月薪是:" + c.getSalary(month) + "元");
6     }
7 
8 }
 1 package Cola;
 2 
 3 public class TestCompany {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         ColaEmployee[] cel = {
 8                 new SalariedEmployee("拿固定工资的员工", 6, 8500),
 9                 new HourlyEmployee("按小时拿工资的员工", 5, 100,85),
10                 new SalesEmployee("销售人员", 3, 70000, 0.1)
11                 };
12         for (int i = 0; i < cel.length; i++) {
13             new Company().getSalary(cel[i],5);
14         }
15         
16         
17     }
18 
19 }

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

(1)创建4个类

1苹果 2香蕉 3葡萄 4园丁

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

以苹果类为例

class apple

{

       public apple()

       {

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

}

}

(3)类图如下:

(4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象     

运行结果如图:

1 package Interfacea;
2 
3 public interface Fruit {
4     
5     public abstract void create();
6     
7     
8 
9 }
 1 package Interfacea;
 2 
 3 public class Apple implements Fruit{
 4     
 5 
 6 
 7     @Override
 8     public void create() {
 9         // TODO Auto-generated method stub
10         
11         System.out.println("创建了一个苹果类的对象");
12         
13     }
14     
15     
16         
17 
18 
19 }
 1 package Interfacea;
 2 
 3 public  class Banana implements Fruit {
 4 
 5     @Override
 6     public void create() {
 7         // TODO Auto-generated method stub        
 8         
 9         
10         System.out.println("创建了一个香蕉类的对象");
11         
12     }
13 
14 }
 1 package Interfacea;
 2 
 3 public class Grape implements Fruit {
 4     
 5     @Override
 6     public void create() {
 7         // TODO Auto-generated method stub        
 8         
 9         
10         System.out.println("创建了一个葡萄类的对象");
11         
12     }
13 
14 }
 1 package Interfacea;
 2 
 3 public class Gardener {
 4     
 5         public void creat(Fruit fruit) {
 6             
 7             fruit.create();
 8             
 9         }
10         
11 
12 }
 1 package Interfacea;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Aaaa {
 6 
 7     public static void main(String[] args) {
 8         Gardener gardener = new Gardener();
 9         System.out.println("输入一个水果");
10         Scanner input = new Scanner(System.in);
11         String fruit = input.nextLine();
12 
13         switch (fruit) {
14         case "苹果":
15             Apple apple = new Apple();
16             gardener.creat(apple);
17             break;
18         case "香蕉":
19             Banana Banana = new Banana();
20             gardener.creat(Banana);
21             break;
22         case "葡萄":
23             Grape Grape = new Grape();
24             gardener.creat(Grape);
25             break;
26         default:
27             System.out.println("没有该水果");
28 
29         }
30     }
31 }

猜你喜欢

转载自www.cnblogs.com/wuhaoovo/p/12929708.html