Baza sinfini konstruktori chaqirish
Xosila sinfi o’zining baza sinfidagi konstruktorni xosila sinfdagi konstruktorni kengaytirilgan shaklidan va base so’zidan foydalangan xolda chaqirishi mumkin.
G’G’ Dobavlenie konstruktorov v klass TwoDShape.
using System;
G’G’ Klass dvumerno’x ob’ektov.
class TwoDShape {
double pri_width; G’G’ Zakro’to’y chlen.
double pri_height; G’G’ Zakro’to’y chlen.
G’G’ Konstruktor klassa TwoDShape.
public TwoDShape(double w, double h) {
width q w;
height q h;
}
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’G’ Vo’zo’vaem konstruktor bazovogo klassa.
public Triangle(string s,
double w,
double h) : base(w, h) {
style q s;
}
G’G’ Metod vozvrahaet plohad treugolnika.
public double area () {
return width * height G’ 2;
}
G’G’ Otobrajaem tip treugolnika.
public void showStyle() {
Console.WriteLine("Treugolnik " Q style);
}
}
class Shapes4 {
public static void Main( )
Triangle t1 q new Triangle ("ravnobedrenno’y", 4.0, 4.0);
Triangle t2 q new Triangle ("pryamougolno’y", 8.0, 12.0);
Console.WriteLine("Informatsiya o t1: ");
t1.showStyle ( ) ;
t1.showDim();
Console.WriteLine ("Plohad ravna " Q t1.area ( ));
Console.WriteLine ( );
Console.WriteLine ("Informatsiya o t2: ");
t2.showStyle ( );
t2.showDim ( );
Console.WriteLine("Plohad ravna " Q t2.area());
}
}
Bu yerda Triangle konstruktori w va h parametrli base uslubini chaqiradi, xaqiqatda esa bu wight va height xususiyatli TwoDShape konstruktoridir. Ushbu xususiyatlarni Triangle konstruktori TwoDShape konstruktoridan olib o’zi fakat style xususiyatini shakllashtiradi xolos.
G’G’ Dobavlyaem v klass TwoDShape konstruktoro’.
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 s parametrami.
public TwoDShape(double w, double h) {
width q w;
height q h;
}
G’G’ Sozdaem ob’ekt, u kotorogo shirina ravna vo’sote.
public TwoDShape(double x) {
width q 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 TwoDShape.
class Triangle : TwoDShape {
string style; G’G’ Zakro’to’y chlen.
G’* Konstruktor po umolchaniyu. On avtomaticheski vo’zo’vaet
konstruktor po umolchaniyu klassa TwoDShape. *G’
public Triangle() {
style q "null";
}
G’G’ Konstruktor, kotoro’y prinimaet tri argumenta.
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 plohad treugolnika.
public double area() {
return width * height G’2;
}
G’G’ Otobrajaem tip treugolnika.
public void showStyle () {
Console.WriteLine("Treugolnik " Q style);
}
}
class Shapes5
public static void Main() {
Triangle t1 q new Triangle();
Triangle t2 q new Triangle("pryamougolno’y", 8.0, 12.0);
Triangle t3 q new Triangle(4.0);
t1 q t2;
Console.WriteLine("Informatsiya o t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Plohad ravna " Q tl.area());
Console.WriteLine();
Console.WriteLine("Informatsiya o t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Plohad ravna " Q t2.area());
Console.WriteLine();
Console.WriteLine("Informatsiya o t3: ");
t3.showStyle () ;
t3.showDim () ;
Console.WriteLine ("Plohad ravna " Q t3.area ( ));
Console.WriteLine ( );
}
}
Ushbu usuldagi dasturni bajarilishi natijasida quyidagi natijaga erishamiz:
Informatsiya o t1:
Treugolnik pryamougolno’y
Shirina i vo’sota ravno’ 8 i 12
Plohad ravna 4 8
Informatsiya o t2:
Treugolnik pryamougolno’y
Shirina i vo’sota ravno’ 8 i 12
Plohad ravna 48
Informatsiya o t3:
Treugolnik ravnobedrenno’y
Shirina i vo’sota ravno’ 4 i 4
Plohad ravna 8
Base-mexanizm konsepsiyasini ko’rib chiqamiz. Base- usul hosila sinfi to’g’ridan-to’g’ri baza sinfi konstruktorini chaqiradi. Bu holda, base kalit so’zi doimo chaqiriluvchi sinflar ierarxiyasida turgan baza sinfiga
ishlatiladi. Bu yuqori darajali ierarxiya uchun asoslidir. Baza sinfi konstruktori argumentlarini berish uchun base-usul argumentlari sifatida ko’rsatish yetarlidir.
Do'stlaringiz bilan baham: |