View Code
案例1:protected 访问修饰符只允许在子类中被访问,不予许在其他外部类中访问 namespace e1 { class Point { //protected 访问修饰符只允许在子类中被访问,不予许在其他外部类中访问 protected int x; protected int y; //在当前程序集中被访问 //internal int s; } class DeriverdPoint : Point { public DeriverdPoint(int x, int y) { this.x = x; this.y = y; Console.WriteLine("坐标为{0},{1}", x, y); } } class Program { static void Main(string[] args) { DeriverdPoint dp = new DeriverdPoint(10, 20); //编译错误 //Point p=new Point() Console.ReadKey(); } } } —————————————————————————————————————————————————— 案例2:程序集的添加引用 namespace ClassLibrary1 { public class Point { //protected internal 可以允许当前程序集中被访问 //protected internal非当前程序集中,允许它的子类被访问 protected internal int x; protected internal int y; internal int s; } class Test { public void Show() { Point p = new Point(); p.x = 10; p.y = 20; //诚意"S",访问修饰符为internal //所以可以被当前的程序集下所有的类访问! p.s = 100; } } } —————————————————————————————————————————— using System; using System.Collections.Generic; using System.Text; using ClassLibrary1; namespace e2 { class DeviredPoint:Point { public DeviredPoint() { this.x=10; this.y=20; Console.WriteLine("{0},{1}",x,y); } } class Program { static void Main(string[] args) { DeviredPoint d=new DeviredPoint(); Console.ReadKey(); /*一个程序集可以包含若干个命名空间,一个命名空间底下可以包含若干个类 */ } } } —————————————————————————————————————— 案例3:protected关键字的运用 只允许子类调用! namespace e3 { //protected关键字的运用 只允许子类调用! public class Vehicle { protected void vehicleRun() { Console.WriteLine("汽车启动了............."); } } class Truck:Vehicle { protected void truckRun() { Console.WriteLine("卡车来了........."); } static void Main(string[] args) { Truck truck = new Truck(); truck.vehicleRun(); truck.truckRun(); Console.ReadKey(); } } } ———————————————————————————————————————————————————— 案例4 继承的单根性 namespace e4 { //继承的单根性 /*C#中继承只能有一个父类,不能有多个父类 * 比如: SmallTruck:Truk,Vehicle会报错 *但C#可以继承自多个接口 */ class Vehicle { protected void VehicleRun() { Console.WriteLine("汽车正在行驶。。。。"); } } class Truk : Vehicle { protected void TruckRun() { Console.WriteLine("卡车正在行驶。。。。"); } } class SmallTruck : Truk { protected void SmallTruckRun() { Console.WriteLine("小型卡车正在行驶。。。。"); } static void Main(string[] args) { SmallTruck st = new SmallTruck(); st.VehicleRun(); st.TruckRun(); st.SmallTruckRun(); Console.ReadKey(); } } } —————————————————————————————————————————— 案例5: namespace e5 { class A { public A() { Console.WriteLine("A的构造函数被调用了。。。"); } } class B:A { //相当于在子类中加入:base() public B() { Console.WriteLine("B的构造函数被调用了。。。"); } } class Program { static void Main(string[] args) { /*C#在继承中,当你new一个子类对象的时候,在调用子类构造函数前先去调用父类的 * 构造函数,当父类的构造函数执行完毕后,再执行子类的构造函数*/ B b = new B(); Console.ReadKey(); } } } —————————————————————————————————————————————————— 案例6: namespace e6 { class Person { private string name; /*一旦我们为一个类显示定义了构造函数,C#编译器就不会为我们隐式添加一个 * 无参的构造函数*/ //public Person()//1.为父类创建无参的构造函数 //{ //} public Person(string name) { this.name = name; } } class Student : Person { //子类中的构造函数默认情况下会去查找父类缺省的构造函数,并调用 //如果找不到缺省无参的构造函数,编译器会报错! //可以使用两种方式来避免这个错误: //1.为父类创建无参的构造函数 //2.在子类中显示的去调用父类构造函数 public Student(string name)//2.在子类中显示的去调用父类构造函数 : base(name) { } } class Program { static void Main(string[] args) { } } }