Objektorientierung in JavaScript und Java

Ich habe eine Klasse „Person“ in JavaScript und Java geschrieben, um zu zeigen, wie Namensräume (engl. namespaces) und Objektorientierung in beiden Programmiersprachen aussehen.

Java

de.bennyn.example.Person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Namespace
package de.bennyn.example;
 
public class Person {
 
  private String firstName = null;
  private String lastName = null;
  private int age;
 
  // Default-Konstruktor
  public Person() {
    this.firstName = "Max";
    this.lastName = "Mustermann";
    this.age = 0;
  }
 
  // Init-Konstruktor
  public Person(String firstName, String lastName) {
    this();
    this.firstName = firstName;
    this.lastName = lastName;
  }
 
  // Getter
  public String getFirstName() {
    return this.firstName;
  }
 
  public String getLastName() {
    return this.lastName;
  }
 
  public int getAge() {
    return this.age;
  }
 
  // Setter
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
 
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
 
  public void setAge(int age) {
    this.age = age;
  }
 
  // toString
  @Override
  public String toString() {
    return "Person{"
            + "firstName=" + this.firstName + ", "
            + "lastName=" + this.lastName + ", "
            + "age=" + this.age
            + '}';
  }
}

Verwendung in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
package javaapplication8;
 
import de.bennyn.example.Person;
 
public class JavaApplication8 {
 
  public static void main(String[] args) {
    Person benny = new Person("Benny", "Neugebauer");
    benny.setAge(24);
    System.out.println(benny);
    // Output: Person{firstName=Neugebauer, lastName=Mustermann, age=24}
  }
}

JavaScript

MyNameSpace.Person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Namespace
var MyNameSpace = MyNameSpace || {};
 
// Default-Konstruktor
MyNameSpace.Person = function(){
  this.firstName = "Max";
  this.lastName = "Mustermann";
  this.age = 0;
};
 
// Init-Konstruktor (JavaScript kennt keine überladenen Funktionen)
MyNameSpace.Person.prototype.initPerson = function(firstName, lastName){
  this.firstName = firstName;
  this.lastName = lastName;
};
 
// Getter
MyNameSpace.Person.prototype.getFirstName = function(){
  return this.firstName;
};
 
MyNameSpace.Person.prototype.getLastName = function(){
  return this.lastName;
};
 
MyNameSpace.Person.prototype.getAge = function(){
  return this.age;
};
 
// Setter
MyNameSpace.Person.prototype.setFirstName = function(firstName){
  this.firstName = firstName;
};
 
MyNameSpace.Person.prototype.setLastName = function(lastName){
  this.lastName = lastName;
};
 
MyNameSpace.Person.prototype.setAge = function(age){
  this.age = age;
};
 
// toString
MyNameSpace.Person.prototype.toString = function(){
  return "Person{"
  + "firstName=" + this.firstName + ", "
  + "lastName=" + this.lastName + ", "
  + "age=" + this.age
  + '}';
};

Verwendung in JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Test</title>
  </head>
  <body>  
    <script src="js/de/bennyn/example/Person.js"></script>
    <script>
      var benny = new MyNameSpace.Person();
      benny.initPerson("Benny", "Neugebauer");
      benny.setAge(24);
      console.log(benny.toString());
      // Output: Person{firstName=Benny, lastName=Neugebauer, age=24}
    </script>
  </body>
</html>

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.