oop

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2022 License: MIT Imports: 1 Imported by: 0

README

oop

Run it in a browser

https://go.dev/play/p/p-2isqWzCHn

Documentation

The code is pretty simple, it creates a Regular Human called "Peter Parker" and "Clark Kent", then it creates two Superheros Spiderman and S uperman. Both RegularHuman and SuperHuman inherit Human. Documentation

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Human

type Human struct {
	First string
	Last  string
	Age   int
	// contains filtered or unexported fields
}

Human These are fields used for all humans

func (*Human) InitHuman

func (h *Human) InitHuman(first, last string, age int)

InitHuman This function is passed with the first, last, age of your human.

Example
package main

import (
	"github.com/rmasci/oop"
)

func main() {
	oop.Human{
		First: "Clark",
		Last:  "Kent",
		Age:   32,
	}
}

type JsonOut

type JsonOut interface {
	JsonPrint() (string, error)
}

JsonOut Polymorphism -- concept that you can access objects of different types through the same interface. This is an interface that implements the JsonPrint function for both Regular Humans and Super Humans

type RegularHuman

type RegularHuman struct {
	Human
	Job       string
	OldPerson string
}

RegularHuman This is for non-superhuman

func (*RegularHuman) InitRegularHuman

func (r *RegularHuman) InitRegularHuman(first, last, job string, age int)

InitRegularHuman Init a regular human

func (*RegularHuman) JsonPrint

func (r *RegularHuman) JsonPrint() (string, error)

JsonPrint Print out Regularhumans or Superhumans in JSON. This code is attached to an interface so that the same 'function' called JsonPrint can be used with a RegularHuman and SuperHuman type. This saves us having to write some kind of if / then statement tha figures out which type the variable belongs to and printing it out accordingly.

Example
package main

import (
	"fmt"

	"github.com/rmasci/oop"
)

func main() {
	var person1 oop.RegularHuman
	var person2 oop.SuperHuman
	person1.InitRegularHuman("Peter", "Parker", "Photographer", 23)
	person2.InitSuperHuman("Spider", "Man", "Spider dude", 23)
	out, _ := person1.JsonPrint()
	fmt.Println(out)
	out, _ = person2.JsonPrint()
	fmt.Println(out)
	/*
		{
		        "First": "Peter",
		        "Last": "Parker",
		        "Age": 23,
		        "Job": "Photographer",
		        "OldPerson": "Your human is young"
		}
		{
		        "First": "Spider",
		        "Last": "Man",
		        "Age": 23,
		        "Superpower": "Spider dude",
		        "OldSuperhero": "Super hero is young"
		}

	*/
}

type SuperHuman

type SuperHuman struct {
	Human
	Superpower   string
	OldSuperhero string
	// contains filtered or unexported fields
}

SuperHuman These are fields for Super Humans, it inherits the type and methods of Human

func (*SuperHuman) InitSuperHuman

func (s *SuperHuman) InitSuperHuman(first, last, superpower string, age int)

InitSuperHuman This initializes a superhuman pass in the first last and age

Example
package main

import (
	"fmt"

	"github.com/rmasci/oop"
)

func main() {
	var person1 oop.RegularHuman
	var person2 oop.SuperHuman
	person1.InitRegularHuman("Peter", "Parker", "Photographer", 23)
	person2.InitSuperHuman("Spider", "Man", "Spider dude", 23)
	out, _ := person1.JsonPrint()
	fmt.Println(out)
	out, _ = person2.JsonPrint()
	fmt.Println(out)
	/*
		{
		        "First": "Peter",
		        "Last": "Parker",
		        "Age": 23,
		        "Job": "Photographer",
		        "OldPerson": "Your human is young"
		}
		{
		        "First": "Spider",
		        "Last": "Man",
		        "Age": 23,
		        "Superpower": "Spider dude",
		        "OldSuperhero": "Super hero is young"
		}

	*/
}

func (*SuperHuman) JsonPrint

func (s *SuperHuman) JsonPrint() (string, error)

JsonPrint Print out Regularhumans or Superhumans in JSON. This code is attached to an interface so that the same 'function' called JsonPrint can be used with a RegularHuman and SuperHuman type. This saves us having to write some kind of if / then statement tha figures out which type the variable belongs to and printing it out accordingly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL