Created
November 22, 2015 02:12
-
-
Save FreeFly19/6ad29f89f2e0a84cd6cc 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
import java.util.Scanner; | |
class Human{ | |
int age; | |
String name; | |
public void sayAboutSelf(){ | |
System.out.println("My name is " + this.name + ". I am " + this.age + " years old."); | |
} | |
} | |
class First { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
Human h1 = new Human(); | |
System.out.print("Enter name of new human: "); | |
h1.name = sc.nextLine(); | |
System.out.print("And age:"); | |
h1.age = sc.nextInt(); | |
h1.sayAboutSelf(); | |
} | |
} |
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 Duck{ | |
public void quack(){ | |
System.out.println("quack"); | |
} | |
} | |
class FirstDuck extends Duck{ | |
public void quack(){ | |
System.out.println("quack-quack-kryaaaa!!!"); | |
} | |
} | |
class SecondDuck extends Duck{ | |
public void quack(){ | |
System.out.println("I can't quack!"); | |
} | |
} | |
class Polymorphism { | |
public static void ducksWantQuack(Duck[] d){ | |
for (int i = 0; i < d.length; i++) { | |
d[i].quack(); | |
} | |
} | |
public static void main(String[] args) { | |
Duck d1 = new Duck(); | |
FirstDuck d2 = new FirstDuck(); | |
SecondDuck d3 = new SecondDuck(); | |
Duck[] ducks = new Duck[4]; | |
ducks[0] = d1; | |
ducks[1] = d2; | |
ducks[2] = d3; | |
ducks[3] = new FirstDuck(); | |
ducksWantQuack(ducks); | |
} | |
} |
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 Human { | |
int age; | |
String name; | |
public void sayAboutSelf(){ | |
System.out.println("My name is " + this.name + ". I am " + this.age + " years old."); | |
} | |
} | |
class Telephone { | |
String model; | |
String brand; | |
String number; | |
public void showInfo(){ | |
System.out.println("Model: " + this.model); | |
System.out.println("Brand: " + this.brand); | |
System.out.println("Number: " + this.number); | |
} | |
} | |
class ModernHuman extends Human { | |
Telephone tel; | |
public void sayAboutSelf(){ | |
super.sayAboutSelf();//Виклик метода з батьківського класу | |
System.out.println("And i have this phone:"); | |
this.tel.showInfo(); | |
} | |
} | |
class Main { | |
public static void main(String[] args) { | |
ModernHuman h1 = new ModernHuman(); | |
h1.name = "Sasha"; | |
h1.age = 19; | |
h1.tel = new Telephone(); | |
h1.tel.brand = "Sony"; | |
h1.tel.model = "ST21i2"; | |
h1.tel.number = "+380930380763"; | |
h1.sayAboutSelf(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment