菜鸟求助C#问题
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public class Point3D : Point
{
public int z;
public Point3D(int x, int y, int z)
: base(x, y)
{
this.z = z;
}
}
static void Main(string[] args)
{
Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);
Console.WriteLine("A:{0}\nB:{1}\n", a.x, b.y);
Console.WriteLine("Z:{0}", b.z);
}
}
}
一个类继承它的基类的成员 继承意味着一个类隐式地包含其基类的所有成员,但基类的构造函数除外。派生类能够在继承基类的基础上添加新的成员,但是它不能移除继承成员的定义。 ,Point3D 类从 Point 类继承了 x 字段和 y 字段,每个 Point3D 实例都包含三个字段 x、y 和 z。
从某个类类型到它的任何基类类型存在隐式的转换。 类类型的变量可以引用该类的实例或任何派生类的实例。例如,对于前面给定的类声明,Point 类型的变量既可以引用 Point 也可以引用 Point3D:
Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);
为什么不能输出b.z?????