📄 chap11.lst
字号:
listing 1
// A simple class hierarchy.
using System;
// A class for two-dimensional objects.
class TwoDShape {
public double width;
public double height;
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// Triangle is derived from TwoDShape.
class Triangle : TwoDShape {
public string style; // style of triangle
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
class Shapes {
public static void Main() {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
t1.width = 4.0;
t1.height = 4.0;
t1.style = "isosceles";
t2.width = 8.0;
t2.height = 12.0;
t2.style = "right";
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
listing 2
// Private members are not inherited.
// This example will not compile.
using System;
// A class for two-dimensional objects.
class TwoDShape {
double width; // now private
double height; // now private
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// Triangle is derived from TwoDShape.
class Triangle : TwoDShape {
public string style; // style of triangle
// Return area of triangle.
public double area() {
return width * height / 2; // Error, can't access private member
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
listing 3
// Use properties to set and get private members.
using System;
// A class for two-dimensional objects.
class TwoDShape {
double pri_width; // now private
double pri_height; // now private
// Properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
public string style; // style of triangle
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
class Shapes2 {
public static void Main() {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
t1.width = 4.0;
t1.height = 4.0;
t1.style = "isosceles";
t2.width = 8.0;
t2.height = 12.0;
t2.style = "right";
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
listing 4
// Demonstrate protected.
using System;
class B {
protected int i, j; // private to B, but accessible by D
public void set(int a, int b) {
i = a;
j = b;
}
public void show() {
Console.WriteLine(i + " " + j);
}
}
class D : B {
int k; // private
// D can access B's i and j
public void setk() {
k = i * j;
}
public void showk() {
Console.WriteLine(k);
}
}
class ProtectedDemo {
public static void Main() {
D ob = new D();
ob.set(2, 3); // OK, known to D
ob.show(); // OK, known to D
ob.setk(); // OK, part of D
ob.showk(); // OK, part of D
}
}
listing 5
// Add a constructor to Triangle.
using System;
// A class for two-dimensional objects.
class TwoDShape {
double pri_width; // private
double pri_height; // private
// properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
// Constructor
public Triangle(string s, double w, double h) {
width = w; // init the base class
height = h; // init the base class
style = s; // init the derived class
}
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
class Shapes3 {
public static void Main() {
Triangle t1 = new Triangle("isosceles", 4.0, 4.0);
Triangle t2 = new Triangle("right", 8.0, 12.0);
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
listing 6
// Add constructors to TwoDShape.
using System;
// A class for two-dimensional objects.
class TwoDShape {
double pri_width; // private
double pri_height; // private
// Constructor for TwoDShape.
public TwoDShape(double w, double h) {
width = w;
height = h;
}
// properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
// Call the base class constructor.
public Triangle(string s, double w, double h) : base(w, h) {
style = s;
}
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
class Shapes4 {
public static void Main() {
Triangle t1 = new Triangle("isosceles", 4.0, 4.0);
Triangle t2 = new Triangle("right", 8.0, 12.0);
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
listing 7
// Add more constructors to TwoDShape.
using System;
class TwoDShape {
double pri_width; // private
double pri_height; // private
// Default constructor.
public TwoDShape() {
width = height = 0.0;
}
// Constructor for TwoDShape.
public TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
public TwoDShape(double x) {
width = height = x;
}
// Properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
/* A default constructor. This automatically invokes
the default constructor of TwoDShape. */
public Triangle() {
style = "null";
}
// Constructor that takes three arguments.
public Triangle(string s, double w, double h) : base(w, h) {
style = s;
}
// Construct an isosceles triangle.
public Triangle(double x) : base(x) {
style = "isosceles";
}
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
class Shapes5 {
public static void Main() {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle("right", 8.0, 12.0);
Triangle t3 = new Triangle(4.0);
t1 = t2;
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
Console.WriteLine();
Console.WriteLine("Info for t3: ");
t3.showStyle();
t3.showDim();
Console.WriteLine("Area is " + t3.area());
Console.WriteLine();
}
}
listing 8
// An example of inheritance-related name hiding.
using System;
class A {
public int i = 0;
}
// Create a derived class.
class B : A {
new int i; // this i hides the i in A
public B(int b) {
i = b; // i in B
}
public void show() {
Console.WriteLine("i in derived class: " + i);
}
}
class NameHiding {
public static void Main() {
B ob = new B(2);
ob.show();
}
}
listing 9
// Using base to overcome name hiding.
using System;
class A {
public int i = 0;
}
// Create a derived class.
class B : A {
new int i; // this i hides the i in A
public B(int a, int b) {
base.i = a; // this uncovers the i in A
i = b; // i in B
}
public void show() {
// this displays the i in A.
Console.WriteLine("i in base class: " + base.i);
// this displays the i in B
Console.WriteLine("i in derived class: " + i);
}
}
class UncoverName {
public static void Main() {
B ob = new B(1, 2);
ob.show();
}
}
listing 10
// Call a hidden method.
using System;
class A {
public int i = 0;
// show() in A
public void show() {
Console.WriteLine("i in base class: " + i);
}
}
// Create a derived class.
class B : A {
new int i; // this i hides the i in A
public B(int a, int b) {
base.i = a; // this uncovers the i in A
i = b; // i in B
}
// This hides show() in A. Notice the use of new.
new public void show() {
base.show(); // this calls show() in A
// this displays the i in B
Console.WriteLine("i in derived class: " + i);
}
}
class UncoverName {
public static void Main() {
B ob = new B(1, 2);
ob.show();
}
}
listing 11
// A multilevel hierarchy.
using System;
class TwoDShape {
double pri_width; // private
double pri_height; // private
// Default constructor.
public TwoDShape() {
width = height = 0.0;
}
// Constructor for TwoDShape.
public TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
public TwoDShape(double x) {
width = height = x;
}
// Properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
/* A default constructor. This invokes the default
constructor of TwoDShape. */
public Triangle() {
style = "null";
}
// Constructor
public Triangle(string s, double w, double h) : base(w, h) {
style = s;
}
// Construct an isosceles triangle.
public Triangle(double x) : base(x) {
style = "isosceles";
}
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
// Extend Triangle.
class ColorTriangle : Triangle {
string color;
public ColorTriangle(string c, string s,
double w, double h) : base(s, w, h) {
color = c;
}
// Display the color.
public void showColor() {
Console.WriteLine("Color is " + color);
}
}
class Shapes6 {
public static void Main() {
ColorTriangle t1 =
new ColorTriangle("Blue", "right", 8.0, 12.0);
ColorTriangle t2 =
new ColorTriangle("Red", "isosceles", 2.0, 2.0);
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
t1.showColor();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
t2.showColor();
Console.WriteLine("Area is " + t2.area());
}
}
listing 12
// Demonstrate when constructors are called.
using System;
// Create a base class.
class A {
public A() {
Console.WriteLine("Constructing A.");
}
}
// Create a class derived from A.
class B : A {
public B() {
Console.WriteLine("Constructing B.");
}
}
// Create a class derived from B.
class C : B {
public C() {
Console.WriteLine("Constructing C.");
}
}
class OrderOfConstruction {
public static void Main() {
C c = new C();
}
}
listing 13
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -