Created
October 1, 2016 16:56
-
-
Save MykolaBova/f0f752f491ddc55f3e4326df105ec230 to your computer and use it in GitHub Desktop.
This file contains 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
class Instanceof { | |
public static void main(String[] args) { | |
Car myCar = new SportCar(); | |
if(myCar instanceof SportCar) { | |
System.out.println("Sport Cat"); | |
} | |
if(myCar instanceof Truck) { | |
System.out.println("Truck"); | |
} | |
if(myCar instanceof Car) { | |
System.out.println("Car"); | |
} | |
} | |
} | |
class Car { | |
protected int speedMax = 100; | |
private long id; | |
public Car(long id, int speedMax) { | |
this.id = id; | |
this.speedMax = speedMax; | |
} | |
public Car() { | |
} | |
@Override | |
public int hashCode() { | |
int result = (int) (id ^ (id >>> 32)); | |
result = 31 * result + speedMax; | |
return result; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if(this == o) return true; | |
if(o == null || getClass() != o.getClass()) return false; | |
Car car = (Car) o; | |
if(id != car.id) return false; | |
return true; | |
} | |
public int getSpeedMax() { | |
return speedMax; | |
} | |
@Override | |
public String toString() { | |
return "My name is car."; | |
} | |
public Car getNewCar() { | |
return new Car(); | |
} | |
public void move() { | |
System.out.println("Car move."); | |
} | |
} | |
class Truck extends Car { | |
@Override | |
public void move() { | |
System.out.println("Truck move."); | |
} | |
} | |
class SportCar extends Car { | |
protected int speedMax = 300; | |
public SportCar(long id, int speedMax) { | |
super(id, speedMax); | |
} | |
public SportCar() { | |
} | |
public int getSpeedMax() { | |
return speedMax; | |
} | |
@Override | |
public String toString() { | |
return super.toString() + "Sport car."; | |
} | |
public void testSuper() { | |
System.out.println(super.speedMax); | |
System.out.println(this.speedMax); | |
} | |
@Override | |
public SportCar getNewCar() { | |
return new SportCar(); | |
} | |
@Override | |
public void move() { | |
System.out.println("Sport car move."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment