YUqori darajali ierarxiyani tuzish.
Xosila sinf boshqa bir xosila sinfga baza sinfga aylansa bu yuqori darajali ierarxiyaga bo’ladi. Bu xolatda oxirgi xosila sinf o’zidan oldingi barcha sinflarni xususiyatini o’z ichiga oladi.
Quyidagi keltirilgan ierarxiyada xosila sinfi bo’lmish Triangle sinfi ColorTriangle sinfiga baza sinf rolini o’ynaydi:
G’G’ Mnogourovnevaya ierarxiya.
using System;
class TwoDShape (
double pri_width; G’G’ Zakro’to’y chlen.
double pri_height; G’G’ Zakro’to’y chlen.
G’G’ Konstruktor po umolchaniyu.
public TwoDShape() (
width q height q 0.0;
}
G’G’ Konstruktor klassa TwoDShape.
public TwoDShape(double w, double h) {
width q w;
height q h;
G’G’ Konstruktor, sozdayuhiy ob’ekto’, u kotoro’x
G’G’ shirina ravna vo’sote.
public TwoDShape(double x) {
width - height q x;
}
G’G’ Svoystva width i height.
public double width {
get { return pri_width; }
set { pri_width q value; }
}
public double height {
get {return pri_height; }
set { pri_height q value; }
}
public void showDim() {
Console.WriteLine("Shirina i vo’sota ravno’ " Q
width Q " i " Q height); }
} G’G’ Klass treugolnikov, proizvodno’y ot klassa TwoDShape.
class Triangle : TwoDShape {
string style; G’G’ Zakro’to’y chlen.
G’* Konstruktor po umolchaniyu. On vo’zo’vaet konstruktor
po umolchaniyu klassa TwoDShape. *G’
public Triangle () {
style q "null";
}
G’G’ Konstruktor s parametrami.
public Triangle(string s, double w, double h) : base(w, h) {
style q s;
}
G’G’ Sozdaem ravnobedrenno’y treugolnik.
public Triangle(double x) : base(x) {
style q "ravnobedrenno’y";
}
G’G’ Metod vozvrahaet znachenie plohadi treugolnika,
public double area() {
return width * height G’ 2;
}
G’G’ Metod otobrajaet tip treugolnika.
public void showStyle() {
Console.WriteLine("Treugolnik " Q style);
}
}
G’G’ Prodoljaem ierarxiyu klassov treugolnikov.
class ColorTriangle : Triangle { string color;
public ColorTriangle(
string c, string s,
double w, double h) : base(s, w, h) {
color q c;
}
G’G’ Metod otobrajaet svet treugolnika.
public void showColor() {
Console.WriteLine("Svet " Q color); }
}
}
class Shapes6 {
public static void Main () {
ColorTriangle t1 q
new ColorTriangle("siniy", "pryamougolno’y",
8.0, 12.0);
ColorTriangle t2 q
new ColorTriangle("krasno’y", "ravnobedrenno’y",
2.0, 2.0) ;
Console.WriteLine("Informatsiya o t1: ");
t1.ShowStyle () ;
t1.showDim () ;
tl.showColor() ;
Console.WriteLine ("Plohad ravna " Q t1.area());
Console.WriteLine();
Console.WriteLine("Informatsiya o t2: ");
t2.showStyle() ;
t2.showDim();
t2.showColor();
Console.WriteLine("Plohad ravna " Q t2.area()); }
}
Dastur bajarilgandan so’ng quyidagi natijaga erishamiz.
Informatsiya o t1:
Treugolnik pryamougolno’y
Shirina i vo’sota ravno’ 8 i 12
Svet siniy
Plohad ravna 4 8
Informatsiya o t2:
Treugolnik ravnobedrenno’y
Shirina i vo’sota ravno’ 2 i 2
Svet krasno’y
Plohad ravna 2
Bunda ColorTriangle sinfi Triangle va TwoDShape xususiyatlaridan tuliq foydalangan xolda o’zining color maydonini qo’shadi.
Do'stlaringiz bilan baham: |