http://www.genbetadev.com/java-j2ee/java-8-metodos-default
http://codecriticon.com/default-static-interfaces-jdk8/
http://www.genbetadev.com/java-j2ee/java-8-metodos-default
http://codecriticon.com/default-static-interfaces-jdk8/
/*
* This code is sample code, provided as-is, and we make no
* warranties as to its correctness or suitability for
* any purpose.
*
* We hope that it's useful to you. Enjoy.
* Copyright 2004-13 LearningPatterns Inc.
*/
/*
* Lab - Interfaces
*
* This class defines a Radio. It will implement Volume easily
*
*
* This class performs no data validation; however, it
* mutes/unmutes appropriately, i.e., can't mute a muted
* Radio
*/
package com.entertainment;
// TODO add in declaration for implementing Volume
public class Radio
{
// INSTANCE VARIABLES
private int volume; // the volume
// for muting behavior
private int oldVolume;
private boolean isMuted;
// CONSTRUCTORS
public Radio()
{
super();
}
public Radio(int volumeIn)
{
super();
this.setVolume(volumeIn);
}
// ACCESSOR METHODS
// from interface Volume
public void setVolume(int volumeIn)
{
// delegate to contained VolumeControl object
volume = volumeIn;
}
/* TODO - Uncomment this method so Radio will compile after implementing the interface
// from interface IVolume
public int getVolume()
{
return volume;
}
*/
// from interface IVolume
// BEHAVIORAL METHODS
public void mute()
{
if (!this.isMuted()) // not currently muted
{
// store current volume in oldVolume
oldVolume = volume;
// set volume to zero
this.setVolume(0);
}
else // currently muted
{
// restore volume from oldVolume value
this.setVolume(oldVolume);
}
// toggle muted flag
isMuted = !isMuted;
}
// from interface IVolume
public boolean isMuted()
{
// delegate to contained VolumeControl object
return isMuted;
}
// returns a string representation of this class instance
@Override
public String toString()
{
// this method uses the "ternary" (three terms) operator
// (expression ? true-value : false-value)
// it's just a very compact if-else statement
return "Radio: volume=" +
(!this.isMuted() ? String.valueOf(volume) : "<muted>");
}
}
Television[] tls={new Television("Miro"),
new ColorTelevision("MediaMarkt",5)};
for(Television t:tls){
System.out.println(t);
}
package com.entertaiment;
public class ColorTelevision extends Television {
public ColorTelevision(String proveedor) {
super(proveedor);
}
public ColorTelevision(String proveedor, int color) {
super(proveedor);
colorTv=color;
}
private int colorTv;
public ColorTelevision(String marca, int volumen, String proveedor) {
super(marca, volumen, proveedor);
// TODO Auto-generated constructor stub
}
public String getMarca(){
return super.getMarca()+" TV!";
}
public String toString(){
return super.toString()+" | "+colorTv;
}
}
public void setMarca(String marca) {
if (marca.equals("Sony") || marca.equals("Zenith") || marca.equals("Hitachi") || marca.equals("RCA")){
this.marca = marca; }
}
public class TestRefTest {
public static void main(String[] args) {
TestRef c1=new TestRef();
TestRef c2=new TestRef();
c1.numero=3;
c1.texto="c1";
c2.numero=30;
c2.texto="c2";
System.out.println(c1.toString());
System.out.println(c2.toString());
TestRef c3=c1;
System.out.println(c3.toString());
c3.texto="c3";
System.out.println(c1.toString());
System.out.println(c1==c3);
c3.cambiar2(c1);
System.out.println(c1.toString());
c3.texto="c2";
System.out.println(c1==c3);
//Destruir objetos
c1=null;
c3=null;
for(int i=0;i<10;i++){
System.out.println(i);
}
}
}
La clase:
public class TestRef {
public int numero;
public String texto;
public String toString(){
return numero+"|"+texto;
}
public void cambiar(TestRef c){
c=null;
}
public void cambiar2(TestRef c){
c.texto="CAMBIADO";
}
}