开心

Second类

 

类:
CupofCoffee(类)aCoffee:CupOfCoffee(实例)
+BeanType:string(属性或字段)
+AddSugar(in ( out 对应 C# out inout:对应ref in:其它)


amount:byte):byte(方法 +:公共成员 -:私有成员 )

const 变量是static 的,但不能用static修饰。

对象生命周期:
构造阶段(构造函数实例化)
使用阶段
析构阶段(删除对象时做释放内存等清理工作)
abstract:抽象只能继承 sealed:不能继承,只能实例化
这两个 关键字不能在接口中使用。
Object 方法:Equals(object):对象的参数是否引用 了同一对象。
Equals(object ,object):两个 对象是否引用 了同一对象 。
ReferenceEquals(object ,object)两个 对象是否是同一实例的引用。
GetType()以System.Type对象的形式返回对象的类型
GetHashCode() : int,用作对象的散列函数,返回一个以压缩形式标识的对象状态的值。
if(myobj.GetType()==typeof(MyComplexClass))
{

}
浅度复制:如果遇到引用复制的是地址,可通过派生于System.Object的memberwiseClone()方法完成。
深度复制:继承ICloneable接口,实现它包含的Clone()方法返回类型为System.Object的值。

 

this 本类,base 基类
部分方法:
部分方法在部分类中定义 ,没有返回值,在一个 类中定义,在另一个类中写执行代码
都用partial关键字,总是私有的,不能用out参数。
如:
public parial calss MyClass
{
partial void MyPartialMethod();
}
public partial class MyClass
{
partial void MyparitalMethod()//如果注释了这段代码,程序不会出
问题运行库发现没有这个方法,就会删除这个方法,和它对它的引用 。
{

}
}
显示实现接口成员:
public class Myclass:IMyInterface
{
void IMyInterface.Dosomething()
{}
}

只能用IMyInterface myint=new MyClass();
myint.Dosomething();
调用。
用非公共的可访问性添加属性访问器:
如:
public interface IMyInterface
{
int MyIntProperty
{get;}
}
public class MyBaseClass:IMyInterface
{
protected int myInt;
public int MyIntProperty
{
get
{
return myInt;
}
protected set
{myInt=value;}
}
}

  static void Main(string[] args)
      {
         
            
         Console.WriteLine(zhu.str );
           
            
          Console.ReadKey();
        }

       
    }
  class zhu
   {
       static zhu()
       {

           str = "奇才";
           Console.Write("nihwoi");
       }
    public    static string str = "s";
    public void z()
    {
        using (zhu1 z = new zhu1())//在结尾释放资源
        {


        }

    }
     
 
      
   }
    class zhu1:IDisposable //可删除 对象
    {
    
     public void kk()
     {}
        public void Dispose()
        {}
    
    }
静态访问,IDisposable
 public  class Class1

    {
        static void Main(string[] args)
      {
         //多态

          Cow myCow = new Cow();
          Chicken myChicken = new Chicken();
          myCow.EatFood();
          myChicken.EatFood();
          Animal myAnimal = myCow;
          myCow.EatFood();
          myCow = (Cow)myAnimal;
          myCow.kk();
        //接口多态
          IAnimal ianimal;
          ianimal = myCow;
        
          ianimal.EatFood1();
          myCow = (Cow)ianimal;
          myCow.kk();
            //包含关系
          dog d = new dog();
          d.cow.EatFood();
          d.cow.EatFood1();
          

          Console.ReadKey();
        }
    }
   class dog
   {
       public Cow cow;
       private Animal ani;
      public void dogshow()
       {


           ani = new Animal();
           ani.EatFood();
           cow = new Cow();
       }
   }
   class Cow : Animal,IAnimal 
   {
       private     Animal ani;
       public   void kk()
       {
           Chicken cddd = new Chicken();
          // ani = new Animal();
           //ani.EatFood();
           
           
       }
       public void EatFood1()
       { 
       
       }
    
   
   }
   class Chicken:Animal,IAnimal 
   {
       public void moo()
       { }
       public void EatFood1()
       {

       }
   }
   interface IAnimal
   {

        void EatFood1();
   }
  class Animal
   { 
   public    void  EatFood()
    {
        
    
    }
   }
多态
 public abstract class MyBase
    { }
    public class Myclass:MyBase
    { }
    public interface IMyBaseInterface
    { }
    internal interface IMyBaseInterface2
    { }
    internal interface IMyInterface : IMyBaseInterface, IMyBaseInterface2
    { 
    
    }
    internal sealed class MyComplexClass:Myclass ,IMyInterface
    { }

   public  class Class1
    {
        static void Main(string[] args)
      {
          MyComplexClass myObj = new MyComplexClass();
           // bool b=Equals()
          Console.WriteLine(typeof(MyComplexClass ));
           
          Console.ReadKey();
        }
    }
接口调用
 public abstract class MyBase
    {
        public MyBase()
        { Console.WriteLine(1); }
    }
    public class Myclass:MyBase
    {
        public Myclass(string x, int y)
        {
            Console.WriteLine(2);
        
        }
    }
   
    internal sealed class MyComplexClass:Myclass 
    { 
    
        public MyComplexClass() :this("xx",9)
        {
            Console.WriteLine(4);
        }
        public MyComplexClass(string x,int y):base(x,y)
        {
            Console.WriteLine(3);
        
        }
    }

   public  class Class1
    {
        static void Main(string[] args)
      {
          MyComplexClass myObj = new MyComplexClass();
          
           
          Console.ReadKey();
        }
    }

输出结果:1 2 3 4 
在此之前会调用System.Object.Object()
构造函数的执行顺序

 

 static void Main(string[] args)
        {
            Deck myDeck = new Deck();
            myDeck.Shuffle();
            for (int i = 0; i < 52; i++)
            {
                Card tempCard = myDeck.GetCard(i);
                Console.WriteLine(tempCard .ToString());
                if (i != 51)
                { Console.WriteLine(", "); }
                else
                    Console.WriteLine();
            }

               
            Console.ReadKey();
        }

//2
public class Deck
    {
        private Card[] cards;
        public Deck()
        {

            cards = new Card[52];
            for (int suitval = 0; suitval < 4; suitval ++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                {
                    cards[suitval * 13 + rankVal - 1] = new Card((Suite )suitval ,(Rank )rankVal );
                }
            }
        }
        public Card GetCard(int cardNum)
        {
           if(cardNum >=0 && cardNum <=51)
           return cards[cardNum];
            else
               throw (new System .ArgumentOutOfRangeException("carNum",cardNum ,"value must be between 0 and 51;") );
        }

        public void Shuffle()
        {
            Card[] newDeck=new Card [52];
            bool [] assigned=new bool[52];
            Random sourceGen = new Random();
            for (int i = 0; i < 52;i++ )
            {
                int destCard = 0;
                bool foundCard = false;
                while(foundCard ==false )
                {
                    destCard = sourceGen.Next(52);
                    if (assigned[destCard] == false) foundCard = true;
                
                }
                assigned [destCard ]=true;
                newDeck[destCard] = cards[i];
            

            }
            newDeck.CopyTo(cards, 0);

        }
//3Card
 public class Card
    {
        private readonly Rank rank;
        private readonly Suite suit;

        private Card()
        {

        }

        public Card(Suite newSuit, Rank newRank)
        {
            suit = newSuit;
            rank = newRank;
        }

        public override string ToString()
        {
            return "The" + rank + "of" + suit + "s";
        }
    }
//4Rank
 public enum Rank
    {
        Ace = 1,
        Deuce,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King,
    }

//5suit
 public enum Suite
    { 
        Club,
        Diamond,
        Heart,
        Spade,
    }
纸牌实例

 

 class Program
    {
        static void Main(string[] args)
        {
            Animals animalCollection = new Animals();
            animalCollection.Add(new Cow("jack"));
            foreach (Animal myanimal in animalCollection)
            { myanimal.Feed(); }

            Console.WriteLine("Create an array type collecton of Animal objects and use it:");
            Animal[] animalArray = new Animal[2];
            Cow myCow1 = new Cow("Deirdre");
            animalArray[0] = myCow1;
            animalArray[1] = new Chicken("ken");
            foreach(Animal myAnimal in animalArray )
            {
                Console.WriteLine("New {0} object added toArray collection,Name={1}",myAnimal .ToString(),myAnimal.Name);

            }
            Console.WriteLine("Array collection contains{0}objects.",animalArray .Length);
            animalArray[0].Feed();
            ((Chicken)animalArray[1]).LayEgg();
            Console.WriteLine();
            Console.WriteLine("Create an arrayList type collecton of Animal objects and use it:");
            ArrayList animalArrayList = new ArrayList();
           
            Cow myCow2 = new Cow("Hayley");
            animalArrayList.Add(myCow2 );
            animalArrayList.Add(new Chicken("Roby"));
            animalArrayList[1] = new Chicken("dog");
            foreach (Animal myAnimal in animalArrayList)
            {
                Console.WriteLine("New {0} object added toArrayList collection,Name={1}", myAnimal.ToString(), myAnimal.Name);

            }
            Console.WriteLine("ArrayList collection contains{0}objects.", animalArrayList.Count );
           ((Animal ) animalArrayList[0]).Feed();
            ((Chicken)animalArrayList[1]).LayEgg();
            Console.WriteLine();

            Console.WriteLine("Additional manipulation of ArrayList:");
            animalArrayList.AddRange(animalArray);
            animalArrayList.RemoveAt(0);
            ((Animal)animalArrayList[0]).Feed();
           
            ((Chicken)animalArrayList[2]).LayEgg();
            Console.WriteLine("The animal called {0} is at index {1}.leng{2}",myCow1 .Name ,animalArrayList.IndexOf (myCow1 ),animalArrayList .Count );
            myCow1.Name = "Janice";
            Console.WriteLine("The animal is now called {0}.{1}",((Animal )animalArrayList [1]).Name ,((Animal )animalArrayList [0]).Name );
            Console.ReadKey();
        }
    }
    public class Animals:CollectionBase
    {

        public void Add(Animal newAnimal)
        { List.Add(newAnimal ); }
        public Animals()
        { }
        public Animal this[int animalIndex]
        {
            get { return (Animal)List[animalIndex]; }
            set { List[animalIndex] = value; }
        }

    }
    public abstract class Animal
    {
        protected string name;

        public  string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Animal()
        {
            name = "The animal with no name";
        }
        public Animal(string newName)
        { name = newName; }
        public void Feed()
        { Console.WriteLine("{0}has been feed,",name); }


    }
    public class Cow : Animal
    {
        public void Milk()
        {
            Console.WriteLine("{0}has been milked.",name);
        }
        public Cow(string newName)
            : base(newName)
        { 
        
        }
    }
    public class Chicken:Animal
    {

        public void LayEgg()
        {
            Console.WriteLine("{0},has laid an egg.",name);
        }
        public Chicken(string newName):base(newName)
        { }
    }
集合arraylist

 

 public class Animals:DictionaryBase
    {

        public void Add(string newID ,Animal newAnimal)
        { Dictionary.Add(newID , newAnimal ); }
        public void Remove(string animalID)
        {
            Dictionary.Remove(animalID );
            
        }
        public Animals()
        { }
        public Animal this[string  animalID]
        {
            get { return (Animal)Dictionary [animalID ]; }
            set { Dictionary [animalID ] = value; }
        }

    }
集合DictionaryBase
class Program
    {
        
       
        static void Main(string[] args)
        {
            Primes primes = new Primes();
            foreach (long i in primes)
            {
                Console.WriteLine("{0}",i);
            }
            Console.ReadKey();
        }
    }

    public class Primes {

        private long min;
        private long max;
        public Primes():this(2,100)
    {}
        public Primes(long minimum,long maximum)
        {
            if(min<2)
            {min=2;}
            else 
            {min=minimum ;}
            max =maximum;
        }
        /// <summary>
        /// 迭代器
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            for(long possiblePrime=min;possiblePrime <=max ;possiblePrime ++)
            {
                bool isPrime=true;
                for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor ++)
                {
                    long remainderAfterDivision = possiblePrime % possibleFactor;
                    if (remainderAfterDivision == 0)
                    {
                        isPrime = false;
                        break;
                        // yield break;
                    }
                }
                if(isPrime )
                {
                    yield return possiblePrime ;
                }
            
            }
集合迭代器

 

class Program
    {
        static void Main(string[] args)
        {
            Cloner mySource = new Cloner(5);
            Cloner myTarget = (Cloner)mySource.GetCopy();
            Console.WriteLine("myTarget.MyContent.Val={0}",myTarget.MyContent.Val );
            mySource.MyContent.Val = 2;
            Console.WriteLine("myTarget.MyContent.Val={0}",myTarget .MyContent .Val);
            Console.ReadKey();
        }
    }
    public class Content
    {
        public int Val;
    }
    public class Cloner
    {

        public Content MyContent = new Content();
        public Cloner(int newVal)
        {
            MyContent.Val = newVal;
        
        }
        public object GetCopy()
        {
            return MemberwiseClone();
        }
    }
浅度复制
 class Program
    {
        static void Main(string[] args)
        {
            Cloner mySource = new Cloner(5);
            Cloner myTarget = (Cloner)mySource.Clone ();
            Console.WriteLine("myTarget.MyContent.Val={0}",myTarget.MyContent.Val );
            mySource.MyContent.Val = 2;
            Console.WriteLine("myTarget.MyContent.Val={0}",myTarget .MyContent .Val);
            Console.ReadKey();
        }
    }
    public class Content
    {
        public int Val;
    }
    public class Cloner:ICloneable
    {

        public Content MyContent = new Content();
        public Cloner(int newVal)
        {
            MyContent.Val = newVal;
        
        }
        public object Clone()
        {
            Cloner clonedCloner = new Cloner(MyContent .Val );
            return clonedCloner;
            //或对MyContent深度复制
            //Cloner clonedCloner =new Cloner();
            //clonedCloner.MyContent=MyContent.Clone();
            //return clonedCloner
            
        }
    }
深度复制

 

posted @ 2016-03-20 16:06  大喜  阅读(175)  评论(0)    收藏  举报
坦然 会增进信任 - 信任 感情才会升华