In der funktionalen Programmiersprache können wir Funktionen und Schlüsselparameter verwenden, um bedingte Verzweigungen zu entfernen. Das heißt, verwenden Sie Funktionen mit Bedingungsparameter anstelle von "if esle". Siehe Beispiel 3. Als computeSphereArea ({Radius: 25,55})
Beispiel 1: OOP // in OOP (verwenden Sie beispielsweise Java (Quellcode von: http: //developer.51cto.com/art/200907/136506.htm)):
public abstract class Shape {
// ...
public abstract void computeArea();
public abstract void computeVolume();
public abstract double getArea();
public abstract double getVolume();
}
public class Circle extends CircleShape2 {
// ...
double volume = 0.0; //
public void computeArea() { //
area = Math.PI * radius * radius;
}
public double getArea() {
return area;
}
public void computeVolume() {} //
public double getVolume() {
return volume;
}
}
public class Sphere extends Circle {
// ...
public void computeArea() { //
super.computeArea(); //
area = 4 * area;
}
public void computeVolume() { //
super.computeArea(); //
volume = 4.0 / 3 * radius * area;
}
}
public class CircleShapeApp {
public static void main(String[] args) {
Circle circle = new Circle(12.98);
Sphere sphere = new Sphere(25.55);
Shape shape = circle; //
//
shape.computeArea();
shape.computeVolume();
System.out.println("circle area: " + shape.getArea());
System.out.println("circle volume: " + shape.getVolume());
//
shape = sphere;
shape.computeArea();
shape.computeVolume();
System.out.println("Sphere area: " + shape.getArea());
System.out.println("Sphere volume: " + shape.getVolume());
}
}
Beispiel 2: funktional wie oop. // in der funktionalen Programmierung (verwenden Sie zum Beispiel Javascript):
function initShape(v) {
var shape = {};
v = v || {};
if (typeOf(v, 'object') === true) {
shape.volumne = v.volumne || 0.0;
shape.computeArea = v.computeArea || function() {};
shape.computeVolume = v.computeVolume || function() {};
shape.getArea = v.getArea || function() {};
shape.getVolume = v.getVolume || function() {};
}
return shape;
}
function initCircle(v) {
var circle = {};
v = v || {};
if (typeOf(v, 'object') === true) {
circle.volume = 0.0;
circle.radius = v.radius || 0.0;
circle.computeArea = v.computeArea || function() {
this.area = Math.PI * this.radius * this.radius;
};
circle.computeVolume = function() {};
circle.getArea = v.getArea || function() {
return this.area
};
circle.getVolume = v.getVolume || function() {
return this.volume
};
}
return initShape(circle);
}
function initSphere(v) {
var sphere = {}
v = v || {};
if (typeOf(v, 'object') === true) {
var circle = initCircle(v);
sphere = circle;
sphere.volume = v.volume;
sphere.computeArea = function() {
circle.computeArea();
this.area = 4 * circle.area;
}
sphere.computeVolume = function() {
circle.computeArea();
this.volume = 4.0 / 3 * this.radius * circle.area;
}
}
return initShape(sphere);
}
var circle = initCircle(12.98);
circle.computeArea();
circle.computeVolume();
console.log("circle area: " + circle.getArea());
console.log("circle volume: " + circle.getVolume());
var sphere = initShpere(25.55);
sphere.computeArea();
sphere.computeVolume();
console.log("sphere area: " + sphere.getArea());
console.log("sphere volume: " + sphere.getVolume());
// Dies ist zwar kein reines Funktionsprogrammbeispiel, sondern eine Funktionsschnittstelle wie initCircle () initSphere (). Sie können weitere Funktionen erstellen, wie computeCircleArea () computeSphereArea () es funktionaler macht. // PS: typeOf () ist hier: https://github.com/will-v-king/javascript-showMe
Beispiel 3: Ok, machen wir es funktionsfähiger:
/** in functional code shape became meaningless.
function initShape(v) {
var shape = {};
v = v || {};
if (typeOf(v, 'object') === true) {
shape = v.object || v.shape || shape;
shape.volumne = v.volumne || 0.0;
}
return shape;
}
function computeShapeArea(v){
}
function computeShapeVolume(v){
}
*/
function initCircle(v) {
var circle = {};
v = v || {};
if (typeOf(v, 'object') === true) {
circle = v.object || v.circle || circle;
circle.volume = 0.0;
circle.radius = v.radius || 0.0;
}
return initShape(circle);
}
function computeCircleArea(v){
var area;
v = v || {};
if(typeOf(v) === 'Object'){
var radius = v.radius || v.object.radius || v.circle.radius;
if(!typeOf(v,'undefined')){
area = Math.PI * radius * radius;
}
}
return area;
}
function computeCircleVolume(v){
return 0.0;
}
/**function initCircle and initSphere are not necessary. why? see the last line.*/
function initSphere(v) {
var sphere = {}
v = v || {};
if (typeOf(v, 'object') === true) {
var circle = initCircle(v);
sphere = circle;
sphere.volume = v.volume;
}
return initShape(sphere);
}
function computeSphereArea(v){
var area;
v = v || {};
if(typeOf(v) === 'Object'){
var radius = v.radius || v.object.radius || v.sphere.radius;
if(!typeOf(v,'undefined')){
area = 4 * computeCircleArea({radius:radius}); // **POINT** the same as :circle.computeArea(); this.area = 4 * circle.area;
}
}
return area;
}
function computeSphereVolume(v){
var volume;
v = v || {};
if(typeOf(v,'object') === ture){
radius = v.radius || typeOf(v.object, 'object') === true ? v.object.radius : typeOf(v.sphere, 'Object') === true ? v.sphere.radius : 0.0;
var circleArea = computeCircleArea({radius:radius});
if(typeOf(circleArea,'number')=== true){
volume = 4.0 / 3 * radius * computeCircleArea({radius:radius}); // **POINT** the same as: circle.computeArea(); this.volume = 4.0 / 3 * this.radius * circle.area;
}
}
return volume;
}
var circle = initCircle({radius:12.98});
console.log("circle area: " + computeCircleArea(circle) );
console.log("circle volume: " + computeCircleVolume(circle) );
var sphere = initShpere(25.55);
console.log("sphere area: " + computeSphereArea({radius:25.55}) );
console.log("sphere volume: " + computeSphereVolume({radius:25.55}) );
console.log("sphere object is unused.That means initSphere is also not necessary as initShape()");