double dispatcher


public class Feeder {
public void feed(Animal animal) {
System.out.println("feed Animal.");
}
public void feed(Dog dog) {
System.out.println("feed Dog: " + dog.eat());
}
public void feed(Cat cat) {
System.out.println("feed Cat: " + cat.eat());
}
}


public interface Animal {
String eat();
}

public class Dog implements Animal {

@Override
public String eat() {
return "骨头";
}

}


public class Main {

public static void main(String[] args) {
Animal a = new Dog();
Feeder feeder = new Feeder();
feeder.feed(a);//print: feed Animal.
}

}


public interface Animal {
String eat();
void bindFeeder(Feeder feeder);
}
public class Dog implements Animal {

@Override
public String eat() {
return "骨头";
}

@Override
public void bindFeeder(Feeder feeder) {
feeder.feed(this);
}

}
public class Main {

public static void main(String[] args) {
Animal a = new Dog();
Feeder feeder = new Feeder();
a.bindFeeder(feeder);//print:feed Dog: 骨头
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是完整实现复数加减乘除四则运算的代码: 1. JavaBean ``` public class Complex { private double real; private double imag; public Complex() { this.real = 0; this.imag = 0; } public Complex(double real, double imag) { this.real = real; this.imag = imag; } public double getReal() { return real; } public void setReal(double real) { this.real = real; } public double getImag() { return imag; } public void setImag(double imag) { this.imag = imag; } public Complex add(Complex b) { Complex a = this; double real = a.real + b.real; double imag = a.imag + b.imag; return new Complex(real, imag); } public Complex subtract(Complex b) { Complex a = this; double real = a.real - b.real; double imag = a.imag - b.imag; return new Complex(real, imag); } public Complex multiply(Complex b) { Complex a = this; double real = a.real * b.real - a.imag * b.imag; double imag = a.real * b.imag + a.imag * b.real; return new Complex(real, imag); } public Complex divide(Complex b) { Complex a = this; double real = (a.real * b.real + a.imag * b.imag) / (b.real * b.real + b.imag * b.imag); double imag = (a.imag * b.real - a.real * b.imag) / (b.real * b.real + b.imag * b.imag); return new Complex(real, imag); } } ``` 2. Servlet ``` @WebServlet("/ComplexCalculator") public class ComplexCalculator extends HttpServlet { private static final long serialVersionUID = 1L; public ComplexCalculator() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { double real1 = Double.parseDouble(request.getParameter("real1")); double imag1 = Double.parseDouble(request.getParameter("imag1")); double real2 = Double.parseDouble(request.getParameter("real2")); double imag2 = Double.parseDouble(request.getParameter("imag2")); Complex c1 = new Complex(real1, imag1); Complex c2 = new Complex(real2, imag2); Complex result = null; String operator = request.getParameter("operator"); switch (operator) { case "+": result = c1.add(c2); break; case "-": result = c1.subtract(c2); break; case "*": result = c1.multiply(c2); break; case "/": result = c1.divide(c2); break; default: break; } request.setAttribute("result", result); RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp"); dispatcher.forward(request, response); } } ``` 3. JSP ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Complex Calculator</title> </head> <body> <h1>Complex Calculator</h1> <form method="post" action="ComplexCalculator"> <input type="number" name="real1" step="0.01" required> + <input type="number" name="imag1" step="0.01" required> i <select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="number" name="real2" step="0.01" required> + <input type="number" name="imag2" step="0.01" required> i <input type="submit" value="Calculate"> </form> <% Complex result = (Complex) request.getAttribute("result"); if (result != null) { out.println("<p>Result: " + result.getReal() + " + " + result.getImag() + "i</p>"); } %> </body> </html> ``` 以上是完整的实现代码,可以实现复数的加减乘除四则运算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值