class __: __ {
//
}
. C# . ( C# C++, . C++ #.) , . (, , .) , , , .
: , , . . , TwoDShape .
// , TwoDShape. class Rectangle: TwoDShape {
// true, // , public bool IsSquare() {
if(Width == Height) return true; return false;
}
// , public double Area() { return Width * Height;
}
}
Rectangle TwoDShape, Is Square (), , , Area (), .
8, , . , . , , . , TwoDShape, Triangle, .
// .
// .
Using System;
// , class TwoDShape {
double Width; // double Height; //
public void ShowDimO {
Console.WriteLine(" " +
Width + " " + Height);
}
}
// Triangle, TwoDShape. class Triangle: TwoDShape {
public string Style; //
// , public double Area() {
return Width * Height /2; // ,
|
|
//
}
// , public void ShowStyle() {
Console.WriteLine(" " + Style);
}
}
Triangle , Width Height Area () . Width Height , , .
. , .
, , . . C# . (protected) , , .
, . , . , , , , .
TwoDShape, Width Height . : Width Height. , , Width Height , .
// // . .
Using System;
// , class TwoDShape {
double pri_width; // double pri_height; //
// , public double Width {
get { return pri_width; }
set { pri_width = value < 0? -value: value; }
}
public double Height {
get { return pri_height; }
set { pri_height = value < 0? -value: value; }
}
public void ShowDim() {
Console.WriteLine(" " +
Width + " " + Height);
}
}
// ,
// TwoDShape.
class Triangle: TwoDShape {
public string Style; //
// , public double Area() {
return Width * Height / 2;
}
// , public void ShowStyleO {
Console.WriteLine(" " + Style);
}
}
class Shapes2 {
static void Main() {
Triangle tl = new Triangle ();
Triangle t2 = new Triangle ();
tl.Width = 4.0;
tl.Height = 4.0;
tl.Style = "";
t2.Width = 8.0;
t2.Height = 12.0;
t2.Style = "";
Console.WriteLine(" tl: "); tl.ShowStyle(); tl.ShowDim();
|
|
Console. WriteLine (" " + tl.AreaO);
Console.WriteLine ();
Console.WriteLine(" t2: "); t2.ShowStyle(); t2.ShowDim();
Console.WriteLine(" " + t2.Area());
}
}
Width Height pri_width pri_height TwoDShape, . , pri_width pri_height TwoDShape , , - .
. Java. , Java , C# . , Java , C# . , - C# , C++.
, . , . , , . , , C# . , .
protected. protected, , , . , , . , protected, , , .