C#部分笔记 二

来源:互联网 发布:淘宝发优惠怎么拿提成 编辑:程序博客网 时间:2024/06/01 18:27

封装

Public 访问修饰符允许一个类将其成员变量和成员函数暴露给其他的函数和对象。任何公有成员可以被外部的类访问。

Private 访问修饰符允许一个类将其成员变量和成员函数对其他的函数和对象进行隐藏。只有同一个类中的函数可以访问它的私有成员。即使是类的实例也不能访问它的私有成员。

Protected 访问修饰符允许子类访问它的基类的成员变量和成员函数。这样有助于实现继承。

Internal 访问说明符允许一个类将其成员变量和成员函数暴露给当前程序中的其他函数和对象。换句话说,带有 internal 访问修饰符的任何成员可以被定义在该成员所定义的应用程序内的任何类或方法访问。
using System;
namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        internal double length;
        internal double width;

        double GetArea()
        {
            return length * width;
        }
       public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {0}", GetArea());
        }
    }//end class Rectangle
    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.length = 4.5;
            r.width = 3.5;
            r.Display();
            Console.ReadLine();
        }
    }
}

C# 中的构造函数

类的 构造函数 是类的一个特殊的成员函数,当创建类的新对象时执行。

构造函数的名称与类的名称完全相同,它没有任何返回类型。
默认的构造函数没有任何参数。但是如果您需要一个带有参数的构造函数可以有参数,这种构造函数叫做参数化构造函数。这种技术可以帮助您在创建对象的同时给对象赋初始值

 class Line
   {
      private double length;   // 线条的长度
      public Line()
      {
         Console.WriteLine("对象已创建");
      }
   }

class Line
   {
      private double length;   // 线条的长度
      public Line(double len)  // 参数化构造函数
      {
         Console.WriteLine("对象已创建,length = {0}", len);
         length = len;
      }
   }

C# 中的析构函数

类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行。

析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数。

析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源。析构函数不能继承或重载。

C# 类的静态成员

我们可以使用 static 关键字把类成员定义为静态的。当我们声明一个类成员为静态时,意味着无论有多少个类的对象被创建,只会有一个该静态成员的副本。

关键字 static 意味着类中只有一个该成员的实例。静态变量用于定义常量,因为它们的值可以通过直接调用类而不需要创建类的实例来获取。静态变量可在成员函数或类的定义外部进行初始化。

using System;
namespace StaticVarApplication
{
    class StaticVar
    {
       public static int num;
        public void count()
        {
            num++;
        }
        public int getNum()
        {
            return num;
        }
    }
    class StaticTester
    {
        static void Main(string[] args)
        {
            StaticVar s1 = new StaticVar();
            StaticVar s2 = new StaticVar();
            s1.count();
            s1.count();
            s1.count();
            s2.count();
            s2.count();
            s2.count();
            Console.WriteLine("s1 的变量 num: {0}", s1.getNum());
            Console.WriteLine("s2 的变量 num: {0}", s2.getNum());
            Console.ReadKey();
        }
    }
}

s1 的变量 num: 6
s2 的变量 num: 6

您也可以把一个成员函数声明为 static。这样的函数只能访问静态变量。静态函数在对象被创建之前就已经存在。

继承

基类的初始化

using System;namespace RectangleApplication{
   class Rectangle
   {
      // 成员变量
      protected double length;
      protected double width;
      public Rectangle(double l, double w)
      {
         length = l;
         width = w;
      }
      public double GetArea()
      {
         return length * width;
      }
      public void Display()
      {
         Console.WriteLine("长度: {0}", length);
         Console.WriteLine("宽度: {0}", width);
         Console.WriteLine("面积: {0}", GetArea());
      }
   }//end class Rectangle
   class Tabletop : Rectangle
   {
      private double cost;
      public Tabletop(double l, double w) : base(l, w)
      { }
      public double GetCost()
      {
         double cost;
         cost = GetArea() * 70;
         return cost;
      }
      public void Display()
      {
         base.Display();
         Console.WriteLine("成本: {0}", GetCost());
      }
   }
   class ExecuteRectangle
   {
      static void Main(string[] args)
      {
         Tabletop t = new Tabletop(4.5, 7.5);
         t.Display();
         Console.ReadLine();
      }
   }
  }
C# 多重继承
C# 不支持多重继承,可以使用接口来实现多重继承
using System;namespace InheritanceApplication{
   
class Shape 
   
{
     
public void setWidth(int w)
     
{
         width 
= w;
     
}
     
public void setHeight(int h)
     
{
         height 
= h;
     
}
     
protected int width;
     
protected int height;
   
}

   
// 基类 PaintCost
   
public interface PaintCost 
   
{
     
int getCost(int area);

   
}
   
// 派生类
   
class Rectangle : Shape, PaintCost
   
{
     
public int getArea()
     
{
         
return (width * height);
     
}
     
public int getCost(int area)
     
{
         
return area * 70;
     
}
   
}
   
class RectangleTester
   
{
     
static void Main(string[] args)
     
{
         
Rectangle Rect = new Rectangle();
         
int area;
         
Rect.setWidth(5);
         
Rect.setHeight(7);
         area 
= Rect.getArea();
         
// 打印对象的面积
         
Console.WriteLine("总面积: {0}", Rect.getArea());
         
Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
         
Console.ReadKey();
     
}
   }
}

接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。

抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。

接口

声明接口
接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。
public interface ITransactions{   // 接口成员   void showTransaction();   double getAmount();}

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace InterfaceApplication{
   
public interface ITransactions
   
{
     
// interface members
     
void showTransaction();
     
double getAmount();
   
}
   
   
public class Transaction : ITransactions
   
{
     
private string tCode;
     
private string date;
     
private double amount;
     
public Transaction()
     
{
         tCode 
= " ";
         date 
= " ";
         amount 
= 0.0;
     
}
     
     
public Transaction(string c, string d, double a)
     
{
         tCode 
= c;
         date 
= d;
         amount 
= a;
     
}
     
     
public double getAmount()
     
{
         
return amount;
     
}
     
     
public void showTransaction()
     
{
         
Console.WriteLine("Transaction: {0}", tCode);
         
Console.WriteLine("Date: {0}", date);
         
Console.WriteLine("Amount: {0}", getAmount());
     
}
   
}
   
class Tester
   
{
     
static void Main(string[] args)
     
{
         
Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
         
Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
         t1
.showTransaction();
         t2
.showTransaction();
         
Console.ReadKey();
     
}
   
}}

多态性

函数重载

您可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明

动态多态性
 abstract
创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能
  • 您不能创建一个抽象类的实例。
  • 您不能在一个抽象类外部声明一个抽象方法。
  • 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 seale
 
using System;
namespace PolymorphismApplication{
   
abstract class Shape
   
{
     
public abstract int area();
   
}
   
class Rectangle: Shape
   
{
     
private int length;
     
private int width;
     
public Rectangle( int a=0, int b=0)
     
{
         length 
= a;
         width 
= b;
     
}
     
public override int area ()
     
{ 
         
Console.WriteLine("Rectangle 类的面积:");
         
return (width * length); 
     
}
   
}

   
class RectangleTester
   
{
     
static void Main(string[] args)
     
{
         
Rectangle r = new Rectangle(10, 7);
         
double a = r.area();
         
Console.WriteLine("面积: {0}",a);
         
Console.ReadKey();
     
}
   
}}

virtual
using System;namespace PolymorphismApplication{
   
class Shape 
   
{
     
protected int width, height;
     
public Shape( int a=0, int b=0)
     
{
         width 
= a;
         height 
= b;
     
}
     
public virtual int area()
     
{
         
Console.WriteLine("父类的面积:");
         
return 0;
     
}
   
}
   
class Rectangle: Shape
   
{
     
public Rectangle( int a=0, int b=0): base(a, b)
     
{

     
}
     
public override int area ()
     
{
         
Console.WriteLine("Rectangle 类的面积:");
         
return (width * height); 
     
}
   
}
   
class Triangle: Shape
   
{
     
public Triangle(int a = 0, int b = 0): base(a, b)
     
{
     
     
}
     
public override int area()
     
{
         
Console.WriteLine("Triangle 类的面积:");
         
return (width * height / 2); 
     
}
   
}
   
class Caller
   
{
     
public void CallArea(Shape sh)
     
{
         
int a;
         a 
= sh.area();
         
Console.WriteLine("面积: {0}", a);
     
}
   
} 
   
class Tester
   
{
     
     
static void Main(string[] args)
     
{
         
Caller c = new Caller();
         
Rectangle r = new Rectangle(10, 7);
         
Triangle t = new Triangle(10, 5);
         c
.CallArea(r);
         c
.CallArea(t);
         
Console.ReadKey();
     
}
   
}}

运算符重载

public static Box operator+ (Box b, Box c){
   
Box box = new Box();
   box
.length = b.length + c.length;
   box
.breadth = b.breadth + c.breadth;
   box
.height = b.height + c.height;
   
return box;}
可重载和不可重载运算符
运算符描述+, -, !, ~, ++, --这些一元运算符只有一个操作数,且可以被重载。+, -, *, /, %这些二元运算符带有两个操作数,且可以被重载。==, !=, <, >, <=, >=这些比较运算符可以被重载。&&, ||这些条件逻辑运算符不能被直接重载。+=, -=, *=, /=, %=这些赋值运算符不能被重载。=, ., ?:, ->, new, is, sizeof, typeof这些运算符不能被重载。

Namespace

命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。
命名空间可以被嵌套,即您可以在一个命名空间内定义另一个命名空间




0 0