Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created January 26, 2016 13:39
Show Gist options
  • Save Viacheslav77/1e36a0beab507f30f60a to your computer and use it in GitHub Desktop.
Save Viacheslav77/1e36a0beab507f30f60a to your computer and use it in GitHub Desktop.
Figures Polimorfizm
package Figures_polimorfism;
public class Circle extends Rectangle {
public Circle(){
r=5;
//System.out.println("Конструктор Круг");
}
public Circle( int r){
this.r=r;
//System.out.println("Конструктор Круг");
}
@Override
public String getName(){
return "Круг с радиусом: " + r + " ";
}
@Override
public int Area(){
return (int) (Math.PI*Math.pow(r, 2));
}
}
package Figures_polimorfism;
public abstract class Figures {
protected int a;
protected int b;
protected int c;
protected int r;
public Figures(){
//System.out.println("Конструктор Фигуры");
}
public String getName(){
return "Фигура";
}
public int getA (){
return a;
}
public int getB (){
return b;
}
public int getC (){
return c;
}
public int getR (){
return r;
}
public abstract int Area();
}
package Figures_polimorfism;
//Построить иерархию классов «Фигуры» с учетом знаний о
//полиморфизме. У каждого класса фигуры должен быть
//метод подсчета площади. Создать список фигур. Вывести
//площади всех фигур на экран.
public class MyClass {
public static void main (String [] args){
Figures [] list = {
new Triangle (),
new Rectangle(),
new Circle(),
new Triangle (9,4,5,4),
new Rectangle(2,5),
new Circle(8)
};
System.out.println("------------------------------------------------------");
for(Figures f : list){
System.out.println(f.getName() + " и площадью = " + f.Area());
System.out.println("------------------------------------------------------");
}
}
}
package Figures_polimorfism;
public class Rectangle extends Triangle {
public Rectangle(){
a=5;b=6;
//System.out.println("Конструктор Прямоугольник");
}
public Rectangle(int a, int b){
this.a=a;
this.b=b;
//System.out.println("Конструктор Прямоугольник");
}
@Override
public String getName(){
return "Прямоугольник со сторонами: " + a + " x " + b + " ";
}
@Override
public int Area(){
return a*b;
}
}
package Figures_polimorfism;
public class Triangle extends Figures {
public Triangle(){
a=5;b=4;c=6;r=4;
//System.out.println("Конструктор Треугольник");
}
public Triangle(int a, int b, int c, int r){
this.a=a;
this.b=b;
this.c=c;
this.r=r;
//System.out.println("Конструктор Треугольник");
}
@Override
public String getName(){
return "Треугольник со сторонами: " + a + " x " + b + " x " + c;
}
@Override
public int Area(){
return (a*b*c)/(4*r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment