Csharp/C Sharp/Class Interface/Class Access Modifiers — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:39, 26 мая 2010

Demonstrate protected

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// 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); 
  } 
} 
 
public 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 
  } 
}


illustrates member accessibility

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example7_3.cs illustrates member accessibility
*/
using System;

// declare the MotorVehicle class
class MotorVehicle
{
  // declare the fields
  private   string make;
  protected string model;
  // define a constructor
  public MotorVehicle(string make, string model)
  {
    this.make = make;
    this.model = model;
  }
  // define the Start() method (may be overridden in a
  // derived class)
  public virtual void Start()
  {
    TurnStarterMotor();
    System.Console.WriteLine("Vehicle started");
  }
  // define the TurnStarterMotor() method
  private void TurnStarterMotor()
  {
    System.Console.WriteLine("Turning starter motor...");
  }
}

// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{
  // define a constructor
  public Car(string make, string model) :
  base(make, model)
  {
    // do nothing
  }
  // override the base class Start() method
  public override void Start()
  {
    Console.WriteLine("Starting " + model);  // model accessible
    base.Start();  // calls the Start() method in the base class
    // Console.WriteLine("make = " + make);  // make is not accessible
  }
}

public class Example7_3
{
  public static void Main()
  {
    // create a Car object and call the object"s Accelerate() method
    Car myCar = new Car("Toyota", "MR2");
    myCar.Start();
    // make and model are not accessible, so the following two lines
    // are commented out
    // Console.WriteLine("myCar.make = " + myCar.make);
    // Console.WriteLine("myCar.model = " + myCar.model);
  }
}


illustrates member hiding

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example7_4.cs illustrates member hiding
*/
using System;

// declare the MotorVehicle class
class MotorVehicle
{
  // declare the fields
  public string make;
  public string model;
  // define a constructor
  public MotorVehicle(string make, string model)
  {
    Console.WriteLine("In MotorVehicle constructor");
    this.make = make;
    this.model = model;
    Console.WriteLine("this.make = " + this.make);
    Console.WriteLine("this.model = " + this.model);
  }
  // define the DisplayModel() method
  public void DisplayModel()
  {
    Console.WriteLine("In MotorVehicle DisplayModel() method");
    Console.WriteLine("model = " + model);
  }
}

// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{
  // hide the base class model field
  public new string model;
  // define a constructor
  public Car(string make, string model) :
  base(make, "Test")
  {
    Console.WriteLine("In Car constructor");
    this.model = model;
    Console.WriteLine("this.model = " + this.model);
  }
  // hide the base class DisplayModel() method
  public new void DisplayModel()
  {
    Console.WriteLine("In Car DisplayModel() method");
    Console.WriteLine("model = " + model);
    base.DisplayModel();  // calls DisplayModel() in the base class
  }
}

public class Example7_4
{
  public static void Main()
  {
    // create a Car object
    Console.WriteLine("Creating a Car object");
    Car myCar = new Car("Toyota", "MR2");
    Console.WriteLine("Back in Main() method");
    Console.WriteLine("myCar.make = " + myCar.make);
    Console.WriteLine("myCar.model = " + myCar.model);
    // call the Car object"s DisplayModel() method
    Console.WriteLine("Calling myCar.DisplayModel()");
    myCar.DisplayModel();
  }
}


Illustrates the use of various access modifiers

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example5_10.cs illustrates the use of various
  access modifiers
*/

// declare the Car class
class Car
{
  // declare the fields
  public             string make;
  protected internal string model;
  internal           string color;
  protected          int horsepower = 150;
  private            int yearBuilt;
  // define the methods
  public void SetYearBuilt(int yearBuilt)
  {
    this.yearBuilt = yearBuilt;
  }
  public int GetYearBuilt()
  {
    return yearBuilt;
  }
  public void Start()
  {
    System.Console.WriteLine("Starting car ...");
    TurnStarterMotor();
    System.Console.WriteLine("Car started");
  }
  private void TurnStarterMotor()
  {
    System.Console.WriteLine("Turning starter motor ...");
  }
}

public class Example5_10
{
  public static void Main()
  {
    // create a Car object
    Car myCar = new Car();
    // assign values to the Car object fields
    myCar.make = "Toyota";
    myCar.model = "MR2";
    myCar.color = "black";
    // myCar.horsepower = 200;  // protected field not accessible
    // myCar.yearBuilt = 1995;  // private field not accessible
    // call the SetYearBuilt() method to set the private yearBuilt field
    myCar.SetYearBuilt(1995);
    // display the values for the Car object fields
    System.Console.WriteLine("myCar.make = " + myCar.make);
    System.Console.WriteLine("myCar.model = " + myCar.model);
    System.Console.WriteLine("myCar.color = " + myCar.color);
    // call the GetYearBuilt() method to get the private yearBuilt field
    System.Console.WriteLine("myCar.GetYearBuilt() = " + myCar.GetYearBuilt());
    // call the Start() method
    myCar.Start();
    // myCar.TurnStarterMotor();  // private method not accessible
  }
}


Member Accessibility

 

using System;
public class MotorVehicle {
    private string make;
    protected string model;
    public MotorVehicle(string make, string model) {
        this.make = make;
        this.model = model;
    }
    public virtual void Start() {
        TurnStarterMotor();
        System.Console.WriteLine("Vehicle started");
    }
    private void TurnStarterMotor() {
        System.Console.WriteLine("Turning starter motor...");
    }
}
public class Product : MotorVehicle {
    public Product(string make, string model) :
        base(make, model) {
        // do nothing
    }
    public override void Start() {
        Console.WriteLine("Starting " + model);
        base.Start();
    }
}
class MainClass {
    public static void Main() {
        Product myProduct = new Product("Toyota", "MR2");
        myProduct.Start();
    }
}


Member Hiding

 

using System;
public class MotorVehicle {
    public string make;
    public string model;
    public MotorVehicle(string make, string model) {
        Console.WriteLine("In MotorVehicle constructor");
        this.make = make;
        this.model = model;
        Console.WriteLine("this.make = " + this.make);
        Console.WriteLine("this.model = " + this.model);
    }
    public void DisplayModel() {
        Console.WriteLine("In MotorVehicle DisplayModel() method");
        Console.WriteLine("model = " + model);
    }
}

public class Product : MotorVehicle {
    public new string model;
    public Product(string make, string model) :
        base(make, "Test") {
        Console.WriteLine("In Product constructor");
        this.model = model;
        Console.WriteLine("this.model = " + this.model);
    }
    public new void DisplayModel() {
        Console.WriteLine("In Product DisplayModel() method");
        Console.WriteLine("model = " + model);
        base.DisplayModel();
    }
}

class MainClass {
    public static void Main() {
        Console.WriteLine("Creating a Product object");
        Product myProduct = new Product("Toyota", "MR2");
        Console.WriteLine("Back in Main() method");
        Console.WriteLine("myProduct.make = " + myProduct.make);
        Console.WriteLine("myProduct.model = " + myProduct.model);
        Console.WriteLine("Calling myProduct.DisplayModel()");
        myProduct.DisplayModel();
    }
}


Public vs private access

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Public vs private access. 
 
using System; 
 
class MyClass {  
  private int alpha; // private access explicitly specified 
  int beta;          // private access by default 
  public int gamma;  // public access 
 
  /* Methods to access alpha and beta.  It is OK for a 
     member of a class to access a private member 
     of the same class. 
  */ 
  public void setAlpha(int a) { 
    alpha = a;  
  } 
 
  public int getAlpha() { 
    return alpha; 
  } 
 
  public void setBeta(int a) { 
    beta = a;  
  } 
 
  public int getBeta() { 
    return beta; 
  } 
}  
  
public class AccessDemo {  
  public static void Main() {  
    MyClass ob = new MyClass();  
  
    /* Access to alpha and beta is allowed only 
       through methods. */ 
    ob.setAlpha(-99); 
    ob.setBeta(19); 
    Console.WriteLine("ob.alpha is " + ob.getAlpha()); 
    Console.WriteLine("ob.beta is " + ob.getBeta()); 
 
    // You cannot access alpha or beta like this: 
//  ob.alpha = 10; // Wrong! alpha is private! 
//  ob.beta = 9;   // Wrong! beta is private! 
 
    // It is OK to directly access gamma because it is public. 
    ob.gamma = 99;  
   }  
}


Using Access Modifiers

 

public class Product {
    public string make;
    protected internal string model;
    internal string color;
    protected int horsepower = 150;
    private int yearBuilt;
    public void SetYearBuilt(int yearBuilt) {
        this.yearBuilt = yearBuilt;
    }
    public int GetYearBuilt() {
        return yearBuilt;
    }
    public void Start() {
        System.Console.WriteLine("Starting Product ...");
        TurnStarterMotor();
        System.Console.WriteLine("Product started");
    }
    private void TurnStarterMotor() {
        System.Console.WriteLine("Turning starter motor ...");
    }
}

class MainClass {
    public static void Main() {
        Product myProduct = new Product();
        myProduct.make = "Toyota";
        myProduct.model = "MR2";
        myProduct.color = "black";

        myProduct.SetYearBuilt(1995);
        System.Console.WriteLine("myProduct.make = " + myProduct.make);
        System.Console.WriteLine("myProduct.model = " + myProduct.model);
        System.Console.WriteLine("myProduct.color = " + myProduct.color);
        System.Console.WriteLine("myProduct.GetYearBuilt() = " + myProduct.GetYearBuilt());
        myProduct.Start();
    }
}