Last active
November 25, 2020 10:45
-
-
Save Kaapiii/1899c7f992fc088787f4168e4013a082 to your computer and use it in GitHub Desktop.
Java Traits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.liechti.traittest.Kreis; | |
public class Bootstrap { | |
public static void main(String[] args) | |
{ | |
init(); | |
} | |
protected static void init() | |
{ | |
Kreis kreis = new Kreis(30, true); | |
System.out.println("Should be visible: " + kreis.isVisible()); | |
kreis.scaleRadiusBy(10); | |
System.out.println("Should be 40: " + kreis.getRadius()); | |
kreis.hide(); | |
System.out.println("Should NOT be visible: " + kreis.isVisible()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.liechti.traittest; | |
public interface Hideable { | |
void setVisible(boolean hide); | |
boolean isVisible(); | |
default void show(){ | |
this.setVisible(true); | |
} | |
default void hide(){ | |
this.setVisible(false); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.liechti.traittest; | |
public class Kreis implements Hideable, Rezisable{ | |
private double radius = 0; | |
private boolean visible = false; | |
public Kreis(){ | |
} | |
public Kreis (double radius, boolean visible){ | |
this.radius = radius; | |
} | |
@Override | |
public void setVisible(boolean isVisible) { | |
this.visible = isVisible; | |
} | |
@Override | |
public boolean isVisible() { | |
return this.visible; | |
} | |
@Override | |
public void setRadius(double radius) { | |
this.radius = radius; | |
} | |
@Override | |
public double getRadius() { | |
return this.radius; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.liechti.traittest; | |
public interface Rezisable { | |
void setRadius(double radius); | |
double getRadius(); | |
/** | |
* @param amount | |
*/ | |
default void shrinkRadiusBy(double amount){ | |
double radius = this.getRadius(); | |
radius -= amount; | |
if(radius > 0) { | |
this.setRadius(radius); | |
} | |
} | |
/** | |
* @param amount | |
*/ | |
default void scaleRadiusBy(double amount){ | |
double radius = this.getRadius(); | |
radius += amount; | |
this.setRadius(radius); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment