[Raspberry Pi] Objekte und Klassen

class Car:
# Klassenvariablen (statisch)     
    name = "Mercedes"
    color = "blau"
 
    def __init__(self, cartype):
# Instanzvariable (self)
        self.cartype = cartype

    def drive(self, sound):
        print("Typ:", self.cartype)
        print("Geräusch:", sound)

# Werte der Klassenvariablen ausgeben
print(Car.name)
print(Car.color)

# Object instanziieren
auto = Car("Kombi")
auto.drive("brumm")

Links

[JavaScript] Assoziatives Array

Array

// Definition
let language = new Array();
language["DE"] = "Guten Morgen";
language["EN"] = "Good morning";
language["IT"] = "Buongiorno";
language["FR"] = "Bonjour";

// Guten Morgen User!
console.log(language["DE"] + " " + "User!");

// ["DE","EN","IT","FR"]
var aKeys = Object.keys(language);
console.log(JSON.stringify(aKeys));

// 12
console.log(language["DE"].length);
// 0
console.log(language.length);

Object

// Definition
let language = new Object();
language["DE"] = "Guten Morgen";
language["EN"] = "Good morning";
language["IT"] = "Buongiorno";
language["FR"] = "Bonjour";

// Guten Morgen User!
console.log(language["DE"] + " " + "User!");

// ["DE","EN","IT","FR"]
var aKeys = Object.keys(language);
console.log(JSON.stringify(aKeys));

// 12
console.log(language["DE"].length);
// undefined
console.log(language.length);

[JavaScript] Einfache Objektdefinition

einfache Objektdefinition mit Literalen

// https://www.w3schools.com/js/js_objects.asp
// https://www.phpied.com/3-ways-to-define-a-javascript-class/

let person = {
  Vorname: "Horst",
  Nachname: "Mustermann",
  Alter: 65,
  getText: function(type){
    let text = "";
     
    switch (type) {
      case "1":
        text = this.Vorname;
        break;
      case "2":
        text = this.Nachname;
        break;
      case "3":
        text = this.Alter;
        break;
      default:
        text = "-";
        break;
    }
     
    return text;
  }
};

// Horst -> Klaus
person["Vorname"] = "Klaus";

// Datenausgabe
console.log(person.Vorname);
console.log(person["Vorname"]);
console.log(person.getText("1"));
console.log(person.getText("2"));
console.log(person.getText("3"));

einfache Objektdefinition mit new Object

let person = new Object();

person.Vorname = "Horst";
person.Nachname = "Mustermann";
person.Alter = 65;
person.getText = function(type){
  let text = "";
   
  switch (type) {
    case "1":
      text = this.Vorname;
      break;
    case "2":
      text = this.Nachname;
      break;
    case "3":
      text = this.Alter;
      break;
    default:
      text = "-";
      break;
  }
   
  return text;
};
 
// Datenausgabe
console.log(person.Vorname);
console.log(person["Vorname"]);
console.log(person.getText("1"));
console.log(person.getText("2"));
console.log(person.getText("3"));

alternative Objektdefinition mit new Object + Literalen

let person = new Object({
  Vorname : "Horst",
  Nachname : "Mustermann",
  Alter : 65,
  getText : function(type){
      let text = "";
       
      switch (type) {
        case "1":
          text = this.Vorname;
          break;
        case "2":
          text = this.Nachname;
          break;
        case "3":
          text = this.Alter;
          break;
        default:
          text = "-";
          break;
      }
       
      return text;
  }
});

// Datenausgabe
console.log(person.Vorname);
console.log(person["Vorname"]);
console.log(person.getText("1"));
console.log(person.getText("2"));
console.log(person.getText("3"));