Virtual usullarning qo’llanishi.
Virtual usullar kuchini yanada yaxshiroq sezish uchun uni TwoDShape sinfiga qo’llaymiz. Oldingi misollarda area () usulida aniqlanishi keltirilgan. Figiralar yuzasini hisoblashda TwoDShape sinfida area() virtual usulidan foydalanish qulayroq ekanligi to’g’risida fikr tug’iladi. Buni keyingi keltirilgan programmada ko’rish mumkin. qulaylik uchun TwoDShape sinfiga name xususiyati kiritilgan.
G’G’ Ispolzovanie virtualno’x metodov i polimorfizma.
using System;
class TwoDShape {
double pri_width; G’G’ Zakro’to’y chlen.
double pri_height; G’G’ Zakro’to’y chlen.
string pri_name; G’G’ Zakro’to’y chlen.
G’G’ Konstruktor po umolchaniyu.
public TwoDShape() {
width q height q 0.0;
name q "null";
}
G’G’ Konstruktor s parametrami.
public TwoDShape(double w, double h, string n) {
width q w;
height q h;
name q n;
}
G’G’ Sozdaem ob’ekt, u kotorogo shirina ravna vo’sote.
public TwoDShape(double x, string n) {
width q height q x;
name q n;
}
G’G’ Sozdaem ob’ekt iz ob’ekta.
public TwoDShape(TwoDShape ob) {
width q ob.width;
height q ob.height;
name q ob.name;
}
G’G’ Svoystva width, height i name.
public double width {
get { return pri_width; }
set { pri_width q value; }
}
public double height {
get ( return pri_height; }
set { pri_height - value; }
}
public string name {
get { return pri_name; }
set { pri_name q value; ) }
public void showDim() {
Console.WriteLine("Shirina i vo’sota ravno’ " Q
width Q " i " Q height);
public virtual double area() {
Console.WriteLine(
"Metod area() neobxodimo pereopredelit.");
return 0.0;
}
}
G’G’ Klass treugolnikov, proizvodno’y ot klassa TwoDShape.
class Triangle : TwoDShape {
string style; G’G’ Zakro’to’y chlen.
G’G’ Konstruktor po umolchaniyu.
public Triangle() {
style - "null";
}
G’G’ Konstruktor s parametrami.
public Triangle(string s, double w, double h) :
base(w, h, "treugolnik") {
style q s;
}
G’G’ Sozdaem ravnobedrenno’y treugolnik.
public Triangle(double x) : base(x, "treugolnik") {
style q "ravnobedrenno’y";
}
G’G’ Sozdaem ob’ekt iz ob’ekta.
public Triangle(Triangle ob) : base(ob) {
style q ob.style;
}
G’G’ Pereopredelyaem metod area() dlya klassa Triangle,
public override double area() {
return width * height G’ 2;
}
G’G’ Metod otobrajaet tip treugolnika.
public void showStyle() {
Console.WriteLine("Treugolnik " Q style);
}
}
G’G’ Klass pryamougolnikov, proizvodno’y ot klassa TwoDShape.
class Rectangle : TwoDShape {
G’G’ Konstruktor s parametrami.
public Rectangle(double w, double h) :
base(w, h, "pryamougolnik"){ }
G’G’ Sozdaem kvadrat.
public Rectangle(double x) :
base(x, "pryamougolnik") { }
G’G’ Sozdaem ob’ekt iz ob’ekta.
public Rectangle(Rectangle ob) : base(ob) { }
G’G’ Metod vozvrahaet true, yesli pryamougolnik - kvadrat.
public bool isSquare() {
if(width qq height) return true;
return false;
}
G’G’ Pereopredelyaem metod area() dlya klassa Rectangle.
public override double area() {
return width * height;
}
class DynShapes {
public static void Main() {
TwoDShape[] shapes q new TwoDShape[5];
shapes[0] - new Triangle("pryamougolno’y", 8.0, 12.0);
shapes[1] q new Rectangle(10);
shapes[2] q new Rectangle(10, 4);
shapes[3] q new Triangle(7.0);
shapes[4] q new TwoDShape(10, 20,
"zagotovka dlya figuro’");
for(int iq0; i < shapes.Length; iQQ) {
Console.WriteLine("Ob’ektom yavlyaetsya " Q
shapes[i].name);
Console.WriteLine("Plohad ravna " Q
shapes [i].area () ) ;
Console.WriteLine();
}
}
}
Ushbu dasur bajarilgandan so’ng quyidagi natijani olamiz:
Ob’ektom yavlyaetsya treugolnik
Plohad ravna 48
Ob’ektom yavlyaetsya pryamougolnik
Plohad ravna 100
Ob’ektom yavlyaetsya pryamougolnik
Plohad ravna 40
Ob’ektom yavlyaetsya treugolnik
Plohad ravna 24.5
Ob’ektom yavlyaetsya zagotovka dlya figuro’
Metod area() neobxodimo pereopredelit.
Plohad ravna 0
Dasturni batafsil ko’rib chiqamiz. Birinchidan, area() usul virtual kalit so’zi qo’llanilgan TwoDShape sinfida bildirilgan va Triangle va Rectangle sinflarida qayta aniqlangan. Har bir qayta aniqlangan area() usul ob’ekt shakliga mos keluvchi yuzani hisoblaydi. Bu holda, agar ellipslar berilgan bo’lsa, area() usuli bu sinfda ellips yuzasini hisoblaydi.
Do'stlaringiz bilan baham: |