In-depth Atlas series: WebSevicesAccessinAtlas example (3)

Source: Internet
Author: User
Polymorphism is an important feature of OOP. Here, polymorphism is a small part. In the WebServices method, we usually use a specific type of parameter. This parameter is generally a data object. All functions are basically used to store data. Although this is generally enough for applications, we use such a large number of WebServi.

Polymorphism is an important feature of OOP. Here, polymorphism is a small part. In the Web Services method, we usually use a specific type of parameter. This parameter is generally a data object. All functions are basically used to store data. Although this is generally enough for applications, we use such Web Servi in large quantities.


 

Polymorphism is an important feature of OOP. Here, polymorphism is a small part.

In the Web Services method, we usually use a specific type of parameter. This parameter is generally a data object. All functions are basically used to store data. Although this is generally enough for applications, we have used such Web Services in large quantities. Isn't it even better? However, in this case, it is too object-oriented.

But how can we use polymorphism in the Web Services method? The easiest way to think about it is to use interface or abstract parameters in the Web Services method. You only need to pass different implementations or subclass objects as parameters to the Web Services method. As an example, we want to see the simplest things. Use this method.

First, we define an Employee abstract class:

Employee abstract class1 public abstract class Employee
2 {
3   private int _Years;
4   public int Years
5   {
6     get
7     {
8       return this._Years;
9     }
10     set
11     {
12       this._Years = value;
13     }
14   }
15
16   public string RealStatus
17   {
18     get
19     {
20       return this.GetType().Name;
21     }
22   }
23
24   public abstract int CalculateSalary();
25 }

The Employee abstract class stores the length of service of an Employee. The RealStatus attribute returns the real class name of the current instance and defines a CalculateSalary method to provide different implementations for the subclass.

We wrote a Web Services method CalculateSalary to calculate the salary of an employee and return the information. The Code is as follows:

CalculateSalary Method

1 [WebMethod]
2 public string CalculateSalary(Employee employee)
3 {
4   return "I'm " + employee.RealStatus + ", my salary is " + employee.CalculateSalary() + ".";
5 }

In this way, we only need to input instances of different Employee subclasses to obtain different information. Now let's start allocating salaries (the following figures are purely fictitious. If there are similarities, it's a coincidence )!

First, interns. Poor interns, no matter how many teenagers are, will always have 2000 yuan:

Intern code

1 public class Intern : Employee
2 {
3   public override int CalculateSalary()
4   {
5     return 2000;
6   }
7 }

Next we sign a contract from a third-party company with a base salary of 5000, increasing by 1000 every year:

Vendor code

1 public class Vendor : Employee
2 {
3   public override int CalculateSalary()
4   {
5     return 5000 + 1000 * (Years - 1);
6   }
7 }

Finally, it is a formal employee (all employees) with a base salary of 12000, increasing by 2000 every year:

FulltimeEmployee class code

1 public class FulltimeEmployee : Employee
2 {
3   public override int CalculateSalary()
4   {
5     return 12000 + 2000 * (Years - 1);
6   }
7 }

Then, how can we tell Atlas to pass parameters of different types of instances to Web Services methods? The answer is to use "_ serverType" to specify the type. We can use the sample code to view this point:

First, let's look at the simple HTML code:

HTML code

1
2  
3

Years:


4


5   Status:
6   7     Intern8     Vendor9     FTE10  
11


12
13 Result:
14

There is a text box in which year is entered. In the drop-down box, you can select the parameter type that you want to pass to the Web Services method. Click "Calculate !" The Web Services method is called and the result is displayed on the last DIV.

The following is the Javascript code used:

Javascript code

1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.